SlideShare a Scribd company logo
1 of 50
Program
 A Program is a set of instructions that a computer uses to perform a
specific function.
To use an analogy, a program is like a computer’s recipe.
It contains a list of ingredients (called variables) and a list of
directions(called statements) that tell the computer how to execute a
specific task.
 A statement causes an action to be performed by the program.
 It translates directly into one or more executable computer
instructions.
 Generally statement is ended with semicolon.
 Most statements need a semicolon at the end; some do not.
Statements
Types of Statements
Compound Statement
 Compound statements are used to group the statements into a single
executable unit.
 It consists of one or more individual statements enclosed within the
braces { }
 The decision is described to the computer as a conditional
statement that can be answered either true or false.
 If the answer is true, one or more action statements are executed.
 If the answer is false, then a different action or set of actions is
executed.
Types of decision control structures:
 if
 if..else
 nested if…else
 else if ladder
 dangling else
 switch statement
Decision Control Structures
if (condition)
{
statement-block;
}
Decision Control Statement: if
The general form of a simple if statement is:
Following are the properties of an if statement:
 If the condition is true then the statement-block will be executed.
 If the condition is false it does not do anything ( or the statement is
skipped)
 The condition is given in parentheses and must be evaluated as true
(nonzero value) or false (zero value).
 If a compound statement is provided, it must be enclosed in opening
and closing braces.
Enter
Condition
Body of the IF Statement
Exit
Decision Control Statement: if
Flow Chart
Example:
main()
{
int a=10,b=20;
if(a>b)
{
printf(“%d”,a);
}
printf(“%d”,b);
}
if...else Logic Flow
Decision Control Statement: if..else
;
;
Syntactical Rules for if…else Statements
 The expression or condition which is followed by if statement
must be enclosed in parenthesis.
 No semicolon is needed for an if…else statement.
 Both the true and false statements can be any statement (even
another if…else).
 Multiple statements under if and else should be enclosed
between curly braces.
 No need to enclose a single statement in curly braces.
A Simple if...else Statement
Decision Control Statement: if..else
Compound Statements in an if...else
Example for Decision Control Statement: if..else
Example for Decision Control Statement: if..else
Nested if…else Statements
Decision Control Statement: nested if…else
 Nested if…else means within the if…else you can include another if…else either in
if block or else block.
Decision Control Statement: nested if…else
Decision Control Statement: nested if…else
if (condition1)
statements1;
else if (condition2)
statements2;
else if (condition3)
statements3;
else if (condition4)
statements4;
……
else if(conditionn)
statementsn;
else
default_statement;
statement x;
Decision Control Statement: else if
 The conditions are evaluated from the top to down.
 As soon as a true condition is found the statement associated with it is executed
and the control is transferred to the statementx by skipping the rest of the ladder.
 When all n conditions become false,final else containing default_statement
