SlidePub
Home
Categories
Login
Register
Home
Technology
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
17 views
35 slides
May 27, 2024
Slide
1
of 35
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
About This Presentation
c programming
Size:
2.02 MB
Language:
en
Added:
May 27, 2024
Slides:
35 pages
Slide Content
Slide 1
PRPEARED BY: PROF. NEHA HARDE ©
C PROGRAMMING
Module 2
Control Structures
Course Outcome (CO2):
Demonstrate the use of control structures.
Slide 2
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)
Slide 3
PRPEARED BY: PROF. NEHA HARDE ©
SEQUENCE STRUCTURE
In Sequence structure instructions are executed in the order they are written following the sequential flow
Slide 4
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
Slide 5
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
Slide 6
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
Slide 7
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
}
Slide 8
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
Slide 9
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
Slide 10
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
Slide 11
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
Slide 12
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"
}
Slide 13
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;
}
Slide 14
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
Slide 15
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
}
Slide 16
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
Slide 17
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
}
Slide 18
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)
Slide 19
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.
Slide 20
PRPEARED BY: PROF. NEHA HARDE ©
IF ELSE VS SWITCH CASE
Slide 21
PRPEARED BY: PROF. NEHA HARDE ©
Looping structure
for loop
do while loop
while loop
break statement
continue statement
Loop Control Structures
Slide 22
PRPEARED BY: PROF. NEHA HARDE ©
LOOPING STRUCTURE
Slide 23
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
}
Slide 24
PRPEARED BY: PROF. NEHA HARDE ©
FOR LOOP
Flowchart of For loop
Slide 25
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
Slide 26
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;
}
Slide 27
PRPEARED BY: PROF. NEHA HARDE ©
WHILE LOOP
Flowchart of while loop
Slide 28
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
Slide 29
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);
Slide 30
PRPEARED BY: PROF. NEHA HARDE ©
DO WHILE LOOP
Flowchart of do-while loop
Slide 31
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
Slide 32
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;
Slide 33
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
Slide 34
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;
Slide 35
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
Categories
Technology
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
17
Slides
35
Age
553 days
Related Slideshows
11
8-top-ai-courses-for-customer-support-representatives-in-2025.pptx
JeroenErne2
44 views
10
7-essential-ai-courses-for-call-center-supervisors-in-2025.pptx
JeroenErne2
45 views
13
25-essential-ai-courses-for-user-support-specialists-in-2025.pptx
JeroenErne2
36 views
11
8-essential-ai-courses-for-insurance-customer-service-representatives-in-2025.pptx
JeroenErne2
33 views
21
Know for Certain
DaveSinNM
19 views
17
PPT OPD LES 3ertt4t4tqqqe23e3e3rq2qq232.pptx
novasedanayoga46
23 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-35)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better