How Loops (for,while,do..while) Works in C Language
In This Post you
will learn how loops work in c language with the help of examples. Before
learning how loops work in c language?. We should know why we use loops?
For example if we
want to print ten times hello launchpadlwd then we have to write print ten
times, but writing printf ten times is not a good technique, instead we can use
loops and perform same task in a single line let’s see how?
Example 1 using printf:
#include<stdio.h> int main() { printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); printf("hello launchpadlwd"); return 0;
} |
Example 2 using loop:
#include<stdio.h> int main() { int i; for(i=1;i<=10;i++) printf("welcome to
launchpadlwd"); return 0;
} |
So we, can see the
output of example 1 and 2 are same but there is difference in code by using
loops we can perform the same program in less code.
C Programming has three types of loops:
For loop
While loop
Do...while loop
For
loop in c language
Syntax of for loop
is :
Rotate your phone to see the table:
1 2 4
For(initializationstatement;testexpression;updatestatement)
{ 3
// statements inside the body of for loop
}
1 2 4
For(initializationstatement;testexpression;updatestatement)
{ 3
// statements inside the body of for loop
}
How for loop works?
1.The
initialization statement is executed first and only once.
2.Then, the test expression is evaluated at second. If the test expression is evaluated to false, the for loop is terminated(exit).
3.However, if the test expression is evaluated to true, statements inside the body of for loop are executed, and the update expression is updated.
4.Again the test expression is evaluated.
5.This process goes on until the test expression is false. When the test expression is false, the loop terminates.
Example 3 for loop in c
#include<stdio.h> int main() { int i; for(i=1;i<=5;i++) printf(“%d",i); return 0;
} |
·
Step 1 i is initialized to 1.
·
Step 2 The test expression i < 5 is evaluated. Since 1 less than 5 that is
true, then the body of for loop is executed.
·
Step 3 This will print the 1 (value of i) on the screen.
·
Step 4 The update statement i++ is executed. Now, the value of i will be 2.
Again, the test expression is evaluated if found true, then body of for loop is
executed else the loop is terminated. This will print 2 (value of i) on the
screen.
·
Again,
the update statement i++ is executed and the test expression i < 5 is
evaluated. This process goes on until i becomes 5.
· When i becomes 5, i < 5 will be false, and the for loop terminates.
While loop
While studying for loop
we known that the number of times loop will execute, i.e. the number of times
the loop body will be executed is known to us. while loops are used in
situations where we do not know the exact number of times the loop will execute.
The loop execution is terminated on the basis of test condition
Syntax of while
loop is:
Rotate your phone
to see table:
While(Text Expression) {
// Statements
inside the body of while loop } |
How while loop
works?
Step 1 The while loop evaluates the test expression inside the parenthesis ().
Step 2 If the test expression is true, statements inside the body of while
loop are executed. Then, the test expression is evaluated again.
Step 3 The process goes on until the test expression is evaluated to false.
Step 4 If the test expression is false, the loop terminates (ends).
#include
<stdio.h> int main() { int a = 1; while (a <= 5) { printf("%d\n", i); i++; }
return 0; } |
Do...While loop
Do...While loop
The do..while loop
is similar to the while loop with one important difference In do while loop the
loop execution is terminated on the basis of test condition. The main
difference between do while loop and while loop is do while loop condition is
tested at the end of loop body, i.e do while loop is exit controlled whereas
the other two loops are entry controlled loops.
Note: In do while
loop the loop body will execute at least once irrespective of test condition.
Do..while loop
syntax:
Rotate your phone to see the box:
Do { //statements inside the body of do..while
loop } While(Text
Expression); |
How do...while loop
works?
Step 1 The body of do...while loop is executed once. Only then, the test
expression is evaluated.
Step 2 If the test expression is true, the body of the loop is executed again
and the test expression is evaluated.
Step 3 This process goes on until the test expression becomes false.
Step 4 If the test expression is false, the loop ends.
Example of
Do..while loop:
#include
<stdio.h> int main() { Int a=1;
// the body of the loop is executed at
least once do { printf("%d”, a); i++; } while(number i<=5);
return 0; } |
0 Comments
Please do not enter any spam links in the comment box