SlideShare a Scribd company logo
Lec#12 - Decisions:
If , Multiple if & If-Else if ladder
Flow of Control
• Unless specified otherwise, the order of statement execution through
a function is linear: one statement after another in sequence
• Some programming statements allow us to:
• decide whether or not to execute a particular statement
• execute a statement over and over, repetitively
• These decisions are based on boolean expressions (or conditions) that
evaluate to true or false
• The order of statement execution is called the flow of control
Conditional Statements
• A conditional statement lets us choose which statement will be
executed next
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• The C conditional statements are the:
• if statement
• if-else statement
• switch statement
Decision Control Structure If & Else
The if-else Statement
• An else clause can be added to an if statement to make an if-else
statement
• If the condition is true, statement1 is executed; if the condition is false, statement2 is executed
• One or the other will be executed, but not both
Boolean Expressions
• A condition often uses one of C's equality operators or relational operators, which all return boolean results:
• == equal to
• != not equal to
• < less than
• > greater than
• <= less than or equal to
• >= greater than or equal to
• Note the difference between the equality operator (==) and the assignment operator (=)
Nested if Statements
• The statement executed as a result of an if statement or else clause
could be another if statement
• These are called nested if statements
• An else clause is matched to the last unmatched if (no matter what
the indentation implies)
• Braces can be used to specify the if statement to which an else clause
belongs
Example-01
#include<stdio.h>
void main()
{
int n;
clrscr();
printf(“enter the number:”);
scanf(“%d”,&n);
if (n%2==0)
printf(“the number is
even”);
else
printf(“the number is
odd”);
getch;
}
Example-02
Example
#include <stdio.h>
int main() /* Most important part of the
program! */
{
int age; /* Need a variable... */
printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in
age */
if ( age < 100 ) { /* If the age is less than
100 */
printf ("You are pretty young!n" ); /* Just to show
you it works... */
}
else if ( age == 100 ) { /* I use else just to
show an example */
printf( "You are oldn" );
}
else {
printf( "You are really oldn" ); /* Executed if no
other statement is */
}
return 0;
}
#include<stdio.h>
main()
{
int grade;
char student[25],section[25];
clrscr();
printf(“Enter the name of a student: “);
scanf(“%s”, &student);
printf(“Enter the section of a student: “);
scanf(“%s”, &section);
printf(“Enter the grade of a student: “);
scanf(“%d”, &grade);
if(grade>75)
printf(“PASSED”);
else
print(“FAILED”);
getch();
}
Example-03
#include <stdio.h>
#define SECRET 17
int main()
{
int guess;
printf("Can you guess the secret
number: ");
scanf("%d",&guess);
if(guess==SECRET)
{
puts("You guessed it!");
return(0);
}
if(guess!=SECRET)
{
puts("Wrong!");
return(1);
}
}
#include <stdio.h>
#include <conio.h>
int main( )
{
char ch,a;
printf("Enter any
character: ");
scanf("%c", &ch);
if((ch>='a' && ch<='z') || (ch>='A'
&& ch<='Z'))
{
printf("Character is an
ALPHABET");
}
else
{
printf("Character is NOT
ALPHABET");
}
getch();
}
Example-04
#include <stdio.h>
int main()
{
int cp,sp, amt;
printf("Enter Cost Price: ");
scanf("%d", &cp);
printf("Enter Selling Price: ");
scanf("%d", &sp);
if(sp>cp) //Profit
{
amt = sp-cp;
printf("nProfit = %d", amt);
}
else if(cp>sp) //Loss
{
amt = cp-sp;
printf("n Loss = %d", amt);
}
else //No Profit No Loss
{
printf("nNo Profit No Loss");
}
return 0;
}
#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if((num%5==0) && (num%11== 0) )
{
printf("Number is divisible by 5 and
11");
}
else
{
printf("Number is not divisible by 5
and 11");
}
return 0;
}
Example-05
#include <stdio.h>
int main()
{
int a, b, c, max;
printf("Enter Three Integersn");
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
if(a > c)
max = a;
else
max = c;
} else
{
if(b > c)
max = b;
else
max = c;
}
printf("Maximum Number is = %dn", max);
return 0;
}

More Related Content

What's hot (20)

PPTX
Decision making statements in C programming
Rabin BK
 
PPTX
Decision making and branching in c programming
Priyansh Thakar
 
PPT
Control structure
Samsil Arefin
 
PPTX
Selection Statements in C Programming
Kamal Acharya
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
PDF
C programming decision making
SENA
 
PPT
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
PPT
Control Structures
Ghaffar Khan
 
PPT
Branching in C
Prabhu Govind
 
PPT
Decision making and branching
Hossain Md Shakhawat
 
PPTX
Conditional Statement in C Language
Shaina Arora
 
PPTX
Control structure of c language
Digvijaysinh Gohil
 
PPSX
C lecture 3 control statements slideshare
Gagan Deep
 
PPSX
Bsit1
jigeno
 
PPTX
Decision statements in c language
tanmaymodi4
 
PPTX
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Decision making statements in C programming
Rabin BK
 
Decision making and branching in c programming
Priyansh Thakar
 
Control structure
Samsil Arefin
 
Selection Statements in C Programming
Kamal Acharya
 
Control statements in c
Sathish Narayanan
 
C Programming: Control Structure
Sokngim Sa
 
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
C programming decision making
SENA
 
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
Control Structures
Ghaffar Khan
 
Branching in C
Prabhu Govind
 
Decision making and branching
Hossain Md Shakhawat
 
Conditional Statement in C Language
Shaina Arora
 
Control structure of c language
Digvijaysinh Gohil
 
C lecture 3 control statements slideshare
Gagan Deep
 
Bsit1
jigeno
 
Decision statements in c language
tanmaymodi4
 
Java if else condition - powerpoint persentation
Maneesha Caldera
 

Similar to Decision Control Structure If & Else (20)

PDF
control statement
Kathmandu University
 
PPTX
CH-4 (1).pptx
Mehul Desai
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PPTX
Decision Making and Branching
Munazza-Mah-Jabeen
 
PPTX
C Unit-2.ppt
Giri383500
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
PPTX
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
PPTX
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
PPTX
computer programming Control Statements.pptx
eaglesniper008
 
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PDF
Control statements anil
Anil Dutt
 
PPT
Lec 10
kapil078
 
PDF
Control statements-Computer programming
nmahi96
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PPTX
Condition Stmt n Looping stmt.pptx
Likhil181
 
PPT
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
PDF
Programming Fundamentals Decisions
imtiazalijoono
 
control statement
Kathmandu University
 
CH-4 (1).pptx
Mehul Desai
 
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Decision Making and Branching
Munazza-Mah-Jabeen
 
C Unit-2.ppt
Giri383500
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
computer programming Control Statements.pptx
eaglesniper008
 
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Control statements anil
Anil Dutt
 
Lec 10
kapil078
 
Control statements-Computer programming
nmahi96
 
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Condition Stmt n Looping stmt.pptx
Likhil181
 
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Programming Fundamentals Decisions
imtiazalijoono
 
Ad

Recently uploaded (20)

PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Ad

Decision Control Structure If & Else

  • 1. Lec#12 - Decisions: If , Multiple if & If-Else if ladder
  • 2. Flow of Control • Unless specified otherwise, the order of statement execution through a function is linear: one statement after another in sequence • Some programming statements allow us to: • decide whether or not to execute a particular statement • execute a statement over and over, repetitively • These decisions are based on boolean expressions (or conditions) that evaluate to true or false • The order of statement execution is called the flow of control
  • 3. Conditional Statements • A conditional statement lets us choose which statement will be executed next • Therefore they are sometimes called selection statements • Conditional statements give us the power to make basic decisions • The C conditional statements are the: • if statement • if-else statement • switch statement
  • 5. The if-else Statement • An else clause can be added to an if statement to make an if-else statement • If the condition is true, statement1 is executed; if the condition is false, statement2 is executed • One or the other will be executed, but not both
  • 6. Boolean Expressions • A condition often uses one of C's equality operators or relational operators, which all return boolean results: • == equal to • != not equal to • < less than • > greater than • <= less than or equal to • >= greater than or equal to • Note the difference between the equality operator (==) and the assignment operator (=)
  • 7. Nested if Statements • The statement executed as a result of an if statement or else clause could be another if statement • These are called nested if statements • An else clause is matched to the last unmatched if (no matter what the indentation implies) • Braces can be used to specify the if statement to which an else clause belongs
  • 9. #include<stdio.h> void main() { int n; clrscr(); printf(“enter the number:”); scanf(“%d”,&n); if (n%2==0) printf(“the number is even”); else printf(“the number is odd”); getch; }
  • 11. Example #include <stdio.h> int main() /* Most important part of the program! */ { int age; /* Need a variable... */ printf( "Please enter your age" ); /* Asks for age */ scanf( "%d", &age ); /* The input is put in age */ if ( age < 100 ) { /* If the age is less than 100 */ printf ("You are pretty young!n" ); /* Just to show you it works... */ } else if ( age == 100 ) { /* I use else just to show an example */ printf( "You are oldn" ); } else { printf( "You are really oldn" ); /* Executed if no other statement is */ } return 0; } #include<stdio.h> main() { int grade; char student[25],section[25]; clrscr(); printf(“Enter the name of a student: “); scanf(“%s”, &student); printf(“Enter the section of a student: “); scanf(“%s”, &section); printf(“Enter the grade of a student: “); scanf(“%d”, &grade); if(grade>75) printf(“PASSED”); else print(“FAILED”); getch(); }
  • 13. #include <stdio.h> #define SECRET 17 int main() { int guess; printf("Can you guess the secret number: "); scanf("%d",&guess); if(guess==SECRET) { puts("You guessed it!"); return(0); } if(guess!=SECRET) { puts("Wrong!"); return(1); } } #include <stdio.h> #include <conio.h> int main( ) { char ch,a; printf("Enter any character: "); scanf("%c", &ch); if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) { printf("Character is an ALPHABET"); } else { printf("Character is NOT ALPHABET"); } getch(); }
  • 15. #include <stdio.h> int main() { int cp,sp, amt; printf("Enter Cost Price: "); scanf("%d", &cp); printf("Enter Selling Price: "); scanf("%d", &sp); if(sp>cp) //Profit { amt = sp-cp; printf("nProfit = %d", amt); } else if(cp>sp) //Loss { amt = cp-sp; printf("n Loss = %d", amt); } else //No Profit No Loss { printf("nNo Profit No Loss"); } return 0; } #include <stdio.h> int main() { int num; printf("Enter any number: "); scanf("%d", &num); if((num%5==0) && (num%11== 0) ) { printf("Number is divisible by 5 and 11"); } else { printf("Number is not divisible by 5 and 11"); } return 0; }
  • 17. #include <stdio.h> int main() { int a, b, c, max; printf("Enter Three Integersn"); scanf("%d %d %d", &a, &b, &c); if(a > b) { if(a > c) max = a; else max = c; } else { if(b > c) max = b; else max = c; } printf("Maximum Number is = %dn", max); return 0; }