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

More Related Content

What's hot

What's hot (20)

Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
C programming - String
C programming - StringC programming - String
C programming - String
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
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
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
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
 
10. switch case
10. switch case10. switch case
10. switch case
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 

Similar to Decision Making and Branching in C

Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 

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
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
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
 
C statements
C statementsC statements
C statements
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 

Recently uploaded

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Recently uploaded (20)

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 

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