SlideShare a Scribd company logo
UNIT-2
CONTROL STRUCTURES / STATEMENTS
Presented by
Dr. P. Sreekanth
C Supports Mainly Three Types Of Control Statements.
I. Decision making statements
1) Simple if Statement
2) if – else Statement
3) Nested if-else statement
4) else – if Ladder
5) switch statement
II. LOOP CONTROL STATEMENTS
1) for Loop
2) while Loop
3) do-while Loop
III. Unconditional Control
Statements
1) goto Statement
2) break Statement
3) continue Statement
IF - Statement
It is one of the powerful conditional statement. If statement is responsible for modifying the
flow of execution of a program.
If statement is always used with a condition.
The condition is evaluated first before executing any statement inside the body of If. The
syntax for if statement is as follows:
IF-Syntax
if(expression)
{
//code to be executed
}
Flowchart Of If Statement In C
How If Statement Works?
1. The if statement evaluates the test expression inside the parenthesis ().
2. If the test expression is evaluated to true, statements inside the body of if are executed.
3. If the test expression is evaluated to false, statements inside the body of if are not
executed.
Exmple-1
IF- Else Statement
•It is observed that the if statement executes only when the condition following if is true.
•It does nothing when the condition is false.
•In if-else either True-Block or False – Block will be executed and not both.
•The “else” Statement cannot be used without “if”.
Syntax
if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
If- Else Flow Chart
EXAMPLE-1
#include<stdio.h>
int main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}
ELSE IF
The else..if statement is useful when you need to check multiple conditions within the program,
nesting of if-else blocks can be avoided using else..if statement.
The if-else-if ladder statement is an extension to the if-else statement.
It is used in the scenario where there are multiple cases to be performed for different conditions.
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.
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
}
EXAMPLE
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
OUT PUT
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
NESTED ELSE IF
When an if else statement is present inside the body of another “if” or “else” then this
is called nested if else.
(or)
When a series of decision is required, nested if-else is used. Nesting means using one
if-else construct within another one
Syntax Of Nested If Else Statement:
if(condition)
{
//Nested if else inside the body of "if"
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
EXAMPLE
#include<stdio.h>
int main()
{
int num=1;
if(num<10)
{
if(num==1)
{
printf("The value is:%dn",num);
}
else
{
printf("The value is greater than 1");
}
}
else
{
printf("The value is greater than 10");
}
return 0;
}
SWITCH
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.
Here, We can define various statements in the multiple cases for the different values
of a single variable.
SYNTAX
switch(expression)
{
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
RULES FOR SWITCH STATEMENT IN C LANGUAGE
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break
statement found in the case, all the cases will be executed present after the matched
case. It is known as fall through the state of C switch statement.
Unit-2 control Structures.pptx.pptx20201
EXAMPLE
#include<stdio.h>
int main()
{
printf("enter a number:");
scanf("%d",&number);
switch(number)
{
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
OUT PUT
enter a number:4
number is not equal to 10, 50 or 100
EXAMPLE 2
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}
}
OUTPUT
Hi
Q1
Check whether a number is positive, negative or zero using switch case.
#include<stdio.h>
int main()
{
int num;
printf("Enter any Number: ");
scanf("%d",&num);
switch(num > 0)
{
case 1 : printf("Number is Positive");
break;
case 0 : if(num < 0)
{
printf("Number is Negative");
}
else
{
printf("Number is Zero");
}
break;
}
return 0;
}
Q2??
Design a calculator that performs arithmetic operations on two numbers using switch
case
Thank You

More Related Content

PPTX
control-statements in C Language MH.pptx
mehedi_hasan
 
PPTX
Conditional control statements explains the programming constructs
GopikaS12
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PPTX
Conditional Statement in C Language
Shaina Arora
 
PPTX
Control statement-Selective
Nurul Zakiah Zamri Tan
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PPTX
Condition Stmt n Looping stmt.pptx
Likhil181
 
PDF
exp227-jan-170127160848 (3) (1).pdf
mounikanarra3
 
control-statements in C Language MH.pptx
mehedi_hasan
 
Conditional control statements explains the programming constructs
GopikaS12
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Conditional Statement in C Language
Shaina Arora
 
Control statement-Selective
Nurul Zakiah Zamri Tan
 
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Condition Stmt n Looping stmt.pptx
Likhil181
 
exp227-jan-170127160848 (3) (1).pdf
mounikanarra3
 

Similar to Unit-2 control Structures.pptx.pptx20201 (20)

PPTX
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PDF
C programming decision making
SENA
 
PPTX
C Unit-2.ppt
Giri383500
 
PDF
Programming C Part 03
Raselmondalmehedi
 
PPTX
Control Statement programming
University of Potsdam
 
PPTX
Conditional statements
NabishaAK
 
PPTX
DECISION MAKING.pptx
Ayshwarya Baburam
 
PPT
C language UPTU Unit3 Slides
Anurag University Hyderabad
 
PPT
Decision Making and Branching in C
RAJ KUMAR
 
PDF
175035 cse lab-03
Mahbubay Rabbani Mim
 
PPT
Decision making in C(2020-2021) statements
BalaKrishnan466
 
PPTX
Selection Statements in C Programming
Kamal Acharya
 
PPTX
BHARGAVISTATEMENTS.PPT.pptx
Sasideepa
 
PPTX
CONTROL FLOW in C.pptx
SmitaAparadh
 
DOCX
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
Sitamarhi Institute of Technology
 
PDF
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
ruvirgandhi123
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
C Programming: Control Statements in C Pgm
Navya Francis
 
PPTX
CH-4 (1).pptx
Mehul Desai
 
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
C programming decision making
SENA
 
C Unit-2.ppt
Giri383500
 
Programming C Part 03
Raselmondalmehedi
 
Control Statement programming
University of Potsdam
 
Conditional statements
NabishaAK
 
DECISION MAKING.pptx
Ayshwarya Baburam
 
C language UPTU Unit3 Slides
Anurag University Hyderabad
 
Decision Making and Branching in C
RAJ KUMAR
 
175035 cse lab-03
Mahbubay Rabbani Mim
 
Decision making in C(2020-2021) statements
BalaKrishnan466
 
Selection Statements in C Programming
Kamal Acharya
 
BHARGAVISTATEMENTS.PPT.pptx
Sasideepa
 
CONTROL FLOW in C.pptx
SmitaAparadh
 
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
Sitamarhi Institute of Technology
 
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
ruvirgandhi123
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
C Programming: Control Statements in C Pgm
Navya Francis
 
CH-4 (1).pptx
Mehul Desai
 
Ad

Recently uploaded (20)

DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Ad

Unit-2 control Structures.pptx.pptx20201

  • 1. UNIT-2 CONTROL STRUCTURES / STATEMENTS Presented by Dr. P. Sreekanth
  • 2. C Supports Mainly Three Types Of Control Statements. I. Decision making statements 1) Simple if Statement 2) if – else Statement 3) Nested if-else statement 4) else – if Ladder 5) switch statement
  • 3. II. LOOP CONTROL STATEMENTS 1) for Loop 2) while Loop 3) do-while Loop
  • 4. III. Unconditional Control Statements 1) goto Statement 2) break Statement 3) continue Statement
  • 5. IF - Statement It is one of the powerful conditional statement. If statement is responsible for modifying the flow of execution of a program. If statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If. The syntax for if statement is as follows:
  • 7. Flowchart Of If Statement In C
  • 8. How If Statement Works? 1. The if statement evaluates the test expression inside the parenthesis (). 2. If the test expression is evaluated to true, statements inside the body of if are executed. 3. If the test expression is evaluated to false, statements inside the body of if are not executed.
  • 10. IF- Else Statement •It is observed that the if statement executes only when the condition following if is true. •It does nothing when the condition is false. •In if-else either True-Block or False – Block will be executed and not both. •The “else” Statement cannot be used without “if”.
  • 11. Syntax if (test-expression) { True block of statements } Else { False block of statements } Statements;
  • 12. If- Else Flow Chart
  • 13. EXAMPLE-1 #include<stdio.h> int main() { int num=19; if(num<10) { printf("The value is less than 10"); } else { printf("The value is greater than 10"); } return 0; }
  • 14. ELSE IF The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement. The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. 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. 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. EXAMPLE #include<stdio.h> int main(){ int number=0; printf("enter a number:"); scanf("%d",&number); if(number==10){ printf("number is equals to 10"); } else if(number==50){ printf("number is equal to 50"); }
  • 17. else if(number==100){ printf("number is equal to 100"); } else{ printf("number is not equal to 10, 50 or 100"); } return 0; }
  • 18. OUT PUT enter a number:4 number is not equal to 10, 50 or 100 enter a number:50 number is equal to 50
  • 19. NESTED ELSE IF When an if else statement is present inside the body of another “if” or “else” then this is called nested if else. (or) When a series of decision is required, nested if-else is used. Nesting means using one if-else construct within another one
  • 20. Syntax Of Nested If Else Statement: if(condition) { //Nested if else inside the body of "if" if(condition2) { //Statements inside the body of nested "if" } else { //Statements inside the body of nested "else" } } else { //Statements inside the body of "else" }
  • 21. EXAMPLE #include<stdio.h> int main() { int num=1; if(num<10) { if(num==1) { printf("The value is:%dn",num); } else { printf("The value is greater than 1"); } }
  • 22. else { printf("The value is greater than 10"); } return 0; }
  • 23. SWITCH 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. Here, We can define various statements in the multiple cases for the different values of a single variable.
  • 24. SYNTAX switch(expression) { case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); }
  • 25. RULES FOR SWITCH STATEMENT IN C LANGUAGE 1) The switch expression must be of an integer or character type. 2) The case value must be an integer or character constant. 3) The case value can be used only inside the switch statement. 4) The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known as fall through the state of C switch statement.
  • 27. EXAMPLE #include<stdio.h> int main() { printf("enter a number:"); scanf("%d",&number); switch(number) { case 10: printf("number is equals to 10"); break;
  • 28. case 50: printf("number is equal to 50"); break; case 100: printf("number is equal to 100"); break; default: printf("number is not equal to 10, 50 or 100"); } return 0; }
  • 29. OUT PUT enter a number:4 number is not equal to 10, 50 or 100
  • 30. EXAMPLE 2 #include <stdio.h> int main() { int x = 10, y = 5; switch(x>y && x+y>0) { case 1: printf("hi"); break; case 0: printf("bye"); break; default: printf(" Hello bye "); } }
  • 32. Q1 Check whether a number is positive, negative or zero using switch case.
  • 33. #include<stdio.h> int main() { int num; printf("Enter any Number: "); scanf("%d",&num);
  • 34. switch(num > 0) { case 1 : printf("Number is Positive"); break; case 0 : if(num < 0) { printf("Number is Negative"); } else { printf("Number is Zero"); } break; } return 0; }
  • 35. Q2?? Design a calculator that performs arithmetic operations on two numbers using switch case