1 Introduction to C

1.5 Flow controls

Flow controls refer to the controls of orders of statements/instructions of programs to be executed. Flow controls determine the next statement (in source code program) or instruction (in executable program) to be executed.

C provides the three basic flow control structures:

  • sequence
  • selection
  • repetition

1.5.1 Sequence

The sequence control executes program instructions in the linear order of their appearances in the program. Sequence control is the default program flow control in programs. That is, a sequence of statements in source code programs will be executed one after another in linear order by sequence control.

Example:

int a = 10;
int b = 20;
int c = a + b;

When executing the above program fragment, it will first write value 10 to variable a, then write 20 to b, then compute a+b and write result of a+b 10 to c. 

1.5.2 Selection

A selection control (also called decision or branching) is to alter the default flow of sequential order of instructions, enabling to jump from one instruction to another depending on a condition.

C provides four decision control statements:

1. if statement

if (condition)
   block

The condition is given by a Boolean or a logic expression. When it is true the block of statements will be executed; otherwise the block will be skipped.

Example:
float a = 3.41415;
if (a > 0.0) {
  printf("number %f is positive",a);
}

Note that the block scope braces {} can be eliminated if there is only one statement in the block. The above program example can be written as:

float a = 3.41415;
if (a > 0.0) 
  printf("number %f is positive",  a);

Extra spaces, new lines, tabs can also be added for readability, and eliminated for compressing. The above program can also be written as:

float a=3.41415;if(a>=0.0)printf("the number %f is positive",a);

2. if-else statement

if (condition) 
  block1 
else 
  block2
Example:
if (mark >= 50.0)
  printf("pass");
else
  printf("fail");

C provides a conditional ternary operator (?:) for a simple if-else expression.

The syntax of the conditional operator is: condition? expression1 : expression2

It means that if condition is true, use expression1; else use expression2.

Examples:

int a = 1>2 ? 10 : 20; // This assigns 20 to a.
int b = 1<2 ? 10 : 20; // This assigns 10 to b. 

3. if-else-if statement

if (condition1) 
  block1; 
else if (condition2) 
  block2;
else 
  block3;

It allows to have multiple else-if statements, and to remove the else statement.

Example:

if (mark >= 90.0)  
  printf("A+");
else if (marks >= 80.0)
  printf("A");
else if (mark >= 70.0)
  printf("B");
else if (mark >= 60.0)
  printf("C");
 else if (mark >= 50.0)
  printf("D");
 else
   printf("F");

4. switch statement

C provides a switch statement to select blocks based on integer or char values.

Example:

switch( grade ) {
  case 'A' : 
    printf( "Excellent" );
    break;         
  case 'B':
    printf( "Good" );             
    break;
  case 'C' :  
    printf( "OK" );           
    break;
   default: 
    printf( "What is your grade?" ); 
}

For a given value of grade, it checks the first case, namely evaluates grade == 'A'. If the evaluation is true, it prints “Excellent”. If it is not true, it checks the next case, and so on. The break; statement means to skip the rest statements of the switch block. Without the break statement, it will move to the next case. For example, given char grade = 'B';, it prints Good, and then leaves the switch statement.

1.5.3 Repetition

A repetition control (also called loop) statement sets to execute a block of statements over and over again until a certain condition is satisfied. Each cycle of executing the block of statements is called an iteration. C provides three repetition statements.

1. for loop

Syntax:

for (a list of initial statements; a list of conditional statements; a list of next-iteration statements)  
  repetition-block

The for loop statement controls the program flow by the following steps.

Step 1. Start from executing the list of initial statements. 
Step 2. Execute the list of conditional statements.   
        If one of them gives true, i.e., none-zero, then goto step 3  
        else goto step 5.
Step 3. Execute the repetition-block.
Step 4. Execute the list of next-iteration statements, goto step 2.
Step 5. Leave the for loop, i.e., move to the next statement after the loop block.   

Example:

int count;
for (count = 1; count < 3; count++)
  printf("%d ", count);  

The output of the above code segment is: 1 2

It runs step-by-step as follows.

count = 1
count < 3 gives 1
print count value 1   

count = count+1 = 1+1 = 2
count < 3 gives 1
print count value 2

count = count+1 = 2+1 = 3 
count < 3 gives 0
leave the for loop. 

2. while loop

Syntax:

while (list of conditional statements)  
  repetition-block

The while loop controls the program flow by the following steps.

Step 1. Execute the list of conditional statements,   
        if one of them values true, then goto step 2 
        else goto step 3.
Step 2. Execute the repetition-block, goto step 1.
Step 3. Leave the while loop. 

Example:

int count = 1;
while (count < 3) {
  printf("%d ", count);
  count++;
}

The output of the above program is: 1 2

It runs step-by-step as follows.

count < 3 gives 1
print count value 1
count = count+1 = 1+1 = 2

count < 3 gives 1
print count value 2
count = count+1 = 2+1 = 3 

count < 3 gives 0
leave the while loop. 

3. do-while loop

Syntax:

do {
  repetition-block
} while (list of conditional statements);

Example:

int count = 1;
do {
  printf("%d ", count);
  count++;
} while (count < 10);

The output of the above program is: 1 2

It runs step-by-step as follows.

print count value 1
count = count+1 = 1+1 = 2

count < 3 gives 1
print count value 2
count = count+1 = 2+1 = 3 

count < 3 gives 0
leave the do-while loop. 

The difference between while and do-while loops is that a while loop statement executes the conditions first, if a true value is derived, it executes the loop block, and so on. A do-while statement executes the loop block fist, then checks the conditions. If a true value is derived, then it executes the loop block again, and so on.

1.5.4 Other control statements

C also provides additional control statements break, continue, and a more flexible control statement goto.

1. break statement

The break statement break; can be used in a repetition-block for jumping out of a loop. When the compiler encounters a break statement, it adds flow control to transfer to the statement that follows the loop statement.

Example:

int count; 
for(count = 1; ; count++) { 
   if (count >= 3) break;
   printf("%d ", count); 
}

The output of the program is: 1 2

It leaves the loop statement when count >=3. Without statement if (count >= 3) break;, the above program runs into an infinite loop.

2. Continue statement

The statement continue; can be used in a repetition-block to skip the rest of statements of the repetition-block. If it is placed within a for-loop, the continue statement causes a jump to the next-iteration statements. If it is in a while-loop or do-while loop, it jumps to the loop conditional statements.

Example:

/* output the integers between 10 and 20, not divisible by 3*/
#include <stdio.h>
int main(void) {
  int n;
  for (n = 10; n <= 20;  n++)
  {
    if (n%3==0)
      continue;
    printf("%d ", n);
  }
  return 0;
}

The above program will print the integers between 10 and 20, which are not divided by 3. That is, it skips the printing n dividable by 3.

3. goto statement

The goto statement transfers flow control to a labeled position within a function scope.

A label is an identifier that specifies the place where the branch is to be made. It can be any valid variable name that is followed by a colon (:). Labels can be placed anywhere in the program either before or after the goto statement within its block. Whenever the goto statement is encountered, the control is immediately transferred to the statements following the label.

Example:

#include <stdio.h>
int main () {
   int i = 0;
   LOOP:
   printf("%d ", i);
   i++;
   if (i < 10) 
     goto LOOP;
   return 0;
}

The goto statement is a flexible and powerful flow control statement provided by C. goto statement can be used to do selection and repetition controls. The above program is an example of using goto statement for repetition controls.

1.5.5 Exercises

Self-quiz

Take a moment to review what you have read above. When you feel ready, take this self-quiz which does not count towards your grade but will help you to gauge your learning. Answer the questions posed below.

Go back