Loop Control Statements
Control Statements
- For Loop
- While Loop
- Do While
- Break and Continue
- Switch Statement and Break
Looping and Iteration
This chapter will look at C's mechanisms for controlling looping and iteration. Even though some of these mechanisms may look familiar and indeed will operate in a standard fashion most of the time.
The for statement
The C for statement has the following form:
Syntax:
for (expression1;Condition;expression2)
statement;
for (expression1;Condition;expression2) {
block of statements
}
expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);
NOTE: C basically treats for statements as while type loops
For loop example program:
/* Example Program For for Loop In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
// Variable Declaration
int x=3;
//for loop
for (x=3;x>0;x--)
{
printf("x=%dn",x);
}
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Output:
x=3
x=2
x=1
The while statement
The while statement is similar to those used in other languages although more can be done with the expression statement -- a standard feature of C.
The while has the form:
Syntax:
while (expression)
statement;
while (expression)
block of statements
}
While statement example program
/* Example Program For while Loop In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
// Variable Declaration
int x=3;
//while loop
while (x>0)
{
printf("x=%dn",x);
x--;
}
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Output:
x=3
x=2
x=1
The do-while statement
Syntax:
C's do-while statement has the form:
do {
statement;
}while (expression);
It is similar to PASCAL's repeat ... until except do while expression is true.
do while Loop example:
/* Example Program For Do While Loop In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
// Variable Declaration
int x=3;
//do while loop
do {
printf("x=%dn",x--);
}while (x>0);
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
outputs:
x=3
x=2
x=1
NOTE: The postfix x- operator which uses the current value of x while printing and then decrements x.
C provides two commands to control how we loop: break and continue
break -- exit form loop or switch.
continue -- skip 1 iteration of loop.
Consider the following example where we read in integer values and process them according to the following conditions. If the value we have read is negative, we wish to print an error message and abandon the loop. If the value read is great than 100, we wish to ignore it and continue to the next value in the data. If the value is zero, we wish to terminate the loop.
Switch Statement and Break
In C programming language, the switch statement is a type of selection mechanism used to allow block code among many alternatives.Simply, It changes the control flow of program execution via multiple blocks.
Top Pages
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C