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.