that will be executed
main() {
float m1,m2,m3,m4;
float perc;
printf(“Enter marksn”);
scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4);
perc=(m1+m2+m3+m4)/4;
if(perc>=75)
printf(“nDistinction”);
else {
if(per<75 && per>=60)
printf(“nFirst Class”);
else {
if(per<60 && per>=50)
printf(“nSecond Class”);
else {
if(per<50 && per>=40)
printf(“nThird Class”);
else
Example program for nested if…else
Example program for else if
main()
{
float m1,m2,m3,m4;
float perc;
printf(“enter marksn”);
scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4);
perc=(m1+m2+m3+m4)/4;
if(perc>=75)
printf(“nDistinction”);
else if(per<75 && per>=60)
printf(“nFirst Class”);
else if(per<60 && per>=50)
printf(“nSecond Class”);
else if(per<50 && per>=40)
printf(“nThird Class”);
else
printf(“nFail”);
}//main
Dangling else
Dangling else
else is always paired with the most recent unpaired if.
Dangling else Solution
Dangling else (contd…)
 To avoid dangling else problem place the inner if statement with in
the curly braces.
Conditional Expression
 A simple if…else can be represented using the conditional (ternary)
expression.
 It is a multi-way conditional statement generalizing the if…else
statement.
 It is a conditional control statement that allows some particular group
of statements to be chosen from several available groups.
 A switch statement allows a single variable to be compared with
several possible case labels, which are represented by constant values.
 If the variable matches with one of the constants, then an execution
jump is made to that point.
 A case label cannot appear more than once and there can only be one
default expression.
Decision Control Statement: switch
 Note: switch statement does not allow less than ( < ), greater than (
> ).
 ONLY the equality operator (==) is used with a switch statement.
 The control variable must be integral (int or char) only.
 When the switch statement is encountered, the control variable is
evaluated.
 Then, if that evaluated value is equal to any of the values specified
in a case clause, the statements immediately following the colon
(“:”) begin to run.
 Default case is optional and if specified, default statements will be
executed, if there is no match for the case labels.
 Once the program flow enters a case label, the statements
associated with case have been executed, the program flow
continues with the statement for the next case. (if there is no break
statement after case label.)
Decision Control Statement: switch
General format of switch:
Decision Control Statement: switch
 The following results are possible, depending on the value of printFlag.
 If printFlag is 1, then all three printf statements are executed.
 If printFlag is 2, then the first print statement is skipped and the last two
are executed.
 Finally, if printFlag is neither 1 nor 2, then only the statement defined by
the default is executed.
Decision Control Statement: switch
Example1 for switch statement:
Decision Control Statement: switch
 If you want to execute only one case-label, C provides break
statement.
 It causes the program to jump out of the switch statement, that is go
to the closing braces (}) and continues the remaining code of the
program.
 If we add break to the last statement of the case, the general form of
switch case is as follows:
Decision Control Statement: switch
General format of switch:
Decision Control Statement: switch
Example2 for switch statement:
Decision Control Statement: switch
Concept of a loop(Iterative)
 The real power of computers is in their ability to repeat an
operation or a series of operations many times.
 This repetition, called looping, is one of the basic structured
programming concepts.
 Each loop must have an expression that determines if the loop is
done.
 If it is not done, the loop repeats one more time; if it is done, the
loop terminates.
Concept of a Loop
Pretest Loop
In each iteration, the control expression is tested first. If it is
true, the loop continues; otherwise, the loop is terminated.
Post-test Loop
In each iteration, the loop action(s) are executed. Then the
control expression is tested. If it is true, a new iteration is
started; otherwise, the loop terminates.
Note
Pretest and Post-test Loops
Loop Comparisons
C Loop Constructs
while
 The "while" loop is a generalized looping structure that employs a variable
or expression for testing the condition.
 It is a repetition statement that allows an action to be repeated while some
conditions remain true.
 The body of while statement can be a single statement or compound
statements.
 It doesn’t perform even a single operation if condition fails.
The while Statement
Example 1: To print 1 to 10 natural numbers
#include<stdio.h>
main()
{
int i;
i=1;
while (i<=10)
{
printf(“%dn”,i);
i++;
}
}
Example 2: To print the reverse of the given number.
void main()
{
int n, n1,rem, rev = 0;
printf("n Enter a positive number: ");
scanf("%d",&n);
n1=n;
while(n != 0)
{
rem = n%10;
rev = rev*10+rem;
n = n/10;
}
printf("The reverse of %d is %d",n1,rev);
}
do…while Statement
Example 3: To print fibonacci sequence for the given number.
#include<stdio.h>
main()
{
int a=0,b=1,c,i;
i=1;
printf("%d%d",a,b);
do
{
c=a+b;
i++;
printf("%3d",c);
a=b;
b=c;
}while(i<=10);
}
Example 4: To print multiplication table for 5.
#include <stdio.h>
void main()
{
int i = 1, n=5;
do
{
printf(“ %d * %d = %d “, n, i, n*i);
i = i + 1;
} while ( i<= 10);
}
 A for loop is used when a loop is to be executed a known number
of times.
We can do the same thing with a while loop, but the for loop is
easier to read and more natural for counting loops.
General form of the for is:
for( initialization; test-condition; updation)
{
Body of the loop
}
for
for Statement
break
 When a break statement is enclosed inside a block or loop, the loop is
immediately exited and program continues with the next statement
immediately following the loop.
 When loop are nested break only exit from the inner loop containing it.
The format of the break statement is:
Example 6: Program to demonstrate break statement.
#include<stdio.h>
main()
{
int i;
i=1;
while(i<=10)
{
if(i==8)
break;
printf(“%dt”,i);
i=i+1;
}
printf(“n Thanking You”);
}
continue
 When a continue statement is enclosed inside a block or loop, the loop is
to be continued with the next iteration.
 The continue statement tells the compiler, skip the following statements
and continue with the next iteration.
 The format of the continue statement is:
Example 5: Program to demonstrate continue statement.
#include<stdio.h>
main()
{
int i;
for(i=1;i<=5;i++)
{
if(i = = 3)
continue;
printf(" %d",i);
}
}

More Related Content

Similar to C statements.ppt presentation in c language

Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlENGWAU TONNY
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
control statements in python.pptx
control statements in python.pptxcontrol statements in python.pptx
control statements in python.pptxAnshu Varma
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
03a control structures
03a   control structures03a   control structures
03a control structuresManzoor ALam
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlanDeepak Lakhlan
 
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
 

Similar to C statements.ppt presentation in c language (20)

Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Control Structures
Control StructuresControl Structures
Control Structures
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
control statements in python.pptx
control statements in python.pptxcontrol statements in python.pptx
control statements in python.pptx
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Control All
Control AllControl All
Control All
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 

Recently uploaded

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

C statements.ppt presentation in c language

  • 1. Program  A Program is a set of instructions that a computer uses to perform a specific function. To use an analogy, a program is like a computer’s recipe. It contains a list of ingredients (called variables) and a list of directions(called statements) that tell the computer how to execute a specific task.
  • 2.  A statement causes an action to be performed by the program.  It translates directly into one or more executable computer instructions.  Generally statement is ended with semicolon.  Most statements need a semicolon at the end; some do not. Statements
  • 4. Compound Statement  Compound statements are used to group the statements into a single executable unit.  It consists of one or more individual statements enclosed within the braces { }
  • 5.  The decision is described to the computer as a conditional statement that can be answered either true or false.  If the answer is true, one or more action statements are executed.  If the answer is false, then a different action or set of actions is executed. Types of decision control structures:  if  if..else  nested if…else  else if ladder  dangling else  switch statement Decision Control Structures
  • 6. if (condition) { statement-block; } Decision Control Statement: if The general form of a simple if statement is: Following are the properties of an if statement:  If the condition is true then the statement-block will be executed.  If the condition is false it does not do anything ( or the statement is skipped)  The condition is given in parentheses and must be evaluated as true (nonzero value) or false (zero value).  If a compound statement is provided, it must be enclosed in opening and closing braces.
  • 7. Enter Condition Body of the IF Statement Exit Decision Control Statement: if Flow Chart Example: main() { int a=10,b=20; if(a>b) { printf(“%d”,a); } printf(“%d”,b); }
  • 8. if...else Logic Flow Decision Control Statement: if..else ; ;
  • 9. Syntactical Rules for if…else Statements  The expression or condition which is followed by if statement must be enclosed in parenthesis.  No semicolon is needed for an if…else statement.  Both the true and false statements can be any statement (even another if…else).  Multiple statements under if and else should be enclosed between curly braces.  No need to enclose a single statement in curly braces.
  • 10. A Simple if...else Statement Decision Control Statement: if..else
  • 11. Compound Statements in an if...else
  • 12. Example for Decision Control Statement: if..else
  • 13. Example for Decision Control Statement: if..else
  • 14. Nested if…else Statements Decision Control Statement: nested if…else  Nested if…else means within the if…else you can include another if…else either in if block or else block.
  • 15. Decision Control Statement: nested if…else
  • 16. Decision Control Statement: nested if…else
  • 17. if (condition1) statements1; else if (condition2) statements2; else if (condition3) statements3; else if (condition4) statements4; …… else if(conditionn) statementsn; else default_statement; statement x; Decision Control Statement: else if  The conditions are evaluated from the top to down.  As soon as a true condition is found the statement associated with it is executed and the control is transferred to the statementx by skipping the rest of the ladder.  When all n conditions become false,final else containing default_statement that will be executed
  • 18. main() { float m1,m2,m3,m4; float perc; printf(“Enter marksn”); scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4); perc=(m1+m2+m3+m4)/4; if(perc>=75) printf(“nDistinction”); else { if(per<75 && per>=60) printf(“nFirst Class”); else { if(per<60 && per>=50) printf(“nSecond Class”); else { if(per<50 && per>=40) printf(“nThird Class”); else Example program for nested if…else
  • 19. Example program for else if main() { float m1,m2,m3,m4; float perc; printf(“enter marksn”); scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4); perc=(m1+m2+m3+m4)/4; if(perc>=75) printf(“nDistinction”); else if(per<75 && per>=60) printf(“nFirst Class”); else if(per<60 && per>=50) printf(“nSecond Class”); else if(per<50 && per>=40) printf(“nThird Class”); else printf(“nFail”); }//main
  • 20. Dangling else Dangling else else is always paired with the most recent unpaired if.
  • 21. Dangling else Solution Dangling else (contd…)  To avoid dangling else problem place the inner if statement with in the curly braces.
  • 22. Conditional Expression  A simple if…else can be represented using the conditional (ternary) expression.
  • 23.  It is a multi-way conditional statement generalizing the if…else statement.  It is a conditional control statement that allows some particular group of statements to be chosen from several available groups.  A switch statement allows a single variable to be compared with several possible case labels, which are represented by constant values.  If the variable matches with one of the constants, then an execution jump is made to that point.  A case label cannot appear more than once and there can only be one default expression. Decision Control Statement: switch
  • 24.  Note: switch statement does not allow less than ( < ), greater than ( > ).  ONLY the equality operator (==) is used with a switch statement.  The control variable must be integral (int or char) only.  When the switch statement is encountered, the control variable is evaluated.  Then, if that evaluated value is equal to any of the values specified in a case clause, the statements immediately following the colon (“:”) begin to run.  Default case is optional and if specified, default statements will be executed, if there is no match for the case labels.  Once the program flow enters a case label, the statements associated with case have been executed, the program flow continues with the statement for the next case. (if there is no break statement after case label.) Decision Control Statement: switch
  • 25. General format of switch: Decision Control Statement: switch
  • 26.  The following results are possible, depending on the value of printFlag.  If printFlag is 1, then all three printf statements are executed.  If printFlag is 2, then the first print statement is skipped and the last two are executed.  Finally, if printFlag is neither 1 nor 2, then only the statement defined by the default is executed. Decision Control Statement: switch
  • 27. Example1 for switch statement: Decision Control Statement: switch
  • 28.  If you want to execute only one case-label, C provides break statement.  It causes the program to jump out of the switch statement, that is go to the closing braces (}) and continues the remaining code of the program.  If we add break to the last statement of the case, the general form of switch case is as follows: Decision Control Statement: switch
  • 29. General format of switch: Decision Control Statement: switch
  • 30. Example2 for switch statement: Decision Control Statement: switch
  • 31. Concept of a loop(Iterative)  The real power of computers is in their ability to repeat an operation or a series of operations many times.  This repetition, called looping, is one of the basic structured programming concepts.  Each loop must have an expression that determines if the loop is done.  If it is not done, the loop repeats one more time; if it is done, the loop terminates.
  • 32. Concept of a Loop
  • 33. Pretest Loop In each iteration, the control expression is tested first. If it is true, the loop continues; otherwise, the loop is terminated. Post-test Loop In each iteration, the loop action(s) are executed. Then the control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Note
  • 37. while  The "while" loop is a generalized looping structure that employs a variable or expression for testing the condition.  It is a repetition statement that allows an action to be repeated while some conditions remain true.  The body of while statement can be a single statement or compound statements.  It doesn’t perform even a single operation if condition fails.
  • 39. Example 1: To print 1 to 10 natural numbers #include<stdio.h> main() { int i; i=1; while (i<=10) { printf(“%dn”,i); i++; } }
  • 40. Example 2: To print the reverse of the given number. void main() { int n, n1,rem, rev = 0; printf("n Enter a positive number: "); scanf("%d",&n); n1=n; while(n != 0) { rem = n%10; rev = rev*10+rem; n = n/10; } printf("The reverse of %d is %d",n1,rev); }
  • 42. Example 3: To print fibonacci sequence for the given number. #include<stdio.h> main() { int a=0,b=1,c,i; i=1; printf("%d%d",a,b); do { c=a+b; i++; printf("%3d",c); a=b; b=c; }while(i<=10); }
  • 43. Example 4: To print multiplication table for 5. #include <stdio.h> void main() { int i = 1, n=5; do { printf(“ %d * %d = %d “, n, i, n*i); i = i + 1; } while ( i<= 10); }
  • 44.  A for loop is used when a loop is to be executed a known number of times. We can do the same thing with a while loop, but the for loop is easier to read and more natural for counting loops. General form of the for is: for( initialization; test-condition; updation) { Body of the loop } for
  • 46.
  • 47. break  When a break statement is enclosed inside a block or loop, the loop is immediately exited and program continues with the next statement immediately following the loop.  When loop are nested break only exit from the inner loop containing it. The format of the break statement is:
  • 48. Example 6: Program to demonstrate break statement. #include<stdio.h> main() { int i; i=1; while(i<=10) { if(i==8) break; printf(“%dt”,i); i=i+1; } printf(“n Thanking You”); }
  • 49. continue  When a continue statement is enclosed inside a block or loop, the loop is to be continued with the next iteration.  The continue statement tells the compiler, skip the following statements and continue with the next iteration.  The format of the continue statement is:
  • 50. Example 5: Program to demonstrate continue statement. #include<stdio.h> main() { int i; for(i=1;i<=5;i++) { if(i = = 3) continue; printf(" %d",i); } }