Control Statements in C Programming

Control statements in C: While solving computational problems we come over conditions where we need to deal with the situations that have two possibilities like a thing would happen if other doesn’t happen. Sometimes we may have to keep the program executing repeatedly unless a certain point is being reached. To overcome these situations we need control statements (statements that control the above two conditions). Control statements are of two types: branching and looping.

Branching and loops
Controls

Branching

It is to execute one of the several possible options depending on the outcome of a logical test, which is carried at some particular point within a time. For example, for a multiple choice question with four choices, the one who solves the problem will be given these four possible answers and his task are to choose the right one after analyzing the question. This kind of conditions involves branching.

There are several types of branching. To name them:

If else statement

It is used to carry out one of the two possible actions depending on the outcome of a logical test. The else part is optional depending on the nature of the problem.

It has the following syntaxes:

If(Boolean expression)
{
//if there is no else part
Statements
}
If(Boolean expression)
{
Statements;
}
Else
{
Statements;
}

Note: Refer example 2 to see how if…else is being used.

Nested if statement

This is if…else statements that contain another if statement(s) in the if section or the else section.
A sample syntax:

if(Boolean expression){
statement(s);
if(Boolean expression){
statement(s);
}
else{
Statement(s);
}
}
else{
statement(s);
}

Ladder if statement

In order to create a situation in which one of the several courses of action is executed we use the ladder – if statements.
It has the following syntax:

if(Boolean expression) statement1
else if(Boolean expression) statement2
else if(Boolean expression) statement3
…
else statement

Example 5: Write a C program that asks the user to enter marks for three subjects (each for out of 100). Find the average. Print “Excellent” if it is more than 70%, print “Good” if it is between 60 and 70%, print “Pass” if is between 50 and 60, and print “Failed” if it is less than 50%.
Sample Solution:


#include <stdio.h>

//result.c by bhutanio.com

void main()

{

int i, j, k;

printf("Enter 3 marks");

scanf("%d", &i);

scanf("%d", &j);

scanf("%d", &k);

float avg = (i+j+k)/3;

if (avg>=70)

printf("Excellent");

else if(avg >=60)

printf("Good");

else if(avg >=50)

printf("Pass");

else if (avg <50)

printf("Failled");

}

Switch Statement

It is used to execute a particular group of statements to be selected from several other available options. The selection is done on the current value of an expression with the switch statement.
It has the following syntax:

switch(expression){
case value1:
statement1;
break;
case value2:
statement2;
break;
…
case valueN:
statementN;
break;
default:
statement;
}

All the cases exist within the boundary of the braces { } the switch. Each case has its own distinct value of expression followed by a colon ‘:’. Each case ends with a break statement. Refer my notes to jump statements to understand what break statement does.

After the end of the cases, there should be a default block. This block gets executed if none of the cases get executed. These case blocks will not get executed when the expression of value does not match any of the values corresponding to each case.

Looping

The repetition of steps in a program is called loop. A loop is used using a condition. The repetition of the loop continues until the condition becomes true. Looping is also referred to as iteration. The number of loops/repetitions is determined by the condition set.

Basically, a loop in a program can be segmented into two:
1. The body of the loop: Determines what each loop/repetition would perform.

2. Control statement: Determines the number of loops.

Types of Looping Statements

There are two types of looping statements. These two variations can be distinguished by looking at the position of the control statement or condition checking mode (which can be either positioning the control statement before the loop or after the loop).

Thus, these two types can be named as:

1. Entry controlled loop

This kind of loop is also called pre-test loop.
while and for loops are entry controlled loops.

2. Exit controlled loop

This kind of loop is also called post-test loop.
Do-while loops are exit controlled loops.

Loops
Loops

There are three repetition structures in C programming language. These are, for, while and do while loops. These repetition structures allow the programmer to repeat a certain block of codes until a certain condition becomes true. Let us discuss each of them one by one.

For Loops

Let us see the syntax first:


for(initialization; condition; update)
{
block of codes to iterate;
}

At first, initialization is executed. It happens only for once.

Then the condition is being tested. If it is true, the conditional test is being executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement.

After the body of the ‘for’ loop executes, the update (increment/decrement) statement gets executed (only if the condition remains true).

The iteration continues until the condition becomes false and the execution of the loop ends.

Example 6: Write a C program to print the multiplication table of 3 using for loop.
Sample code:


#include <stdio.h>

//table3.c by bhutanio.com

void main()

{

int n = 3;

int i;

for(i=1; i<=12; i++){

int pro = i*n;

printf("\n 3 X %d = %d", i, pro);

}

}

While Loops

It has the following syntax:
while(expression)
{
Statement(s);
}

While loops get executed as long as the given expression is true.

Example 7: Write a program to print the numbers 1 to 10 using while loop.
Sample Code:


#include <stdio.h>

//looped1to10.c by bhutanio.com

void main(){

int i =1;

while (i<=10){

printf("%d\n", i);

i++;

}

}

Do…while Loops

It also performs execution as long as the conditions remain true like while loops.
Even if the condition is not true at the first iteration, it is certain that the loop would get executed at least once.
It has the following syntax:
do{
Statement(s);
}
while(expression)

Example 8: Write a program to print the numbers 1 to 10 using do…while loop.
Sample Code:


#include <stdio.h>

//onetoten.c by bhutanio.com

void main(){

int i = 1;

do{

printf("%d\n", i);

i++;

}

while(i<=10);

}

In this section, we covered a wide range of concepts including loops and branching in control statements. To understand these concepts the previous chapters too are important. If you have not checked this out, here are the links to the previous chapters.

  1. Chapter 1: Introduction to C Programming
  2. Chapter 2: Setting Up Environment
  3. Chapter 3: C Programming Basics

In the next chapter, we will be seeing another very interesting topic, that is on functions. Check this out here:

Chapter 5: Functions

%d bloggers like this: