In C programming, controlling the flow of the program is the key to unleashing the true potential of your code. In this article, we will look into the loops and control statements which are fundamental constructs and shape the flow of execution in your programs.
These are multiple ways in which we can repeat a part of a program. They are:
- The for Loop
- The while Loop
- The do-while Loop
- The break Statement
- The continue Statement
Table of Contents
- The for Loop
- The while Loop
- The do-while Loop
- The break Statement in C
- The continue Statement
- The Case Control Structure: switch Statement
The for Loop
A for
loop is a control flow statement that allows you to repeatedly execute a block of code a specified number of times. It is useful when you know in advance how many times you want to iterate. The for
loop consists of three components: initialization, condition, and increment/decrement. It has the following standard form.
1 2 3 | for (initialization; condition; increment/decrement) { // Code to be executed } |
Here the initialization step is executed first, and only once. Then condition is evaluates to see if it’s true or false. If true, the body of the loop is executed otherwise body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After each time the body of the for loop executes, the increment/decrement counter value is increased/decreased based on the increment counter.
Now let us write a simple interest problem using the for loop
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> int main () { // for loop execution for( int a = 5; a <= 10; a ++ ) { printf("Value of a: %d \n", a); } return 0; } |
When the above code is compiled and executed the result is as below:
1 2 3 4 5 6 | Value of a: 5 Value of a: 6 Value of a: 7 Value of a: 8 Value of a: 9 Value of a: 10 |
The while Loop
A while
loop is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition is true. Unlike the for
loop, the while
loop only consists of a condition, and the loop continues as long as the condition remains true.
1 2 3 | while (condition) { // Code to be executed } |
The parentheses after the while contains a condition so long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly for e.g.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main () { int a = 5; // while loop execution while(a <= 10) { printf("Value of a: %d\n", a); a++; } return 0; } |
When the above code is compiled and executed the result is as below:
1 2 3 4 5 6 | Value of a: 5 Value of a: 6 Value of a: 7 Value of a: 8 Value of a: 9 Value of a: 10 |
The do-while Loop
The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the do-while statement. So do-while loop is always executed at least once. Its general form is
1 2 3 4 | do { statements(s); } while (condition); |
There is a minor difference between the working of while and do-while loops. The difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this the do-while tests the condition after having executed the statements within the loop. for e.g.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main () { int a = 11; // while loop execution while(a <= 10) { printf("Value of a: %d\n", a); a++; } return 0; } |
In the above example the printf()
will not get executed at all since the condition fails at the first time itself. Now let’s write the same program using a do-while loop.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main () { int a = 11; // while loop execution do { printf("Value of a: %d\n", a); a++; } while(a <= 10); return 0; } |
In the above program the printf() would be executed once, since first the body of the loop us executed and then the condition is tested.
The break Statement in C
The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any loop in C, control automatically passes to the first statement after the loop. For example the following program is to determine whether a number is prime or not.
To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. It the remainder of any of the divisions is zero, the number is not a prime. Following program implements this logic:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> int main(){ int num, i; printf("Enter a number \n"); scanf("%d", &num); i = 2; while (i <= num -1){ if (num%i == 0){ printf("Number: %d is not a prime number \n", num); break; } i++; } if(i == num){ printf("Number: %d i Prime Number \n", num); } return 0; } |
The continue Statement
The keyword continue
allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> int main( ) { int i,j; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) { if (i == j) continue; printf("%d %d \n", i, j); } } } |
The output of the above C program is:
1 2 3 4 5 6 | 1 2 1 3 2 1 2 3 3 1 3 2 |
when the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statements pending execution in the for loop (inner).
The Case Control Structure: switch Statement
The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression that is included within the switch statement.
The form of switch statement is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | switch (integer expression) { case constant 1: do this; break; case constant 2: do this; break; case constant 3: do this; break; default; do this; } |
Here are some of the rules which apply on switch statement in C language.
- The expression used in a switch statement should have an integral or enumerated type.
- There can be multiple case statement with in switch statement where case is followed by the value to be compared to and a colon.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- If there is no break statement appears within a case statement, the flow of the program continues until break statement is is reached or the switch statement end block reaches.
- A switch statement can have an optional default that can be used to execute some statements when no case statements are met before. As default is the last statement in switch so no break; statement is required to terminate this.
If you want to further understand conditional statements, then study the following C Programs: