Control Statements and Loops


In the previous sections a variety of variables, data types and operators have been discussed, however this knowledge is not sufficient to write a program that has a meaningful purpose. To accomplish that, a combination of control statements is needed, which allow to perform various tasks depending on the given conditions. For this purpose conditional statements such as “switch” ,“while”, “do while”, “go to” and “if else” are used. What is more, there are also “loops”, which allow a multiple execution of certain instructions. In C control statements are divided into two main groups:
1. Conditional statements
2. Loops

Conditional Statements


If else conditional statement looks as follows:

if (condition1)
{
instructions;
}
else if (condition2)
{
instructions;
}
else
{
instructions;
}

If the condition1 is met, the first set of instructions is executed. However, if the condition1 is not met, condition2 is checked. If it is met, another set of instructions placed in the curly brackets below “else if” statement is executed. If no condition is met, the last set of instructions placed below “else” statement is executed.

Switch statement allows a selection from multiple choices based on fixed values given for a certain expression.
Switch statement syntax:

switch ( expression)
{
case value_1:
instruction_1;
break;
case value_2:
instruction_2;
break;
case value_n:
instruction_n;
break;
default:
instruction_def;
break;
}
The switch statement body consists of a series of case labels and an optional default label. No two constant expressions in case statements can evaluate to the same value. The default label can appear only once. The expression placed next to switch statement looks for case constant expression matching and executes its instructions, if no case is found, the default instructions are executed.

Goto is a one-way transfer of control to another line of code in the program. The locations where the goto statement jumps to is identified by using labels or in some languages by line numbers. Labels can be defined anywhere in the function above or below the goto statement.
An example of the syntax of goto control statement looks as follows:

int function1{
int i=1;
jumppoint:
printf(“%d”, i);
i++;
if ( i <= 10 )
goto jumppoint;
return 0;
}
The above function prints the numbers from 1 to 10 in the terminal and then returns 0.

Loops


While statement’s general form is:
while(condition)
{
instructions;
}
The condition is evaluated and if it appears to be true, the body of the while statement is executed. This process of repeated execution of the body continues until the test condition finally becomes false.

Do while statement’s general form looks as follows:
do{
instructions;
}while(condition);
Do while statement creates a structured loop, which is executed until the condition is true at the end of each pass through the loop.

For loop is the conditional statement that allows the code to be executed multiple times.
The general syntax of a for loop looks as follows:
for (initialization; condition; increment )
{
instructions;
}
When the program enters for loop, the variable used is initialized to the value specified as a starting value example i=0. The value initialized is then checked if it meets the for loop condition. Usually the condition is a relational condition such as i< 5. If the condition is met, the body of the loop is executed and the control variable is incremented. The new value of the control variable is checked if it meets the for loop conditional statement. The process is repeated until the condition is no longer true.

Other control statements


Break statement immediately terminates the execution of a loop even if the condition for its end is not fulfilled.
Example:
for( i = 0; i < 100; i++)
{
if( key[ i ] == rightkey)
{
printf(“the key was found”);
break;
}
}
In the example above, the break statement is executed once the key matches the rightkey. There is no need to execute the loop repeatedly after finding the requested value.

Continue statement causes the program to skip the remaining part of the loop in the current iteration as if the end of the statement block had been reached. It causes it to jump to the start of the following iteration.
Example:
for( i = 0; i < 20 ; i++)
{
if( i != 19)
continue;
printf(“it is the final iteration”);
}
In the example above all of the iterations will be skipped, until the final 19th iteration is executed. Then the text is printed in the terminal.