Module 2 - Control Structures c programming.pptm.pdf

SudipDebnath20 17 views 35 slides May 27, 2024
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

c programming


Slide Content

PRPEARED BY: PROF. NEHA HARDE ©
C PROGRAMMING
Module 2
Control Structures
Course Outcome (CO2):
Demonstrate the use of control structures.

PRPEARED BY: PROF. NEHA HARDE ©
WHAT IS CONTROL STRUCTURE ?
A statement that is used to control the flow of execution in a program is
called control structure
It combines instruction into logical unit
Logical unit has one entry point and one exit point
There are three types of control structures available in C:
1) Sequence structure (straight line paths)
2) Selection structure (one or many branches)
3)Loop structure (repetition of a set of activities)

PRPEARED BY: PROF. NEHA HARDE ©
SEQUENCE STRUCTURE
In Sequence structure instructions are executed in the order they are written following the sequential flow

PRPEARED BY: PROF. NEHA HARDE ©
SELECTION STRCTURE
Selection structures are used to perform
‘decision making‘ and then branch the program
flow based on the outcome of decision making
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

PRPEARED BY: PROF. NEHA HARDE ©
LOOP STRUCTURE
A loop structure is used to execute a certain set
of actions for a predefined number of times or
until a particular condition is satisfied

PRPEARED BY: PROF. NEHA HARDE ©
If statement
If else statement
Nested if else
Else if ladder
Switch Case Statement
If else vs switch case
Decision Control Structures

PRPEARED BY: PROF. NEHA HARDE ©
IF STATEMENT
if statement is used to check a given condition and perform
operations depending upon the correctness of that condition
It is mostly used in the scenario where we need to perform
the different operations for the different conditions
Syntax:
if(expression)
{
//code to be executed
}

