C programs are executed in a sequence, but we can control the execution of program by using any control mechanism by which we can compare things and come to a decision. This involves using some operations called Relational Operators and conditional statements called if-else and loops. We use fundamental operators to compare two values in our C programs.

These relational operators compare two values and return true or false after comparison. We can use the relational operators to compare values of any basic data type, so all we need is to modify the behavior of the program.

“The relational operators compare two values of any basic data type and return true or false after comparison.”

In C, there is another operator called ternary operator which is an expression instead of a statement i.e. you can have it on the right-hand side (RHS) of a statement. So you can write certain code statements more concisely.

The rest of the article is divided into the following main sections:

  1. The Decision Control Structure in C
  2. The Loop Control Structure in C
  3. The Case Control Structure in C

Decision Control Structure in C

The decision control structure in C can be implemented using the following statement types.

  1. The if statement
  2. The if – else statement
  3. The conditional operators

The if Statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true.

If the condition is false then a group of statements can be executed using else statement

The simple example of an if statement is:

We can also use the code block to specify the statements to be pre-executed if the given condition is true i.e.

The general form of if statement looks like this:

Here the keyword if tells the compiler that what follows, is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C’s relational operators. The relational operators help us to build expression which are either true or false

This expression is true if

Demonstration of if statement in C

Multiple statements within an if statement

If it is desired that more than one statement is to be executed if the condition is satisfied, then such statements must be placed within pair of braces. for e.g.

The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee. The program illustrates the multiple statements used within if

The if-else statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true. it does nothing when the condition is false. It the condition is false then a group of statements can be executed using else statement. The following program illustrates this

Nested if – else statement

It we write an entire if – else construct within the body of the if statement or the body of an else statement. This is called ?nesting? of if. For e.g.

What are the examples of Conditional Operators?

  1. < Less than
  2. > Greater than
  3. == Equal to
  4. <= Less than or equal to
  5. >= Greater than or equal to
  6. != Not equal to

The Loop Control Structure in C

These are three methods by way of 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

The for Loop

The for loop is started with the keyword  for. There are three expressions which appear with in a for loop.

  1. Initializing Expression
  2. Test Expression
  3. Increment Expression

It has the following standard form.

The general form of for statement is as under:

Here the  initialise  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_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

The general from of while is as shown below:

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 would be….

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

The 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: