0% found this document useful (0 votes)
6 views

Control Structure

Uploaded by

bikashbogati001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Control Structure

Uploaded by

bikashbogati001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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)

Decisions ((selective control structure.): -


Here selection is made on the basis of condition. We have options to go when the given condition is true or false.
The flow of program statement is totally directed by the result obtained from checking condition.
It is mainly categorized into two types
 Conditional statement.
 Switch-case statement.

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.

Program to find the greatest number between any two number.


#include<stdio.h>
Void main()
{
Int a,b;
printf(“Enter two number = “);
scanf(“%d%d”,&a,&b);
If(a>b)
printf(“\n%d is larger than %d”,a,b);
else
printf(“\n%d is larger than %d”,b,a);
}

Q. Program to check whether the entered number is even or odd.


#include<stdio.h>
void main()
{
Int n;
printf(“ Enter any number = “);
scanf(“%d”,&n);
if(n%2==0)
printf(“Entered number is even”);
else
printf(“Entered number is odd = );
}

Q. program to find whether entered number is divisible by 5 or not


#include<stdio.h>
void main()
{
Int n;
printf(“ Enter any number = “);
scanf(“%d”,&n);
if(n%5==0)
printf(“Entered number is divisible by 5”);
else
printf(“Entered number is not divisible by 5 );
}
Q. write a program to input a number and display it if it is exactly divisible by 5 but not by 11.
include<stdio.h>
void main()
{
Int n;
printf(“ Enter any number = “);
scanf(“%d”,&n);
if(n%5==0 && n%3==0)
printf(“Entered number is divisible by 3 and 5”);
else
printf(“Entered number is not divisible by 3 and 5 );
}

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");
}

Nested if else statement:


An entire if else statement written within the body of if part or else part of another if else statement is called
nested if else statement.
Syntax Flowchart

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");
}
}

You might also like