SlideShare a Scribd company logo
• Introduction
• Decision Making: If Statement
• If-Else Statement
• Nested If-Else Statements
• Else-If Ladder
• The Switch Statement
1
Introduction
• The statements that change the flow of a program to
change the order of execution of statements based on
certain conditions, or repeat a group of statements until
certain specified conditions are met are called as
Decision Making statements or Control statements. The
Control statements are categorized into three major
conditional types they are Decision making, Iteration
statements, Jump Statements
2
If Statement
The general Syntax for the simplest if statement:
if (expression) /* no semi-colon */
Statement;
Syntax for the simplest if statement:
if (expression) /* no semi-colon */
{
Statement 1;
Statement 2;
----------------
}
The if keyword is followed by an expression in parentheses. The
expression is evaluated. If the expression is true, it returns 1,
otherwise 0. The value 1 or any non-zero value is considered as
true and 0 as false. If the given expression in the if statement is
true, the following statement or block of statements are
executed; otherwise, the statement that appears immediately
after the if block (true block) is executed. 3
4
Execution of Statements
Multiple If Statement
The general Syntax for the multiple ifs:
if (expression) /* no semi-colon */
Statement 1;
if (expression) /* no semi-colon */
Statement 2;
if (expression) /* no semi-colon */
Statement 3;
5
If-Else Statement
If the expression/condition is true, the body of the if statement is
executed; otherwise, the body of the else statement is executed.
The else keyword is used when the expression is not true.
The general Syntax of if–else statement can be given as follows.
if (expression is true) // if block
{
statement1;
statement 2;
}
else // else block
{
statement 3;
statement 4;
} 6
If-Else Statement
7
Nested If-Else Statement
In this kind of statement, a number of logical conditions are
tested for taking decisions. Here, the if keyword followed
by an expression is evaluated. If it is true, the compiler
executes the block following the if condition; otherwise, it
skips this block and executes the else block. It uses
the if statement nested inside an if-else statement, which
is nested inside another if-else statement.
8
The general Syntax of nested if–else statement is
if (expression1)
{
if(expression2)
statement1;
else
statement2;
}
else
{
if(expression3)
statement3;
else
statement4;
}
next statement5;
9
The flowchart for nesting an if-else statement is shown below
10
Else – If Ladder
A common programming construct is the else-if ladder,
sometimes called the if-else-ifstaircase because of its
appearance. In the program one can write a ladder of else-
if. The program goes down the ladder of else-if, in
anticipation of one of the expressions being true.
11
The general Syntax of nested if–else statement is
if(condition)
{
statement 1; /* if block*/
statement 2;
}
else if(condition)
{
statement 3; /* else if block*/
statement 4;
}
else
{
statement 5; /* last else block */
statement 6;
}
12
The flowchart for nesting an else-if statement is shown below.
The conditions are evaluated from top to bottom. As soon as a true
condition is met, the associated statement block gets executed and the
rest of the ladder is bypassed. If none of the conditions are met, then
the final else block is executed. If this else is not present and none of
the if statements evaluate to true, then the entire ladder is bypassed. 13
Switch Statement
The switch statement is a multi-way branch statement and an
alternative to if-else-if ladder in many situations. The expression
of switch contains only one argument, which is then checked with a
number of switch cases. The switch statement evaluates the
expression and then looks for its value among the case constants. If
the value is matched with a particular case constant, then those
case statements are executed until a break statement is found or
until the end of switch block is reached. If not, then simply
the default (if present) is executed (if a default is not present, then
the control flows out of the switch block). The default is normally
present at the bottom of theswitch case structure. But we can also
define default statement anywhere in the switchstructure.
The default block must not be empty. Every case statement
terminates with a ‘:’ (colon). The break statement is used to stop the
execution of succeeding cases and pass the control to the end of
the switch block. 14
The general Syntax of switch statement is
switch(variable or expression)
{
case constant A: statement;
break;
case constant B: statement;
break;
default: statement;
}
15
The flowchart for switch statement is shown below.
The conditions are evaluated from top to bottom. As soon as a true
condition is met, the associated statement block gets executed and the
rest of the ladder is bypassed. If none of the conditions are met, then
the final else block is executed. If this else is not present and none of
the if statements evaluate to true, then the entire ladder is bypassed. 16
Note the following for switch case.
1. The switch expression: In the block the variable or expression can be a character
or an integer. The integer expression following the keyword switch will yield an
integer value only. The integer may be any value 1, 2, 3, etc. In case of character
constant, the values may be with alphabets such as ‘x’, ‘y’, ‘z’, etc.
2. The switch organization: The switch expression should neither be terminated
with a semicolon (;) nor with any other symbol. The entire case structure following
the switch should be enclosed within curly braces. The keyword case is followed by a
constant. Every constant terminates with a colon (:). Each case statement must
contain different constant values. Any number of case statements can be provided. If
the case structure contains multiple statements, they need not be enclosed within
curly braces. Here, the keyword case & break performs, respectively, the job of
opening and closing curly braces.
3. The switch execution: When one of the cases is satisfied, the statements
following it are executed. In case there is no match, the default case is executed.
4.The break statement used in switch passes control outside the switch block. By
mistake if no break statements are given, all the cases following it are executed.
17
#include <stdio.h>
int main()
{
char ch;
printf("Input a charactern");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.n", ch);
break;
default:
printf("%c is not a vowel.n", ch);
}
return 0;
} 18
Switch – Statement : Example
#include <stdio.h>
main()
{
char grade;
printf("Enter a character");
scanf(" %c",&grade);
/* grade=getchar() */
switch( grade )
{
case 'A' : printf( "Excellentn" );
break;
case 'B' : printf( "Goodn" );
break;
case 'C' : printf( "OKn" );
break;
case 'D' : printf( "Can do bettern" );
break;
case 'F' : printf( "You must do better than thisn" );
break;
default : printf( "What is your grade anyway?n" );
break;
}
return 0;
}
19
20
#include <stdio.h> /* Roots of a quadratic equation */
#include <math.h>
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
Code 1 : Biggest of three numbers 3 given numbers
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
} 21
Code 2 : Biggest of three numbers 3 given numbers
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0; 22
Ad

More Related Content

What's hot (20)

Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
Manash Kumar Mondal
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
Bishal Sharma
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Mohammed Sikander
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
Rabin BK
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
kamalbeydoun
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
If and select statement
If and select statementIf and select statement
If and select statement
Rahul Sharma
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
Bishal Sharma
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
Rabin BK
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
kamalbeydoun
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
If and select statement
If and select statementIf and select statement
If and select statement
Rahul Sharma
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 

Similar to Decision Making and Branching in C (20)

Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
TejaswiB4
 
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
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201
vpenubot
 
control-statements in C Language MH.pptx
control-statements in C Language MH.pptxcontrol-statements in C Language MH.pptx
control-statements in C Language MH.pptx
mehedi_hasan
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
MLG College of Learning, Inc
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
MLG College of Learning, Inc
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
Shahzu2
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
C language control statements
C language  control statementsC language  control statements
C language control statements
suman Aggarwal
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
Atul Sehdev
 
C statements
C statementsC statements
C statements
Ahsann111
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
FarshidKhan
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
TejaswiB4
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201Unit-2 control Structures.pptx.pptx20201
Unit-2 control Structures.pptx.pptx20201
vpenubot
 
control-statements in C Language MH.pptx
control-statements in C Language MH.pptxcontrol-statements in C Language MH.pptx
control-statements in C Language MH.pptx
mehedi_hasan
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
Shahzu2
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 
C language control statements
C language  control statementsC language  control statements
C language control statements
suman Aggarwal
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
Atul Sehdev
 
C statements
C statementsC statements
C statements
Ahsann111
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
FarshidKhan
 
Ad

Recently uploaded (20)

Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Ad

