This Tutorial explains various loops in C with example usage.
A loop statement is one that repeatedly executes, ensuring a given condition to be true prior to execution of each iteration, to perform some computations/work. As the condition turns false, loop terminates.
There are three types of loops in C programming language.
While Loop in C
The while loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The while loop can be used to iterate over a collection of data, wait for an external event to occur, or repeatedly execute a block of code until a specific condition is met.
Before each iteration, the condition is verified. The condition must be a boolean expression; that is, it must evaluate to either true or false. If the condition is true, the while loop’s body is executed; if not, the body is skipped, and execution moves on to the statement that follows the while loop.
While Loop Syntax
/* * In while, condition is tested in the beginning of each iteration */ while (condition) { statements; }
While Loop Examples:
Example 1:
/* echo.c -- repeats input */ #include <stdio.h> int main(void) { int ch; /* * 1. getchar() function reads-in one character from the keyboard * at a time * 2. return type of getchar() is int * 3. getchar() returns integer value corresponding to char read * from the keyboard */ /* * ch is checked if it is not same as '!', and as match occurs, * loop terminates. */ while ((ch = getchar()) != '!') putchar(ch); return 0; }
Example 2:
/* echo.c -- repeats input Indefinitely */ #include <stdio.h> int main(void) { int ch; /* * 1. getchar() function reads-in one character from the keyboard * at a time * 2. return type of getchar() is int * 3. getchar() returns integer value corresponding to char read * from the keyboard */ /* * Non-Zero value as a condition causes loop to Indefinitely * Iterate Over. * key-in ctrl+c on Linux to terminate the program. */ while (500.345) { ch = getchar(); putchar(ch); } return 0;
For Loops in C
A for loop is a type of loop that is used in many programming languages. A for loop is similar to a while loop, but the main difference is that a for loop has an initializer, condition, and increment/decrement.
The syntax of a for loop in C programming is:
for (initialization; condition; adjustment) { statements; }
The initializer is used to initialize the loop control variable. The condition is checked each time before execution of the statements. If the condition evaluates to true, then the statements are executed. And if the condition evaluates to false, then execution of the statements is terminated. The increment/decrement step can be used to change the value of the control variable each time through the loop.
For Loop Examples:
Example 1:
#include <stdio.h> int main() { for (int i=1; i<=5; i++) { printf("%d\n",i); } return 0; }
Example 2:
/* echo_eof.c --- repeating input to end-of-file */ #include <stdio.h> int main(void) { int ch; /* * EOF is end-of-file and defined as a Macro, in the stdio.h header file * functions like fgetc(), getc() and getchar() return EOF on end of file * adjustment statement not required so left blank */ for (ch = getchar(); ch != EOF; ) { putchar(); ch = getchar(); } return 0; }
Example 3:
/* echo_eof.c --- repeating input Indefinitely */ #include <stdio.h> int main(void) { int ch; /* * 1. initialization & adjustment statements not required so left blank * 2. By default, initialization, conditional & adjustment statements, * all three are Optional. If not given, they are considered TRUE. */ for ( ; 200 ; ) { putchar(); ch = getchar(); } return 0; }
Do While Loop in C
A do while loop is a type of loop that runs a set of instructions until a specified condition is met. The do while loop is similar to the while loop, except that the do while loop will always run the code at least once before checking the condition.
To create a do while loop in C, you use the following syntax:
Do-while loop Syntax:
/* * In do-while: condition is tested in the end of each iteration */ do { statements; } while(condition);
The code inside the do {} block will run until the condition inside the while () statement evaluates to false. When the condition is false, the program will exit the do while loop and continue running any code that follows.
Do While Loop Examples:
Example 1:
#include <stdio.h> int main(void) { int x = 1; do { printf("%d\n", x); x++; } while (x <= 10); }
In this example, we’ve created a do while loop that will print out the numbers 1 through 10. The variable x is initialized to 1 and then incremented by 1 each time through the loop. The condition in the while statement checks to see if x is less than or equal to 10. As long as this condition evaluates to true,the code in the do {} block will keep running. Once x becomes greater than 10,the condition will evaluate to false and the program will exit the do-while loop.
Example 2:
/* echo.c -- repeats input */ #include <stdio.h> int main(void) { int ch; /* * 1. ch is checked if it is not same as '!', as match occurs, * loop terminates. * 2. Even if the first character reads-in is '!', it is printed * on the screen & then loop terminates! */ do { ch = getchar(); putchar(ch); } while(ch != '!') return 0; }
This is important to note that any Non-Zero value, integer, floating point values, as a condition is considered to be true while 0 is false!
Example 3:
/* echo.c -- repeats input indefinitely */ #include <stdio.h> int main(void) { int ch; do { ch = getchar(); putchar(ch); } while(1) return 0; }
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Check C Books
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for C Internship