In C programming, controlling the flow of the program is the key to unleashing the true potential of your code. In this article, we will look into the loops and control statements which are fundamental constructs and shape the flow of execution in your programs.

These are multiple ways in which we can repeat a part of a program. They are:

  1. The for Loop
  2. The while Loop
  3. The do-while Loop
  4. The break Statement
  5. The continue Statement

Table of Contents

The for Loop

A for loop is a control flow statement that allows you to repeatedly execute a block of code a specified number of times. It is useful when you know in advance how many times you want to iterate. The for loop consists of three components: initialization, condition, and increment/decrement. It has the following standard form.

Here the initialization step is executed first, and only once. Then condition is evaluates to see if it’s true or false. If true, the body of the loop is executed otherwise body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After each time the body of the for loop executes, the increment/decrement counter value is increased/decreased based on the increment counter.

Now let us write a simple interest problem using the for loop

When the above code is compiled and executed the result is as below:

The while Loop

A while loop is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition is true. Unlike the for loop, the while loop only consists of a condition, and the loop continues as long as the condition remains true.

The parentheses after the while contains a condition so long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly for e.g.

When the above code is compiled and executed the result is as below:

The do-while Loop

The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the do-while statement. So do-while loop is always executed at least once. Its general form is

There is a minor difference between the working of while and do-while loops. The difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this the do-while tests the condition after having executed the statements within the loop. for e.g.

In the above example the printf() will not get executed at all since the condition fails at the first time itself. Now let’s write the same program using a do-while loop.

In the above program the printf() would be executed once, since first the body of the loop us executed and then the condition is tested.

The break Statement in C

The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any loop in C, control automatically passes to the first statement after the loop. For example the following program is to determine whether a number is prime or not.

To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. It the remainder of any of the divisions is zero, the number is not a prime. Following program implements this logic:

The continue Statement

The keyword continue allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop. For example:

The output of the above C program is:

when the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statements pending execution in the for loop (inner).

The Case Control Structure: switch Statement

The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression that is included within the switch statement.

The form of switch statement is.

Here are some of the rules which apply on switch statement in C language.

  1. The expression used in a switch statement should have an integral or enumerated type.
  2. There can be multiple case statement with in switch statement where case is followed by the value to be compared to and a colon.
  3. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  4. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  5. If there is no break statement appears within a case statement, the flow of the program continues until  break statement is is reached or the switch statement end block reaches.
  6. A switch statement can have an optional default that can be used to execute some statements when no case statements are met before. As default is the last statement in switch so no break; statement is required to terminate this.

If you want to further understand conditional statements,  then study the following C Programs: