Control Structure
Control Structure
If condition returns true then the statements inside the body of “if”
are executed and the statements inside body of “else” are
skipped.
If condition returns false then the statements inside the body of “if”
are skipped and the statements in “else” are executed.
Syntax
if(condition)
{
// Statements inside body of if
}
else
{
//Statements inside body of else
}
Flow diagram
Example
Example
Write a c program that checks if student result as pass or fail. Assume the
pass mark is 40%
#include <stdio.h>
int main()
{
int Mark;
printf("Enter Per : ");
scanf("%d",&Mark);
if(Mark >= 40)
{
printf("\n Result is pass");
}
else
{
printf("\n Result is fail");
}
return 0;
}
if else..if statement
The if else..if statement is useful when you need to check
multiple conditions within the program.
The program uses a series of if else..if condition checking in
checking for different conditions. Each if else condition is checked
and if the outcome is TRUE then the statements under it are
executed and all other if conditions are omitted or skipped, but if it
is FALSE, then the next condition is checked.
Syntax
Example
Write a program to find the grade of the student based on the
marks entered by the user.
The grading rules are as follows
70% and above -A (Distinction)
60% – Below 70% -B (Credit)
50% – Below 60% -C (Satisfactory)
40% – Below 50% -D (Pass)
Below 40% -E (Fail)
#include<stdio.h> else if(marks<60)
int main() {
{ printf("Grade C");
int marks; }
printf("Enter your marks: "); else if(marks<70)
scanf("%d",&marks); {
if(marks<0 || marks>100) printf("Grade B");
{ }
printf("Wrong Entry"); else if(marks<=100)
} {
else if(marks<40) printf("Grade A");
{ }
printf("Grade E"); else
} { printf("Grade A+");
else if(marks<50) }
{ return 0;
printf("Grade D"); }
}
Switch Case Statement
The switch case statement is used when we have multiple options and we
need to perform a different task for each option.
Switch case tests the value of a variable and compares it with multiple
cases. Once the case match is found, a block of statements associated with
that particular case is executed.
If a case match is NOT found, then the default statement is executed, and
the control goes out of the switch block.
Syntax
A general syntax of how switch-case is implemented as follows:
•The expression can be integer expression or a character expression.
switch(expression)
{ •Value-1, 2, n are case labels which are used to identify each case individually.
case value-1: Remember that case labels should not be same
Block-1;
Break; •Case labels always end with a colon ( : ). Each of these cases is associated with a
case value-2: block.
Block-2;
•A block is nothing but multiple statements which are grouped for a particular case.
Break;
.
•Whenever the switch is executed, the value of test-expression is compared with all the
. cases which we have defined inside the switch.
case value-n:
Block-n; •The break keyword in each case indicates the end of a particular case. If we do not put
Break; the break in each case then even though the specific case is executed, the switch in C
default: will continue to execute all the cases until the end is reached.
Block-1;
} •The default case is an optional one. Whenever the value of test-expression is not
matched with any of the cases inside the switch, then the default will be executed.
Example
Program to create a simple calculator
case ‘-‘:
#include <stdio.h> printf(“%d-%d=%d”,num1,num2,num1-
void main() num2);
{ break;
char operator; case ‘*’:
int num1,num2; printf(“%d*%d=
printf(“\n Enter the operator (+, -, %d”,num1,num2,num1*num2);
*, /):”); break;
scanf(“%c”,&operator); case ‘/’:
printf(“\n Enter the Two numbers:”); printf(“%d / %d =
scanf(“%d%d”,&num1,&num2); %d”,num1,num2,num1/num2);
switch (operator) break;
{ default:
case ‘+’: printf(“\n Enter the operator only”);
printf(“%d+%d= break;
%d”,num1,num2,num1+num2); }
break; }
Example