PRPEARED BY: PROF. NEHA HARDE ©
IF STATEMENT
#include<stdio.h>
int main()
{
int n;
printf("Enter a number:");
scanf("%d", &n);
if(n%2 == 0)
{
printf("%d is even number“ ,n);
}
return 0;
}
Example: Find whether the
given number is even number

PRPEARED BY: PROF. NEHA HARDE ©
IF ELSE STATEMENT
The if-else statement is used to perform two operations
for a single condition
i.e., one is for the correctness of the condition, and the

other is for the incorrectness of the condition
if and else block cannot be executed simultaneously

PRPEARED BY: PROF. NEHA HARDE ©
IF ELSE STATEMENT
Syntax:
if(expression)
{
//code to be executed if condition is true
}
else
{
//code to be executed if condition is false
}
int main()
{
int n;
printf("Enter a number:");
scanf("%d", &n);
if(n%2 == 0)
{
printf("%d is even
number“ ,n);
}
else
{
printf("%d is odd
number“ ,n);
}
return 0;
}
Example: Find whether the given number is
even number or odd number

PRPEARED BY: PROF. NEHA HARDE ©
NESTED IF ELSE STATEMENT
When an if else statement is present
inside the body of another “if” or
“else” then this is called nested if else
It is used when there are more than one
condition to check at a time

PRPEARED BY: PROF. NEHA HARDE ©
NESTED IF ELSE STATEMENT
Syntax:
if(condition)
{
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else
{
//Statements inside the body of "else"
}

PRPEARED BY: PROF. NEHA HARDE ©
NESTED IF ELSE
STATEMENT
#include<stdio.h>
int main()
{
int age;
char gender;
printf("\n Your gender ? (M/F)");
scanf("%c", &gender);
printf("\n Enter your age:");
scanf("%d", &age);
if(gender == 'M')
{
if(age >= 21)
printf("\n Eligible for marriage");
else
printf("\n Not eligible for Marriage");

}
Example: Find whether the person is eligible for marriage or not based on the age and gender
else
{
if(age>= 18)
printf("\n Eligible for
marriage");
else
printf("\n Not eligible for
Marriage");
}
return 0;
}

PRPEARED BY: PROF. NEHA HARDE ©
ELSE IF LADDER
The if-else-if ladder statement is an extension
to the if-else statement
In if-else-if ladder statement, if a condition is
true then the statements defined in the if
block will be executed, otherwise if some
other condition is true then the statements
defined in the else-if block will be executed,
at the last if none of the condition is true then
the statements defined in the else block will
be executed

PRPEARED BY: PROF. NEHA HARDE ©
ELSE IF LADDER
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
else
{
//code to be executed if all the conditions are false
}

PRPEARED BY: PROF. NEHA HARDE ©
NESTED IF ELSE
STATEMENT
#include<stdio.h>
int main()
{
int age;
printf("\n Enter your age:");
scanf("%d", &age);
if(age >= 60)
printf("\n Vaccination will start from 1
st
Feb 2021");
else if(age >= 45)
printf("\n Vaccination will start from 1
st
March 2021 ");
else if(age >= 18)
printf("\n Vaccination will start from 1
st
May 2021 ");
else
printf("\n Not eligible for Vaccination");
return 0;
}
Example: Find start date of vaccination based on the age of the person

PRPEARED BY: PROF. NEHA HARDE ©
SWITCH CASE
STATEMENT
The switch statement in C is an alternate to if-else-if
ladder statement which allows us to execute multiple
operations for the different possible values of a single
variable called switch variable
Switch statement in C 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:
switch (expression)
​{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}

PRPEARED BY: PROF. NEHA HARDE ©
SWITCH CASE
STATEMENT
Rules for Switch Statement:
The switch expression must be of an integer or character type
The case value must be an integer or character constant
The case value can be used only inside the switch statement
The break statement in switch case is not must.
If there is no break statement found in the case, all the cases
will be executed present after the matched case (fall through
the state of C switch statement)

PRPEARED BY: PROF. NEHA HARDE ©
SWITCH CASE
STATEMENT
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ch, n1, n2;
printf("\n MENU \n 1. addition \n 2. subtraction");
printf(" \n 3. multiplication \n 4. division");
printf(" \n 5. modulus \n 6. exit \n Enter you choice:");

scanf("%d", &ch);
printf("\n Enter two numbers:");
scanf("%d %d", &n1, &n2);
switch(ch)
{
case 1:
printf("\n addition = %d", n1+n2);

break;
case 2:
printf("\n subtraction = %d", n1-
n2);
break;
case 3:
printf("\n multiplication = %d", n1*n2);
break;
case 4:
printf("\n division = %d", n1/n2);
break;
case 5:
printf("\n remainder = %d", n1%n2);
break;
case 6:
exit(0);
default:
printf("\n You have entered wrong
choice!!");
break;
}
return 0;
}
Example: Write a C Program to perform addition, subtraction, multiplication, division and modulus operations using switch case.

PRPEARED BY: PROF. NEHA HARDE ©
IF ELSE VS SWITCH CASE

PRPEARED BY: PROF. NEHA HARDE ©
Looping structure
for loop
do while loop
while loop
break statement
continue statement
Loop Control Structures

PRPEARED BY: PROF. NEHA HARDE ©
LOOPING STRUCTURE

PRPEARED BY: PROF. NEHA HARDE ©
FOR LOOP
For loop specifies the three things:
Setting a loop counter to an initial value
Testing a loop counter to determine whether its value has reached the number of repetitions required
Increasing the value of loop counter each time the program within the loop has been executed
Syntax:
for(initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}

PRPEARED BY: PROF. NEHA HARDE ©
FOR LOOP
Flowchart of For loop

PRPEARED BY: PROF. NEHA HARDE ©
FOR LOOP
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
printf(“%d”, i);
return 0;
}
Example: Print 1 to 10 numbers using for loop

PRPEARED BY: PROF. NEHA HARDE ©
WHILE LOOP
While loop is also known as a pre-tested loop
It can be viewed as a repeating if statement
The while loop is mostly used in the case where the number of iterations is not known in advance
Syntax:
initialization expression;
while (test_expression)
{
// statements

update_expression;
}

PRPEARED BY: PROF. NEHA HARDE ©
WHILE LOOP
Flowchart of while loop

PRPEARED BY: PROF. NEHA HARDE ©
WHILE LOOP
#include<stdio.h>
int main()
{
int i = 1;
while(i <= 10)
{
printf(“%d”, i);
i++;
}
return 0;
}
Example: Print 1 to 10 numbers using while loop

PRPEARED BY: PROF. NEHA HARDE ©
DO WHILE LOOP
A do...while loop is similar to the while loop except that the condition is always executed after the
body of a loop so it is an exit-controlled loop
The do-while loop is mainly used in the case where we need to execute the loop at least once
 The do-while loop is mostly used in menu-driven programs where the termination condition depends
upon the end user
Syntax:
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);

PRPEARED BY: PROF. NEHA HARDE ©
DO WHILE LOOP
Flowchart of do-while loop

PRPEARED BY: PROF. NEHA HARDE ©
WHILE LOOP
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf(“%d”, i);
i++;
} while(i <= 10);
return 0;
}
Example: Print 1 to 10 numbers using do - while loop

PRPEARED BY: PROF. NEHA HARDE ©
BREAK STATEMENT
The break is a keyword in C which is used to bring the program control out of the loop
The break statement is used inside loops or switch statement and it is associated with if
Syntax:
//loop or switch case
break;

PRPEARED BY: PROF. NEHA HARDE ©
#include<stdio.h>
int main()
{
int n, count = 0, sum=0;
printf(“\n Get sum of any 5 numbers between 1 to 10”);
do
{
printf(“\n Enter a number only between 1 to 10”);
scanf(“%d”, &n);
if(n < 1 && n > 10)
break;
sum = sum + n;
count++;
}while(count <= 5);
return 0;
}
Example: Find sum of any 5 numbers entered only between 1 to 10
BREAK STATEMENT

PRPEARED BY: PROF. NEHA HARDE ©
CONTINUE
STATEMENT
The continue statement skips the current iteration of the loop and continues with the next iteration
When the continue statement is executed in the loop, the code inside the loop following the
continue statement will be skipped and next iteration of the loop will begin.
Syntax:
//loop or switch case
continue;

PRPEARED BY: PROF. NEHA HARDE ©
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i == 4)
continue;
printf(“%d”, i);
}
return 0;
}
Example: Print 1 to 10 numbers except 4 using for loop
CONTINUE
STATEMENT
Tags