In this tutorial, we are going to learn about some of the iterative constructs used in the C language.
C while loop:
Most of the programming languages provide a special construct/statement using which we can repeatedly execute one or more statements as long as a condition is true.
In C, we have a while, do-while, and for as the three main looping constructs or statements.
Below is a general syntax for using a while statement:
while (condition) {
statement_1;
statement_2;
....
....
....
statement_n;
}
The block of code inside the opening and closing brace which follows the while-statement is called the while-loop body.
A while statement is used to execute some code repeatedly as long as a condition evaluates to true.
The condition is an expression that should always evaluate as either true (1) or false (0).
- If it evaluates to true, the body containing one or more code statements is executed.
- If the expression evaluates to false, the control skips executing the while-loop body.
The while-loop construct is also referred to as an entry controlled loop. Meaning, first the condition is evaluated, and only if the condition evaluates to true the body of the loop is executed. After executing the body the control is automatically transferred back to the condition and the process continues until the condition evaluates to false.
Let’s consider one example,
Below partial code is to verify if the given number is a prime number or not.
A prime number is a positive integer greater than 1, which is not divisible by any other number other than 1 and itself. Examples of a few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, etc.
#include<stdio.h>
void main() {
int num, count = 0;
printf("Enter any positive number : ");
scanf("%d", &num);
while (i <= (num / 2)) {
if ((num % i) == 0) {
count++;
break;
}
i++;
}
if ((count == 0) && (num != 1)) {
printf("The given number %d is a Prime Number.\n", num);
} else {
printf("The given number %d is not a Prime Number.\n", num);
}
}
Output:
Enter any positive number : 5
The given number 5 is a Prime Number.
Let’s consider a value for num = 47. Since 47 is not divisible by any number between 1 and 47. Hence, 47 is a Prime Number.
Let’s consider a value for num = 15. Since 15 is not divisible by any number between 1 and 15. Hence, 15 is not a Prime Number.
C do-while loop:
A do-while loop statement is used to execute a section of code at least once and then repeatedly execute the same section of code as long as a certain condition is true.
Its syntax is similar to the reverse of while loop as shown below: And note the extra ; which we have to provide at the end of while(condition).
do {
statement_1;
statement_2;
....
....
....
....
statement_n;
} while (condition);
Note: The statements inside the do-while block will be executed once before the condition in the while is evaluated.
The condition is an expression that should always evaluate to either a true or a false value.
- If it evaluates to true, the do-while body containing one or more code statements is executed again.
- If the expression evaluates to false, the control comes out of the do-while construct.
A do-while loop construct is termed as an exit controlled loop. In an exit controlled loop first, the body of the loop is executed and finally, the condition will be checked. If the condition evaluates to true it automatically transfers the control to the opening brace of the loop. If the condition evaluates to false the next statement that immediately follows the loop is executed.
In a do-while loop, the statements within the loop are executed at least once.
Let’s consider an example:
#include<stdio.h>
void main() {
int a = 10, sum = 0;
int t = a;
do { //start of do-while loop body
sum += a;
a--;
} while (a > 0); //end of do-while loop body
printf("The sum of first %d numbers is %d\n.", t, sum);
}
Output:
The sum of first 10 numbers is 55
Let’s discuss some points here:
- The condition in the above example is a > 0.
- The code inside the do-while loop’s block between the opening-brace { and the closing-brace }, will be repeatedly executed until the condition evaluates to false.
- In our case, since in every iteration the value is decremented using a–;, after four iterations the value will be equal to 0 and the condition a > 0 will evaluate to false.
Note: do-while is one of the rarest control structures in C where there is a ; at the end of the statement. You will not see that in case of for, while switch, if or other data structures.
C for loop:
A for-loop is used to iterate over a range of values using a loop counter, which is a variable taking a range of values in some orderly sequence (e.g., starting at 0 and ending at 10 in increments of 1).
The value stored in a loop counter is changed with each iteration of the loop, providing a unique value for each individual iteration. The loop counter is used to decide when to terminate the loop.
A for-loop construct can be termed as an entry controlled loop.
The basic syntax of a for loop is as follows:
for (initialization; condition; update) {
statement(s);
}
Let’s discuss some points here:
- The initialization expression initializes the loop counter; it is executed once at the start of the loop.
- The loop continues to execute as long as the condition expression evaluates to true.
- The update expression is executed after each iteration through the loop, to increment, decrement, or change the loop counter.
Let’s consider one example,
#include<stdio.h>
void main() {
int i = 0;
for (i = 0; i < 10; i++) { //beginning of for loop
printf("%d\n", i);
} //ending of for loop
}
Output:
0
1
2
3
4
5
6
7
8
9
The above code prints the numbers from 0 to 9.
Let’s discuss some points here:
- The above for-loop statement initializes an integer variable i (which is the loop counter) as part of the initialization expression.
- In the update section, it increments the variable i by 1 using the post-increment expression i++.
- The expression in the condition is i < 10. The for-loop keeps on executing the code inside the loop body as long as this condition evaluates to true. And the loop terminates when the condition evaluates to false.
- It is a good practice to always keep the loop body (which contains the code to be executed) within an opening-brace { and a closing-brace }.
Note: No ; at the end of the for a statement.
REFERENCES:
- Wiki – Control flow statements
- Wiki – Iterations
- Wiki – For loop
- Wiki – While loop
- Wiki – do-while loop
Happy Learning