SlideShare a Scribd company logo
1 of 17
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
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

Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programmingPriyansh Thakar
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
C programming decision making
C programming decision makingC programming decision making
C programming decision makingSENA
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Control structure of c language
Control structure of c languageControl structure of c language
Control structure of c languageDigvijaysinh Gohil
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshareGagan Deep
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentationManeesha Caldera
 

What's hot (20)

Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Control structure
Control structureControl structure
Control structure
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Branching in C
Branching in CBranching in C
Branching in C
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Control structure of c language
Control structure of c languageControl structure of c language
Control structure of c language
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
Bsit1
Bsit1Bsit1
Bsit1
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 

Similar to Decision Control Structure If & Else

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CChandrakantDivate1
 
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 CSowmya Jyothi
 
programming c language.
programming c language. programming c language.
programming c language. Abdul Rehman
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
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 LanguagesChandrakantDivate1
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptxishaparte4
 
C programming
C programmingC programming
C programmingXad Kuain
 
Branching statements
Branching statementsBranching statements
Branching statementsArunMK17
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 

Similar to Decision Control Structure If & Else (20)

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
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
 
programming c language.
programming c language. programming c language.
programming c language.
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
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
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
control statement
control statement control statement
control statement
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
Session 3
Session 3Session 3
Session 3
 
C programming
C programmingC programming
C programming
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 

Recently uploaded

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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
  • 4.
  • 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; }