0% found this document useful (0 votes)
27 views27 pages

Control Structure

The program uses if, if-else, and if-else if statements to check multiple conditions: it checks if a student's marks are greater than or equal to 40 to pass or fails, uses if-else if to determine a student's grade based on marks ranges from A to E, and implements a switch case statement
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views27 pages

Control Structure

The program uses if, if-else, and if-else if statements to check multiple conditions: it checks if a student's marks are greater than or equal to 40 to pass or fails, uses if-else if to determine a student's grade based on marks ranges from A to E, and implements a switch case statement
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

What is Control structure?

Control structure determines the order in which statements are executed. It


control flow is the order in which individual statements, instructions or function
calls of an imperative program are executed or evaluated. They make it possible
to make decisions, to perform tasks repeatedly or to jump from one section of
code to another.

The three basic control structures include the following:

1. Sequence— default mode. Successive execution of code statements (one line


after another)

2. Decision (Conditional)—used for branching or choosing between 2 or more


alternative paths.

3. Loop (Iterative)—used for repeating a piece of code multiple times in a row.


Decision or Selection structures

It selects a statement to execute on the basis of condition.


Statement is executed when the condition is true and ignored
when it is false. There are two decision structures used in the C
language i.e. if statement and switch case statement
if statement
The if statement consists of a Boolean expression followed by one
or more statements.
The if statement evaluates the test (condition) expression inside
the parenthesis ().
•If the test expression evaluates to true, then the statements
inside the body of if are executed.
•If it evaluates to false, then the statements inside the body of if
are not executed i.e. the statements are skipped
Syntax

The syntax of an 'if' statement in C programming language is −


if(condition)
{
/* statement(s) will execute if the boolean expression is true */
}
if else statement:

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.

Each case in a block of a switch has a different name/number which is


referred to as an identifier. The value provided by the user is compared with
all the cases inside the switch block until the match is found.

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

Write a C program to input week number(1-7) and print day of


week name using switch case. C program to find week day name
using switch case. How to find day name of week using switch
case in C programming.
//C program to print day of week using switch case
#include <stdio.h>
int main()
{
int week; /* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week); switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}
1. Write a program to find the largest number of the three entered by the user

2. Write a C program to input marks of three subjects; Physics, Chemistry, and


Computer. Calculate and display Total, average and grade based on average
according to following rule:
Average Grade
80 below 100 : A
70 below 80 : B
60 below 70 : C
50 below 60 : D
40 below 50 : E
below 40 : F

You might also like