Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



Loops

Loops cause program to execute certain block of code repeatedly on the basis of given condition. In programming loops are used in performing repetitive tasks. Consider these scenarios

  • When you want to execute some code/codes 1000 times.
  • You want to execute some code/codes certain number of times depending upon input from the user.

These types of task can be done in programming using loops.
There are basically 3 types of loops in C programming:

1.for loop

2.while loop

3.do-while loop

for loop
Syntax
for(initialization statement; test condition; update statement) {
code/s to be executed;
}
How for loop works in C programming?

First of all we know that a for loop has parts i.e. initialization (declaration), condition, update statement, and body of the for loop where all the statements to be executed written. In this the initialization statement is executed only once at the beginning of the for loop. And then the conditional expression is checked by the program. If conditional expression is true then the code/codes inside body of for loop is executed and finally then update expression is updated. But if the test expression is false, for loop is terminated in first time. This process repeated until test expression is false.

Below given flowchart describes the working of for loop in C programming.



#include <stdio.h>
int main(){
int n, count, sum=0;
printf("enter the value of n");
scanf("%d",&n);
for(count=1;count<=n;++count) //for loop terminates if count>n
{
sum+=count; /* this statement is equivalent to sum=sum+count */
}
printf("Sum=%d",sum);
return 0;
} RUN

In this program, the user is asked to enter the value of n. Suppose you entered 20 then, count is initialized to 1 at first. Then after the test expression in the for loop, i.e., (count is less than equals to n) becomes true. So that the code in the body of for loop is executed which makes value of sum variable to 1. Then, the expression ++count (increases the value of count to + 1) is executed and again the conditional expression is checked, which is true. Again, the body of for loop is executed which makes sum to 3 and this process continues until the value of count is to set to 20. When count is set to 20, the test condition becomes false and the for loop is terminated.

while loop
Syntax
while (test expression) {
statement/s to be executed.
}

The while loop checks whether the conditional expression is true or not. If it is true, code/codes inside the body of while loop is executed,that is, code/codes inside the braces { } (curly brackets) are executed. Then again the conditional expression is checked whether test condition is true or not. This process continues until the test condition becomes false.



#include <stdio.h>
int main(){
int number=5,factorial=1;
while (number>0){ /* while loop continues util test condition number>0 is true */
factorial=factorial*number;
--number;
}
printf("Factorial=%d",factorial);
return 0;
} RUN

do-while loop

In C, do...while loop is very similar to while loop. The only difference between while and do..while loop is that, in while loops, test condition checked at first but, In do...while loop code is executed first then the condition is checked weather it is true or false. So, the code written inside do while is executed at least once.

Syntax
do {
some code/codes;
}
while (test expression);

At first code/codes inside body of do is executed. Then, the test condition is checked. If it is true, code/codes inside body of do are executed again and the process continues until test expression becomes false(or zero).

Notice, that there is semicolon in the end of while (); in do..while loop.



#include <stdio.h>
int main(){
int s=0,n;
printf("enter the value of n:");
scanf("%d",&n);
do /* Codes inside the body of do...while loops are at least executed once. */
{
s+=n;
n=n-1;
}
while(n!=0);
printf("sum=%d",s);
return 0;
} RUN

In this C program, user is asked a number and it is added with s. Then, test condition in the do...while loop is checked. And if the test condition is true,i.e, n is not equal to 0, the body of do...while loop is again executed until n equals to zero.

Loop Control Statements:

Loop control statements change execution from its normal sequence of executing program (i.e. procedural). When execution leaves the scope, all automatic objects and variable that were created in that scope are destroyed.



Related Videos