Unit 6
Looping
While statement:
The while loop in C is most fundamental loop statement. It repeats a statement or block while its controlling expression is true.
The general form is:
While(condition) {
// body of loop
}
The condition can be any boolean expression. The body of the loop will be executed if the conditional expression is true.
Here is more practical example.
The output of the program is:
Tick 10
Tick 9
Tick 8
Tick 7
Tick 6
Tick 5
Tick 4
Tick 3
Tick 2
Tick 1
Do-while statement
The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.
Its general form is:
Do{
// body of loop
} while(condition);
Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of C’s loops, condition must be a Boolean expression.
The program presented in previous while statement can be re-written using do-while as:
//example program to illustrate do-while looping
#include <stdio.h>
Int main ()
{
Int n = 10;
Do
{
Printf("tick %d", n);
Printf("\n");
n--;
}while (n > 0);
}
For loop
Here is the general form of the traditional for statement:
For(initialization; condition; iteration) {
// body
}
The program from previous example can be re-written using for loop as:
//example program to illustrate for looping
#include <stdio.h>
Int main ()
{
Int n;
For(n = 10; n>0 ; n--){
Printf("tick %d",n);
Printf("\n");
}
}
The output of the program is same as output from program in while loop.
There can be more than one statement in initialization and iteration section. They must be separated with comma.
//example program to illustrate more than one statement using the comma
// for looping
#include <stdio.h>
Int main ()
{
Int a, b;
For (a = 1, b = 4; a < b; a++, b--)
{
Printf("a = %d \n", a);
Printf("b = %d \n", b);
}
}
The output of the program is:
a = 1
b = 4
a = 2
b = 3
Here, the initialization portion sets the values of both a and b. The two comma separated statements in the iteration portion are executed each time the loop repeats.
Nested Loops
Loops can be nested as per requirement. Here is example of nesting the loops.
// nested loops
#include <stdio.h>
Int main ()
{
Int i, j;
For (i = 0; i < 8; i++)
{
For (j = i; j < 8; j++)
Printf(".");
Printf("\n");
}
}
Here, two for loops are nested. The number times inner loop iterates depends on the value of i in outer loop.
The output of the program is:
The break statement is used to terminate the execution where number of iterations is not unknown. It is used in loop to terminate the loop and then control goes to the first statement of the loop. The break statement is also in switch case.
Structure of the break:
e.g.
When the compiler will reach to break it will come out and print the output. Output will be x=1,x=9,x=8,x=7,x=6,break.
The continue statement work like break statement but it is opposite of break. The Continue statement continue with execution of program by jumping from current code instruction to the next.
Structure of the break:
e.g.
Output is 1 2 3 4 5.