17 Oct Loops in C Programming
Loops are used when you need to repeat a part of the program. In C, we have loops; through which we can repeat a part of the program until a condition is satisfied. In this lesson, we will learn how to work with loops in C Programming.
Here are the loops,
- while loop
- do-while loop
- for loop
while loop
The while loop in C language is used when you want to repeat a part of the program a fixed number of times. This is specified as a condition.
For example, marks of students with more than 75 in Maths.
Syntax
The following is the syntax of the while loop in C Language,
1 2 3 4 5 6 7 8 9 10 11 12 |
loop counter initialization; while (condition is true) { statement1; statement2; . . . loop counter increment; } |
The following is an example showing the usage of the while loop in C Language,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> void main() { int i = 50; printf("Displaying Values!\n"); // while loop condition while( i < 55 ) { printf("i = %d\n", i); i++; } getch(); } |
The following is the output,
do-while loop
The do-while is the same as while, but the do-while executes the statement first and then the condition is tested.
Syntax
The following is the syntax of using the do-while loop in C Language,
1 2 3 4 5 6 7 8 9 10 11 12 |
loop counter initialization; do { Statement1; Statement2; . . . loop counter increment; } while(condition is true) |
The following is an example showing the usage of the do-while loop in C Language,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> void main() { int i = 50; printf("Displaying Values!\n"); do { printf("i = %d\n", i); i++; } while( i < 55 ); getch(); } |
The following is the output showing the usage of the do-while loop in C Language,
for Loop
The for loop is a loop that executes a specified number of times. Under this, initialize, test, and increment loop counter in a single line, unlike the while and do-while loop.
Syntax
The syntax of for loop shows the usage of the loop counter, its initialization, as well as condition,
1 2 3 4 5 6 7 8 9 10 |
for (initialize loop counter; test loop counter; loop counter increment) { Statement1; Statement2; . . . } |
The following is an example showing the usage of for loop in C language,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> void main () { int i; for( i = 0; i < 5; i++ ) { printf("i = %d\n", i); } getch(); } |
The output shows the value of i iterated five times with for loop,
Loop Control Statements
Repeatedly execute a block of code using the loop control statements. The following are the loop control statements in C Language:
- break statement
- continue statement
break Statement
Use the break statement, if you want to jump out of a loop. The control passes to the first statement after the loop, since the break statement terminates the loop.
The syntax for break statement:
1 2 3 |
break; |
Example
The following is an example showing the usage of the break statement in C language,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> void main () { int i = 5; while( i < 10 ) { printf("i = %d\n", i); i++; if( i > 7) { break; } } } |
The following is the output showing the usage of the break statement in C Language,
continue statement
Use the continue statement, if you want to pass control to the next iteration of the loop. However, it skips any code in between.
The syntax for continue statement in C language,
1 2 3 |
continue; |
The following is an example showing the usage of the continue statement,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> void main () { int val1 = 100; do { if( val1 == 110) { val1 = val1 + 1; continue; } printf("%d\n", val1); val1++; } while( val1 < 115 ); getch(); } |
The following is the output,
In this lesson, we learned how to work with loops in C Language. We saw the implementation of the while loop, do-while loop, for loop, break statement, and continue statement.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments