Control Structure
Control Structure
Control structure are those programing constructs which control the flow of program statements execution in a
program. It also specifies the order of statements in program.
Mainly controls structures are classified into the following three categories:
1. Branching (selective control structure.)
2. Looping (repetitive control structure.)
3. Jumping (Unconditional Control Structure)
Conditional statement: -
it is the most common decision-making control structure which controls the flow of program statement execution
based on the condition checked. It can be used in different forms as:
If statement
If else statement
If else if statement
Nested if else statement
If statement: -
It is the simplest form of conditional statement in which statements are executed if the test expression or
condition is true. When condition is false there is no option to go within this structure; in such situation control must get
out from the structure and statements outside this structure will be executed.
Syntax Flowchart
If(condition)
{
Statements;
……………..;
}
Example:
#include<stdio.h>
void main()
{
Int n;
printf(“Enter a number = ”);
scanf(“%d”,&n);
if(n==2)
printf(“\nEntered number is 2 “);
}
If else statement: -
In this control structure, statement written as a part of if are executed if the condition is true otherwise
statements written in the body part of else are executed. This is appropriate where we have to check only one condition.
Syntax Flowchart
If(condition)
{
Statements 1
}
Else
{
Statement 2;
}
In this case if the condition is true then statement
1 is executed otherwise statement 2 is executed.
The else portion is optional.
If else if statement;: -
When we have two or more conditions to be checked in a series we can use if-else if statement. It is also known
as multiple conditional statement/multipath conditional statement.
Syntax Flowchart
If(condition 1)
{
Statements 1;
}
Else if (condition 2)
{
Statement 2;
}
:
:
:
If else (condition n-1)
{
Statement n-1;
}
Else
{
Statement n;
}
Example: - Program to display the greatest number between any three variables.
#include<stdio.h>
void main()
{
Int a,b,c;
printf("Enter three number = ");
scanf("%d,%d,%d",&a,&b,&c);
if(a>b && a>c)
printf("A is greatet");
else if (b>c && b>a)
printf("B is greatest");
else
printf("C is greatest");
}
Example- Program to display entered number is negative positive, negative or zero
#include<stdio.h>
void main()
{
Int a,b,c;
printf("Enter a number = ");
scanf("%d",&a,);
if(a>0)
printf("A is positive Number");
else if (a<0)
printf("A is Negative Number");
else
printf("A is Zero");
}
If(condition 1)
{
If (condition 2)
{
Statement 1;
}
Else
{
Statement 2;
}
}
Else
Statement 3;
Example: Write a program that read marks of 5 different subject and calculate total marks and percentage. Also award
the division on the basis of following criteria.
Percentage Division
p>= 75 Distinction
p>=60 and <75 First division
p>=45 and <60 Second division
p>=35 and <45 Third division
Otherwise failed
Hint: pass mark and full mark of subject are 35 and 100 respectively.
#include<stdio.h>
void main()
{
int eng, nep, com, acc, eco, tm;
float per;
printf("Enter the marks of English =");
scanf("%d",&eng);
printf("Enter the marks of Nepali =");
scanf("%d",&nep);
printf("Enter the marks of Computer =");
scanf("%d",&com);
printf("Enter the marks of Account =");
scanf("%d",&acc);
printf("Enter the marks of Economics =");
scanf("%d",&eco);
tm=eng+nep+com+acc+eco;
per=(float)tm/5;
if(eng>=35 && nep >=35 && com>=35 && acc>=35 &&eco>=35)
{
if (per>=75)
printf("Distinction");
else if(per>=60 && per < 75)
printf("First Division");
else if (per>=45 && per < 60)
printf("Second Division");
else
printf("Third Division");
}
else
printf("Failed");
}
Switch case statement: -
C switch statement is a multipath decision-making statement that allows selection of and execution of a particular
block of statements from several blocks of statements. The switch case statement is used when we have multiple options
and we need to perform a different task for each option.
Syntax Flowchart
switch (expression)
{
case constant 1:
Statements;
Break;
case constant 2:
Statements;
break;
default:
Statements;
}
Write a program which reads any two integer values from user and calculates sum, difference and product using switch
case statement.
#include<stdio.h>
void main()
{
int a,b,c,ch;
printf("Enter the value of A and B = ");
scanf("%d%d",&a,&b);
printf(" \n Operation Menu \n");
printf("\nPress 1 for Addtition"); printf("\nPress 2 for Subtraction");
printf("\nPress 3 for Multiplication ");
printf("\n\nYour choice is = ");
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("\nSum = %d",c);
break;
case 2:
c=a-b;
printf("\nDifference = %d",c);
break;
case 3:
c=a*b;
printf("\nProduct = %d",c);
break;
default:
printf("\nWrong choice");
}
}