Program Flow and control is a control method of your program. For example, Loop constructions control repeated execution of repeated program segments where control is taken by control parameter. In this article, we will go through if/else switch/case statements and loop sentences like while, do while, for.
While statement
As I mentioned, three looping sentences available in C language one of them is While sentence. Let’s take an example:
#include <stdio.h>
int main(void){
int guess, i;
i=1;
guess=5;
while (guess!=i){
i=guess;
guess=(i+(10000/i))/2;
}
printf(“Square root of 10000 is %d\n”, guess);
return 0;
}
While(guess!=i) invokes looping operation. This causes the statement to be executed repeatedly at the beginning the condition guess!=i is checked. As long the argument is TRUE, the while sentence will be continued continuously. When guess becomes equal to i while statement will be skipped. I am not going too deep into it as there are tons of information about basic C.
For Loop
As we didn’t know how many times the loop should be executed then, we used while the sentence, but when we know precisely how many times we need to execute a sentence, there is another loop sentence for(s1;s2;s3). For construct takes three arguments, each separated by semicolons. Without any theories, lets see an example:
for (i = 0; i < 5; i++)
{
printf("value of i");
}
- The for loop has four components; three are given in parentheses, and one in the loop body.
- All three components between the parentheses are optional.
- The initialization part is executed first and only once.
- The condition is evaluated before the loop body is executed. If the condition is false then the loop body is not executed.
- The update part is executed only after the loop body is executed and is generally used for updating the loop variables.
- The absence of a condition is taken as true.
- It is the responsibility of the programmer to make sure the condition is false after certain iterations.
Do While sentence
do while is somehow similar to while just one difference that there testing of conditions is done after execution of first loop:
do{
printf(" the value of i is %d\n", i);
i = i + 1;
} while (i<5)
so
- The loop body is executed at least once.
- The condition is checked after executing the loop body once.
- If the condition is false then the loop is terminated.
- In this example, the last value of i is printed as 5.
If Else Statement
if(expression)
statement1
else
statement2
Lets consider a fragment of program:
while((c=getchar())!=EOF)
if(c>'0'&&c<'9')
n++;
else
n--;
I bet there is no need of explanation. Simply if first part true then first sentence is executed else other sentence is executed.
Other case is with if else if sentence structure:
if (condition 1)
simple or compound statement // s1
else if (condition 2)
simple or compound statement // s2
else if ( condition 3)
simple or compound statement // s3
.....
else if ( conditon n )
Simple or compound statement // sn
Remember that:
- You can use if-else if when you want to check several conditions but still execute one statement.
- When writing an if-else if statement, be careful to associate your else statement to the appropriate if statement.
- You must have parentheses around the condition.
- You must have a semicolon or right brace before the else statement.
Switch statement
switch (expressions)
{
case constant expressions
}
For example:
switch (i/10)
{
case 0: printf ("Number less than 10"); // A
break;
case 1: printf ("Number less than 20"); // B
break;
case 2: printf ("Number less than 30"); // C
break;
default: printf ("Number greater than or equal to 40"); // D
break; }
Remember that:
- The switch expression should be an integer expression, and when evaluated, it must have an integer value.
- The case constant expression must represent a particular integer value, and no two case expressions should have the same value.
- The value of the switch expression is compared with the case constant expression in the order specified, that is, from the top down.
- The execution begins from the case where the switch expression is matched, and it flows downward.
- In the absence of a break statement, all statements that are followed by matched cases are executed. So, if you don’t include a break statement and the number is 5, then all the statements A, B, C, and D are executed.
- If there is no matched case then the default is executed. You can have either zero or one default statement.
- In the case of a nested switch statement, the break statements break the inner switch statement.
- The switch statement is preferable to multiple if statements.