SlideShare a Scribd company logo
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 1st
Feb 2021");
else if(age >= 45)
printf("n Vaccination will start from 1st
March 2021 ");
else if(age >= 18)
printf("n Vaccination will start from 1st
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

More Related Content

Similar to Module 2 - Control Structures c programming.pptm.pdf (20)

CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
Unit 2=Decision Control & Looping Statements.pdf
Unit 2=Decision Control & Looping Statements.pdfUnit 2=Decision Control & Looping Statements.pdf
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
JAYDEV PATEL
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
chapter-4 slide.pdf
chapter-4 slide.pdfchapter-4 slide.pdf
chapter-4 slide.pdf
cricketreview
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
Vikash Dhal
 
Structured Programming Unit-5-Control-Structure.pptx
Structured Programming  Unit-5-Control-Structure.pptxStructured Programming  Unit-5-Control-Structure.pptx
Structured Programming Unit-5-Control-Structure.pptx
SuryaBasnet1
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
indra Kishor
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
C operators
C operatorsC operators
C operators
srmohan06
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
nikshaikh786
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
JAYDEV PATEL
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
Vikash Dhal
 
Structured Programming Unit-5-Control-Structure.pptx
Structured Programming  Unit-5-Control-Structure.pptxStructured Programming  Unit-5-Control-Structure.pptx
Structured Programming Unit-5-Control-Structure.pptx
SuryaBasnet1
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
indra Kishor
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
nikshaikh786
 

Recently uploaded (20)

6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
STKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 versionSTKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 version
Dr. Jimmy Schwarzkopf
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Palo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity FoundationPalo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity Foundation
VICTOR MAESTRE RAMIREZ
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
The case for on-premises AI
The case for on-premises AIThe case for on-premises AI
The case for on-premises AI
Principled Technologies
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto CertificateCybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
VICTOR MAESTRE RAMIREZ
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
STKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 versionSTKI Israel Market Study 2025 final v1 version
STKI Israel Market Study 2025 final v1 version
Dr. Jimmy Schwarzkopf
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
Palo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity FoundationPalo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity Foundation
VICTOR MAESTRE RAMIREZ
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto CertificateCybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
VICTOR MAESTRE RAMIREZ
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
Ad

Module 2 - Control Structures c programming.pptm.pdf

  • 1. PRPEARED BY: PROF. NEHA HARDE © C PROGRAMMING Module 2 Control Structures Course Outcome (CO2): Demonstrate the use of control structures.
  • 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)
  • 3. PRPEARED BY: PROF. NEHA HARDE © SEQUENCE STRUCTURE In Sequence structure instructions are executed in the order they are written following the sequential flow
  • 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
  • 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
  • 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
  • 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 }
  • 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
  • 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
  • 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
  • 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
  • 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" }
  • 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; }
  • 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
  • 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 }
  • 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 1st Feb 2021"); else if(age >= 45) printf("n Vaccination will start from 1st March 2021 "); else if(age >= 18) printf("n Vaccination will start from 1st May 2021 "); else printf("n Not eligible for Vaccination"); return 0; } Example: Find start date of vaccination based on the age of the person
  • 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 }
  • 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)
  • 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.
  • 20. PRPEARED BY: PROF. NEHA HARDE © IF ELSE VS SWITCH CASE
  • 21. PRPEARED BY: PROF. NEHA HARDE ©  Looping structure  for loop  do while loop  while loop  break statement  continue statement Loop Control Structures
  • 22. PRPEARED BY: PROF. NEHA HARDE © LOOPING STRUCTURE
  • 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 }
  • 24. PRPEARED BY: PROF. NEHA HARDE © FOR LOOP Flowchart of For loop
  • 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
  • 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; }
  • 27. PRPEARED BY: PROF. NEHA HARDE © WHILE LOOP Flowchart of while loop
  • 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
  • 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);
  • 30. PRPEARED BY: PROF. NEHA HARDE © DO WHILE LOOP Flowchart of do-while loop
  • 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
  • 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;
  • 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
  • 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;
  • 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