Decision Making and Branching in C

  • 1. • Introduction • Decision Making: If Statement • If-Else Statement • Nested If-Else Statements • Else-If Ladder • The Switch Statement 1
  • 2. Introduction • The statements that change the flow of a program to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met are called as Decision Making statements or Control statements. The Control statements are categorized into three major conditional types they are Decision making, Iteration statements, Jump Statements 2
  • 3. If Statement The general Syntax for the simplest if statement: if (expression) /* no semi-colon */ Statement; Syntax for the simplest if statement: if (expression) /* no semi-colon */ { Statement 1; Statement 2; ---------------- } The if keyword is followed by an expression in parentheses. The expression is evaluated. If the expression is true, it returns 1, otherwise 0. The value 1 or any non-zero value is considered as true and 0 as false. If the given expression in the if statement is true, the following statement or block of statements are executed; otherwise, the statement that appears immediately after the if block (true block) is executed. 3
  • 5. Multiple If Statement The general Syntax for the multiple ifs: if (expression) /* no semi-colon */ Statement 1; if (expression) /* no semi-colon */ Statement 2; if (expression) /* no semi-colon */ Statement 3; 5
  • 6. If-Else Statement If the expression/condition is true, the body of the if statement is executed; otherwise, the body of the else statement is executed. The else keyword is used when the expression is not true. The general Syntax of if–else statement can be given as follows. if (expression is true) // if block { statement1; statement 2; } else // else block { statement 3; statement 4; } 6
  • 8. Nested If-Else Statement In this kind of statement, a number of logical conditions are tested for taking decisions. Here, the if keyword followed by an expression is evaluated. If it is true, the compiler executes the block following the if condition; otherwise, it skips this block and executes the else block. It uses the if statement nested inside an if-else statement, which is nested inside another if-else statement. 8
  • 9. The general Syntax of nested if–else statement is if (expression1) { if(expression2) statement1; else statement2; } else { if(expression3) statement3; else statement4; } next statement5; 9
  • 10. The flowchart for nesting an if-else statement is shown below 10
  • 11. Else – If Ladder A common programming construct is the else-if ladder, sometimes called the if-else-ifstaircase because of its appearance. In the program one can write a ladder of else- if. The program goes down the ladder of else-if, in anticipation of one of the expressions being true. 11
  • 12. The general Syntax of nested if–else statement is if(condition) { statement 1; /* if block*/ statement 2; } else if(condition) { statement 3; /* else if block*/ statement 4; } else { statement 5; /* last else block */ statement 6; } 12
  • 13. The flowchart for nesting an else-if statement is shown below. The conditions are evaluated from top to bottom. As soon as a true condition is met, the associated statement block gets executed and the rest of the ladder is bypassed. If none of the conditions are met, then the final else block is executed. If this else is not present and none of the if statements evaluate to true, then the entire ladder is bypassed. 13
  • 14. Switch Statement The switch statement is a multi-way branch statement and an alternative to if-else-if ladder in many situations. The expression of switch contains only one argument, which is then checked with a number of switch cases. The switch statement evaluates the expression and then looks for its value among the case constants. If the value is matched with a particular case constant, then those case statements are executed until a break statement is found or until the end of switch block is reached. If not, then simply the default (if present) is executed (if a default is not present, then the control flows out of the switch block). The default is normally present at the bottom of theswitch case structure. But we can also define default statement anywhere in the switchstructure. The default block must not be empty. Every case statement terminates with a ‘:’ (colon). The break statement is used to stop the execution of succeeding cases and pass the control to the end of the switch block. 14
  • 15. The general Syntax of switch statement is switch(variable or expression) { case constant A: statement; break; case constant B: statement; break; default: statement; } 15
  • 16. The flowchart for switch statement is shown below. The conditions are evaluated from top to bottom. As soon as a true condition is met, the associated statement block gets executed and the rest of the ladder is bypassed. If none of the conditions are met, then the final else block is executed. If this else is not present and none of the if statements evaluate to true, then the entire ladder is bypassed. 16
  • 17. Note the following for switch case. 1. The switch expression: In the block the variable or expression can be a character or an integer. The integer expression following the keyword switch will yield an integer value only. The integer may be any value 1, 2, 3, etc. In case of character constant, the values may be with alphabets such as ‘x’, ‘y’, ‘z’, etc. 2. The switch organization: The switch expression should neither be terminated with a semicolon (;) nor with any other symbol. The entire case structure following the switch should be enclosed within curly braces. The keyword case is followed by a constant. Every constant terminates with a colon (:). Each case statement must contain different constant values. Any number of case statements can be provided. If the case structure contains multiple statements, they need not be enclosed within curly braces. Here, the keyword case & break performs, respectively, the job of opening and closing curly braces. 3. The switch execution: When one of the cases is satisfied, the statements following it are executed. In case there is no match, the default case is executed. 4.The break statement used in switch passes control outside the switch block. By mistake if no break statements are given, all the cases following it are executed. 17
  • 18. #include <stdio.h> int main() { char ch; printf("Input a charactern"); scanf("%c", &ch); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("%c is a vowel.n", ch); break; default: printf("%c is not a vowel.n", ch); } return 0; } 18
  • 19. Switch – Statement : Example #include <stdio.h> main() { char grade; printf("Enter a character"); scanf(" %c",&grade); /* grade=getchar() */ switch( grade ) { case 'A' : printf( "Excellentn" ); break; case 'B' : printf( "Goodn" ); break; case 'C' : printf( "OKn" ); break; case 'D' : printf( "Can do bettern" ); break; case 'F' : printf( "You must do better than thisn" ); break; default : printf( "What is your grade anyway?n" ); break; } return 0; } 19
  • 20. 20 #include <stdio.h> /* Roots of a quadratic equation */ #include <math.h> int main() { float a, b, c, determinant, r1,r2, real, imag; printf("Enter coefficients a, b and c: "); scanf("%f%f%f",&a,&b,&c); determinant=b*b-4*a*c; if (determinant>0) { r1= (-b+sqrt(determinant))/(2*a); r2= (-b-sqrt(determinant))/(2*a); printf("Roots are: %.2f and %.2f",r1 , r2); } else if (determinant==0) { r1 = r2 = -b/(2*a); printf("Roots are: %.2f and %.2f", r1, r2); } else { real= -b/(2*a); imag = sqrt(-determinant)/(2*a); printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag); } return 0; }
  • 21. Code 1 : Biggest of three numbers 3 given numbers #include <stdio.h> int main(){ float a, b, c; printf("Enter three numbers: "); scanf("%f %f %f", &a, &b, &c); if(a>=b && a>=c) printf("Largest number = %.2f", a); if(b>=a && b>=c) printf("Largest number = %.2f", b); if(c>=a && c>=b) printf("Largest number = %.2f", c); return 0; } 21
  • 22. Code 2 : Biggest of three numbers 3 given numbers #include <stdio.h> int main() { float a, b, c; printf("Enter three numbers: "); scanf("%f %f %f", &a, &b, &c); if (a>=b) { if(a>=c) printf("Largest number = %.2f",a); else printf("Largest number = %.2f",c); } else { if(b>=c) printf("Largest number = %.2f",b); else printf("Largest number = %.2f",c); } return 0; 22
  翻译: