C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a block of code multiple times and it reduces the length of the code. Example:  Let suppose we have to print counting 1to 50 then we have to use 50 times lines of code but if we use loop then print it by 2 or 5 lines of code.

  • for loop
  • while loop
  • do while loop
Difference between conditional and looping statement: The conditional statement executes only once in the program but looping statements executes repeatedly several number of times. for Loop The for loop statement is used to execute the code until a condition is false. In this section, there are 3 parts initialization, condition and increment or Decrements. 

   

The following syntax of for Loop:
for(initialization;condition;incr/decr)
{
//code to be executed
}
 Example1
#include<stdio.h>
int main(){
int i;
 for(i=0;i<=5;i++){
 printf("%d \n", i);
 } 
 return 0;
}
Output
0 
1 
2 
3 
4 
5
Example 2 //To print any childish table of any number
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,temp,i;
printf("Enter any number for Table :");
scanf("%d",&n);
for(i=1;i<=10;i++){
 temp=n*i;
printf("%d * %d = %d \n",n,i,temp);
}
return 0;
}
Output
Enter any number for Table :32764 * 1 = 32764 
32764 * 2 = 65528 
32764 * 3 = 98292 
32764 * 4 = 131056 
32764 * 5 = 163820 
32764 * 6 = 196584 
32764 * 7 = 229348 
32764 * 8 = 262112 
32764 * 9 = 294876 
32764 * 10 = 327640
While Loop
While loop is an entry-controlled loop statement. The basic format of the while statement is:

Syntax:
while( test condition{
body of loop;
}
Or
Assignment;
while(condition)
{
Statements;
......
Increment/decrements (++ or --);
}
    

Note: While loop is used when the number is uncertain or unknown.

Example:
#include<stdio.h>
int main(){
printf("To Print Counting by using While Loop \n");
int i=1;
while(i<5){
printf("\n%d",i);
i++;
}
 return 0;
}
Output
To Print Counting by using While Loop 

1
2
3
4
Do while Loop
Do while loop is used to execute the program. In do while loop, the first part of the execution is body then evaluate the text condition. If the condition is true, the body of the loop is executed again and again until the condition becomes false.

Syntax:
do{
Statement(s);
}while(condition);
Let us consider the flow chart of the C program 

   

Let us consider an example:
#include<stdio.h>
int main(){
 int a=1;
 do{
 printf("Couning: %d\n",a);
  a=a+1;
 }while(a<5);
return 0;
}
Output
Couning: 1
Couning: 2
Couning: 3
Couning: 4



ADVERTISEMENT
ADVERTISEMENT