Control Statements

Control Statements in C

The statements which alter the flow of execution of a program are known as control statements. Discover the essentials of control statements in C programming. Learn how decision-making, looping, and branching structures help control the flow of a C program.

There are two types of control statements: Decision Making & Repeating construct

 Decision-Making Statements 

 

Loop Statements or Repeating construct

 

if statements for loop
if.. else statements while loop
if.. else  if  statements do.. while loop
Nested if.. else statement  
switch statement  

 Decision-Making Statements

i.  if  Statements

An if statement is used to control the flow of statement execution. The if statement first evaluates an expression, and then, if the value of the evaluated expression is true, it executes the statements within its block.

 

if(test_ expression)
{
statements-block;
}

Example 1

Write a program to read a number from the user and test whether the number is negative [Show message “The number is negative ” if it is a negative number otherwise show nothing].

#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf(“Enter a number to be tested:”);
scanf(“%d”, &num);
if(num<0)
printf(“The number is negative”);
getch();
return 0;
}
Output
Enter a number to be tested:-6
The number is negative

 Nested if  Statement

When one if statement is written within the body of another if statement, it is called a nested if statement. Several if statements shall be declared as nested if statements.

         Syntax

if(expression)
{
if(expression)
{
//body
}
}

 Example

Write a program to read the percentage of marks obtained by a student in SLC and +2 levels, Display the message “Congratulation!! You have a first division in both SLC and +2” if both levels have a percentage greater than or equal to 60.

#include<Stdio.h>
#include<conio.h>
int main()
{
float SLC_ per, plus2_ per; ;
print f(“Enter percentage of SLC:”);
scan f(“%f”, &SLC_ per)
print f(“Enter percentage in +2 in science:”);
scan f(“%f”, &plus2_per);
if(SLC_ per>=60)
{
if(plus2_per>=60)
{
print f(“Congratulation!!”);
print f(“\n You have first division in both SLC and +2.”);
}
}
getch () ;
return 0;
}
Output
Enter the percentage of SLC: 70
Enter percentage in +2 in science: 60
Congratulation!!!!!
You have the first division in both SLC and +2.

if…. else Statement

The if.. else statement is an extension of the simple if statement. It is used when there are only two possible actions one happens when a test condition is true, and the other when it is false.

if(test_ expression)
{
true-block statement (s);
}
else
{
false-block statement (s);
}

Example

Write a program to read a number from the user and determine whether the number is even or odd.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, remainder;
print f(“Enter a number:”);
scan f(“%d”, &num);
remainder = num % 2;
// Modulo division
if (remainder == 0)
{
print f(“The number is even”);
}
else
{
print f(“The number is odd”);
}
getch();
return 0;
}
Output
Enter a number: 90
The number is: even
Enter a number: 17
The number is odd

 

Nested if.. else Statement

Similar to nested if statements if.. else statements shall also be written inside the body of another if.. else body called nested if.. else statement.

if(condition)
{
if(condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(condition3)
{
statement-3;
}
else
{
statement-4;
}
}

Example

Write a program to read three numbers from the user and determine the largest number among them.

int main()
int n1, n2, n3;
print f(“Enter 3 numbers:”);
scan f (“%d%d%d”, &n1 , &n2 , &n3);
if (n1>n2)
{
if(n1>n3)
print f(“Largest = %d , n1);
else
print f(“Largest = %d“, n3);
}
else
{
if (n2>n3)
print f(“Largest = %d , n2);
else
print f(“Largest = %d, n3);
}
getch();
return 0;
}
Output
Enter 3 numbers: 100 50 10
Largest = 100
Enter 3 numbers: 50 100 10100
Largest = 100

 

if…. else if Statement

An if… else if statement is used when there are more than two possible actions depending upon the outcome of the test expression. When an action is taken, no others can be executed or taken. In such a situation, structure is used if…. else if… else if.. else.

if(condition)
{
statement-1;
}
else if(condition2)
{
statement-2;
}
else if(condition3)
{
statement-3;
}
else if(condition n)
{
statement-n;
}
else
{
default-statement;
}
statement-x;

Example

Write a program that reads the total marks of a student in seven subjects. Then calculate the percentage and determine the division. Use the following conditions:

  1. Percentage greater than or equal to 80- distinction,
  2. Percentage between 60 and 79- First Division
  3. Percentage between 45 and 59-second division
  4. Percentage between 32 and 44  – third division
  5. Percentage less than or equal to 31 -fail.
