C105(3): Conditional

Repetitive statement allows a same statement(s) to be executed again and again until specified condition becomes false. Repeatative statement also called loop.

The syntax for the For loop is

for (initialise; condition; increment){
//code block
}

following for loop example, display the message 5 times.

for(i = 0; i < 5; i++){
  printf("Hi, this is loop example");
}

on each loop, condition i < 5 is checked, if this condition is true, the output statement is executed. The loop is exited when the condition becomes false. The for loop execution is explained:

  • on first iteration, i is 0, so 1 < 5 is true, so message is displayed. i is incremented by 1 and goes to second iteration with i value of 2.
  • on second iteration i is 2, so condition 2 < 5 is true, the message is displayed again and i incremented by 1.
  • on third iteration i is 3 , 3 < 5 is true, message is displayed and i becomes 4.
  • on fourth iteration i is 4, 4 < 5 is true, message is displayed again and i becomes 5 and
  • on fifth iteration 5 < 5 is false, this time the output statement will not execute because program exit the loop.


this is the output for the loop discussed earlier:

Hi, this is loop example
Hi, this is loop example
Hi, this is loop example
Hi, this is loop example
Hi, this is loop example

Second type is the while loop, following is the syntax

while(condition){
  //code block
}

for example,

int i = 0;
while(i < 5){
  printf("Hi, this is loop example");
  i = i+1; // same as i++
}

this examples will produce same output as the for loop example just now. Only difference is it is written using while loop. the variable i used in this condition is declared outside the while loop. And the increment statement (i = i + 1) is written inside the loop code block, together with the statement(s) that we want to repeat.

Hi, this is loop example
Hi, this is loop example
Hi, this is loop example
Hi, this is loop example
Hi, this is loop example

Third type is the do...while loop, ant syntax as follows:

do{
//code block
}while(condition);

For the do while loop example below, instead of using the index to control we use a sentinel value to stop the loop.You can use sentinel values in while loop too. the variable for the sentinel value is called input, because we will store input from user in this variable.

int input;
do
  printf("Enter any number to continue, 0 to exit:"); 
  scanf("%d", &input);
  printf("Value entered is %d\n", input); 
}while(input != 0 );

On each iteration, the program will ask the user to enter any number and 0 if the user want to end the program. When user types a number and press enter the value first stored in the input variable, then the entered value is displayed on the screen.

Before go to next iteration, the program check the input value. if the input value is not 0, the loop is revisited. The program again ask for input. This loop will continues on until user enters zero. One possible output is:

Enter any number to continue, 0 to exit: 3
Value entered is 3 
Enter any number to continue, 0 to exit: 10 
Value entered is 10
Enter any number to continue, 0 to exit: 0

Both while and do...while loop is more suitable for situation where we don't know how many times the program need to loop. Whereas, for loop we know number of loops beforehand.

Click here to go back to part 2

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

Setting up data set and display it