C Programming Professional Made easys (2015)
Chapter 8 C Loops
In case you have to execute a block of code a few times, you should execute the statements sequentially. This means that you have to execute the first statement in a function first, and then the second, and so on.
A loop statement allows you to execute a statement or a number of statements several times. The following is the general form of a loop statement:
The Different Types of Loops
While Loop
The while loop repeatedly executes a particular statement if the given condition is true.
Syntax
The general syntax of a while loop is as follows:
while (condition)
{
statement(s);
}
You can use a single statement or a block of statements in a while loop. Also, you can give a condition of any expression, but non-zero values are considered true. As long as the condition is true, the loop continues to iterate. Once the condition becomes false, the program control passes to the line that immediately follows the loop.
Flow Diagram
If the condition of the while loop is tested and found to be false, then the first statement after the while loop is executed while the loop body is skipped.
Example:
#include <stdio.h>
int main ( )
{
/* local variable definition */
int x = 10;
/* while loop execution */
while ( x < 20)
{
printf (“The value of x: %d\n”, x);
x++;
}
return 0;
}
Once the above code has been executed, the following output is shown:
The value of x: 10
The value of x: 11
The value of x: 12
The value of x: 13
The value of x: 14
The value of x: 15
The value of x: 16
The value of x: 17
The value of x: 18
The value of x: 19
For Loop
The for loop is used to repeat a certain block of codes for a certain number of times.
Syntax
This is the general syntax for a for loop:
for ( init; condition; increment )
{
statement(s);
}
Flow Diagram
The Flow of Control in a For Loop
First, the init is executed to enable you to initialize and declare loop control variables. As long as you use a semicolon, there is no need for you to place a statement here.
Then, the condition is evaluated. If it is proven to be true, then the loop is executed. Otherwise, the flow of control moves on to the next statement.
After the body of the for loop is executed, the flow of control moves back to the increment. The increment statement allows you to update your loop control variables. You can leave it blank if you want for as long as you use a semicolon after your condition.
Finally, the condition is re-evaluated. If it is found to be true, then the loop executes and the entire process is repeated. When the condition becomes false, the for loop terminates.
Example:
#include <stdio.h>
int main ( )
{
/* for loop execution */
for ( int x = 10; x < 20; x = x + 1)
{
printf (“The value of x: %d\n”, x);
}
return 0;
}
Once the above code is executed, the following output is shown:
The value of x : 10
The value of x : 11
The value of x : 12
The value of x : 13
The value of x : 14
The value of x : 15
The value of x : 16
The value of x : 17
The value of x : 18
The value of x : 19
Different Ways of Using For Loop
If you want to perform a certain action multiple times, you can use loop control statements. You can write for loop using the following:
1. Single Statement Inside For Loop
Example:
for (i = 0; i < 5; i++)
printf (“Programming”);
2. Multiple Statements Inside For Loop
Example:
for (i = 0; i < 5; i++)
{
printf (“First Statement”);
printf (“Second Statement”);
printf (“Third Statement”);
if (condition)
{
}
}
3. No Statement Inside For Loop
Example:
for (i = 0; i < 5; i++)
{
}
4. Semicolon at the End of For Loop
Example:
for (i = 0; i < 5; i++);
5. Multiple Initialization Statement Inside For Loop
Example:
for (i = 0; j = 0; i < 5; i++)
{
statement 1;
statement 2;
statement 3;
}
6. Missing Increment/Decrement Statement
Example:
for (i = 0; i < 5; )
{
statement 1;
statement 2;
statement 3;
i++
}
7. Missing Initialization in For Loop
Example:
i = 0;
for ( ; i < 5; i++ )
{
statement 1;
statement 2;
statement 3;
}
8. Infinite For Loop
Example:
i = 0;
for ( ; ; )
{
statement 1;
statement 2;
statement 3;
if (breaking condition)
break;
i++;
}
Do While Loop
Just like the while loop, the do while loop is also used for looping. The loop condition is tested at the end of the loop and the loop is executed at least once. However, unlike the while loop, the do while loop is rarely used by most programmers.
Syntax
The general syntax for the do while loop is as follows:
Do
{
statement(s);
}
while (condition);
As you can see from the above example, the conditional expression is found at the end of the loop. Hence, the statement(s) in the loop is/are executed once before the condition is tested. If the condition is found to be true, the flow of control moves back up to do and the statement(s) is/are executed once more. The process continues to repeat until the condition is found to be false.
Flow Diagram
Example:
#include <stdio.h>
int main ( )
{
/* local variable definition */
int x = 10;
/* do loop execution */
do
{
printf (“The value of x: %d\n, x);
x = x + 1;
}
while (x < 20);
return 0;
}
Once the above code is executed, the following output is shown:
The value of x: 10
The value of x: 11
The value of x: 12
The value of x: 13
The value of x: 14
The value of x: 15
The value of x: 16
The value of x: 17
The value of x: 18
The value of x: 19
Nested Loops
The usage of one loop inside another is allowed in the C programming language. This is what nested loops are all about.
Syntax
This is the general syntax for nested loops:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
{
statement(s);
}
This is the general syntax for a nested do while loop:
do
{
statement(s);
do
{
statement(s);
}
while (condition);
}
while (condition);
Take note that when it comes to loop nesting, you can place whatever type of loop you want inside any other type of loop. For instance, you can put a while loop inside a for loop and vice versa.
Example:
#include <stdio.h>
int main ( )
{
/* local variable definition */
int a, b;
for (a = 2; a < 100; a++)
{
for (b = 2; b <= (a/b); b++)
if (! (a % b)) break; // if a factor is found, it is not a prime number
if (b > (a/b)) printf (“%d is a prime number\n”, a);
}
return 0;
}
Once the above code is executed, the following output is shown:
2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
53 is a prime number
59 is a prime number
61 is a prime number
67 is a prime number
71 is a prime number
73 is a prime number
79 is a prime number
83 is a prime number
89 is a prime number
97 is a prime number
Break Statement
The break statement contains two main functions:
1. If the break statement is found in a loop, then the loop is terminated immediately and the program control goes to the next statement that follows the loop.
2. The break statement can be used for case termination in a switch statement.
If you are running nested loops, the break statement stops the running of the innermost loop and begins executing the code found after it.
Syntax
This is the general syntax of a break statement:
break;
Flow Diagram
Example:
#include <stdio.h>
int main ( )
{
/* local variable definition */
int y = 10;
/* while loop execution */
while (y < 20)
{
printf (“The value of y: %d\n”, y);
y++;
if (y > 15)
{
/* terminate the loop using break statement */
break;
}
}
return 0;
}
Once the above code is executed, the following output is shown:
The value of y: 10
The value of y: 11
The value of y: 12
The value of y: 13
The value of y: 14
The value of y: 15
Continue Statement
The continue statement is similar to the break statement, except that it does not force termination but rather continues to force the next loop iteration to occur while it skips any codes in between.
If you are using a for loop, the continue statement causes the increments and conditional tests of the loop to execute. If you are using a do while or while loop, the continue statement causes the program control to move on to the conditional tests.
Syntax
The following is the general syntax for a continue statement:
continue;
Flow Diagram
Example:
#include <stdio.h>
int main ( )
{
/* local variable definition */
int y = 10;
/* do loop execution */
do
{
if ( y == 15 )
{
/* skip the iteration */
y = y + 1;
continue
}
printf (“The value of y: %d\n”, y);
y++;
}
while ( y < 20 );
return 0;
}
Once the above code is compiled and executed, the following output is shown:
The value of y: 10
The value of y: 11
The value of y: 12
The value of y: 13
The value of y: 14
The value of y: 16
The value of y: 17
The value of y: 18
The value of y: 19
Goto Statement
The goto statement causes the control to jump to the corresponding label that is mentioned with goto. However, goto is rarely used for applications and level prorgamming because it can be quite confusing, complex, and less readable. It also makes the control of the program difficult to trace. Likewise, it tends to make debugging and testing hard to do.
Syntax
This is the general syntax of a goto statement:
goto label;
. .
. .
label: statement;
Flow Diagram
Example:
#include <stdio.h>
int main ( )
{
/* local variable definition */
int w = 10;
/* do loop execution */
LOOP:do
{
if ( w == 15)
{
/* skip the iteration */
w = w + 1;
goto LOOP;
}
printf (“The value of w: %d\n”, w);
w++;
}
while ( w < 20 );
return 0;
}
Once the above code is executed, the following output is shown:
The value of w: 10
The value of w: 11
The value of w: 12
The value of w: 13
The value of w: 14
The value of w: 16
The value of w: 17
The value of w: 18
The value of w: 19
The Infinite Loop
It can be said that a loop is infinite if its condition never becomes false. Traditionally, the for loop is used for this purpose. However, since the expressions found in the for loop are not required, you can leave out the condition to create an endless loop.
#include <stdio.h>
int main ( )
for ( ; ; )
{
printf (“This is an endless loop.\n”);
}
return 0;
}
If the condition is not present, then it is presumed to be true. It is alright to use an increment expression or initialization, but you can also use the for (; ;) construct if you want to indicate an endless loop. Also, you can press the Ctrl + C keys to terminate an endless loop.