#include<stdio.h>
#include<conio.h>
int main()
{
float nep, eng, math, phy, chem, bio, comp, percent;
print f(“Enter the marks in 7 subjects:”);
scan f(“%f%f%f%f%f%f%f”, &nep, &eng, &math, &phy, &chem, &bio, &comp);
percent =(nep+ eng + math +phy + chem + bio + comp) ;
if(percent>= 80)
print f(“Distinction”);
else if(percent >=60 && percent < 80)
print f (“First Division”);
else if (percent>=45 && percent <60)
print f(“Second Division”)
else if (percent >=32 && percent <45)
print f(“Third Division”);
else
print f(“Fail”);
print f(“\n Your percentage is: %f”, percent);
getch();
return 0;
}
Output:
i. Enter the marks in 7 subjects: 45 78 89 35 76 80 35
First Division
Your percentage is: 62.5714286
ii. Enter the marks in 7 subjects: 20 10 32 25 24 35 40
Fail
Your percentage is: 26.5714286

 

Loop or Iteration or Repeating Construct

Types of Loop

  1. for loop
  2. while loop
  3. do-while loop

i. for Loop

for(counter_ initialization; test_ condition; increment or decrement)
{
statements; or body of the loop
}

Example

Write a program to calculate the factorial of a number.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, i;
long fact=1;
print f(“\n Enter a number whose factorial is to be calculated:”);
scan f(“%d”, &num);
for(i=1;i<=num;i++)
fact*= i;
print f(“\n The factorial is :%d”, fact);
getch();
return 0;
}
Output
Enter a number whose factorial is to be calculated: 5
The factorial is: 120

 

While Loop

While(test_ condition)
{
body of loop
}

Example

Write a program to calculate the factorial of a number using a while loop.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, i=1;
long fact = 1;
print f(“Enter a number whose factorial is to be calculated:”);
scan f(“%d”, &num);
while(i<=num)
{
fact*=i;
i++;
}
print f(“The factorial is:%d”, fact);
getch();
return 0;
}

 

do-while Loop

Do
{
//statements or body of loop;
}while(test_condition);

Example

Write a program to read a number from a keyword until a zero or a negative number is keyed in. Finally, calculate the sum and average of the entered numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int num, count=0;
float sum=0, avg;
do
{
print f(“\n Enter number:\t”);
scan f(“%d”, &num);
sum+=num;
count++;
}while(num>0);
sum=sum-num;
avg=(sum)/(count-1)
print f(“\n The sum is :\t%d”, sum);
print f(“\n The average is: \t%f”, avg);
getch();
return 0;
}
Output
Enter a number: 10
Enter number: 20
Enter number: 15
Enter number: 5
Enter a number: 0
The sum is: 50.000000
The average is: 12.500000

 

break Statement

The break statement terminates the execution of the loop and the control is transferred to the statement immediately following the loop.

break;

Example

What is the output of the following program?

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1; i<10; i++)
{
print f(“\t%d, i);
if (i==5)
break;
}
getch();
}
Output
1 2 3 4 5

 

continue Statement

The continue statement is used when we want to continue running the loop.

continue;

Example

Write a program that asks for a number n from the user and then displays only even numbers from 2 to n.

#include<stdio.h>
#include<conio.h>
int main()
{
int i, num;
print f(“\n Enter a number:”);
scan f(“%d”, &num);
print f(“\n The even numbers from 2 to %d are: \n”, num);
for (i=1; i<= num; i++)
{
if(i%2!=0)
continue;
print f(“\t%d”, i);
}
getch();
return 0;
}
Output
Enter a number: 20
The even numbers from 2 to 20 are:
2 4 6 8 10 12 14 16 18 20

 

goto STATEMENT

The goto statement is used to after the normal sequence of program execution by unconditionally transferring control to some part of the program.

goto label;
label: statements;

Example

Write a program to ask for two numbers. Display the message” Either number is negative ” if either number is negative; otherwise, display the message ” Both numbers are positive”.

int main()
{
int i, num1, num2;
print f(“Enter first number:”);
scan f(“%d”, &num1);
if(num<0)
goto negative;
print f(“Enter second number:”);
scan f(“%d”, &num2);
if(num2<0)
goto negative;
print f(“The both numbers are positive”);
getch();
return;
negative :
print f(“Enter number is negative “);
getch();
return 0;
}
Output
a) Enter first number: 20
Enter the second number: 10
The both numbers are positive
b) Enter first number: -5
Either number is negative
c) Enter first number: 6
Enter the second number: -4
Either number is negative

 

switch STATEMENT

When there are a number of options available and one of them is to be selected on the basis of some criteria, a switch statement is used.

switch(variable or expression)
{
case caseConstant1:
statements;
break;
case caseConstant2:
statements;
break;
default:
statements;
}
Control statements in C are used to control the flow of a program’s execution. They include decision-making statements (if, else, switch), looping statements (for, while, do-while), and branching statements (break, continue, return). These constructs help the program make choices, repeat actions, or skip parts of the code based on conditions, making them key for dynamic and flexible programming.

Leave a Reply

Your email address will not be published. Required fields are marked *