SlideShare a Scribd company logo
Lecture 3: Control
Structures - Selection
Author : Aamir Saleem Ansari
Web : www.techora.net
The Plan for Today
 Review from last two weeks
 Flowcharts

Pseudocode
 Data types
 Variables and Constants
 Structure of a C program
 Formatted output: printf( )
 Operators and their precedence
 Review control structures
 Sequence
 Selection
 Repetition
 Selection structures
 If
 If/else
 Switch
 Relational operators
 Selection structure example
Learning Objectives
 Apply concepts for developing algorithms, using
variables, and structuring a C program
 Explain what is meant by a control structure
 Explain the three basic types of control
structures
 Determine the result of relational comparisons
 Apply the if and if/else control structures
Control Structures - Review
 All programs can be written in terms of three
control structures (like building blocks)
 Sequence
 ‘Built-in’ to C
 Unless otherwise directed, one statement after the next is
executed
 Selection (three types)
 Depending on a condition, select between one statement or
another
 If var1 is greater than 10, do this…, else do that…
 Repetition (three types)
 Depending on a condition, execute one or more statements
repeatedly
Selection Structure Overview
 Three kinds of selections structures
 if (also called, ‘single-selection’)
 if condition is true
Perform action
 if condition is false, action is skipped, program continues
 if/else (also called, ‘double-selection’)
 if condition is true
Perform action
 else (if condition is false)
Perform a different action (this will be skipped if condition is true)
 switch (also called ‘multiple-selection’)
 Allows selection among many actions depending on the
integral value of a variable or expression
Single Selection IF - Flowchart
TRUE
FALSE
Speed > 65
connector
flow line
decision symbol
action symbol
Print “You’re
speeding”
Operations Associativity
::
() [] left to right
Function_name() right to left
. -> left to right
‘ ! ` ++ -- + - *
&(type) sizeof
right to left
* / % .* ./ left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
&& left to right
^^ left to right
|| left to right
?: right to left
= += -= *= /= %= |=
<<= >>=
right to left
, left to right
Relational Operators
Important for constructing
the decision expression
5 < 7 result is ____
5 > 7 result is _____
7 <= 7 result is ____
8 >= 7 result is ____
5 == 5 result is ____
5 == 7 result is ____
var1 = 7 result is____
5.0 == 5 result is ___
6 != 5 result is ____
Adapted from H. Cheng chap04.ppt, slide 5
Practice
Double-Selection IF - Flowchart
TRUE
Speed > 65
FALSE
Print “Over
speed limit”
Print “Within
speed limit”
Adapted from Deitel & Deitel, C How to Program, 6th
ed., p. 111
SWITCH - Flowchart
IF statement (single-selection)
 Syntax
if(expression) /* if expression is TRUE (i.e., NOT EQUAL to zero) */
statement1; /* then execute this statement */
statement2; /* execute this statement next*/
 Notes
 Indent the statements for clarity
 Can have multiple statements
 Enclose a ‘block’ of statements using { } (curly braces)
if( x <= 2 )
{
statement1;
statement2;
}
statement3;
IF statement example
 Pseudocode (notice indentation!)
If speed is greater than 65 mph
print “You’re speeding!”
 C code
if(speed > 65)
printf(“You’re speeding!n”);
 C code with multiple statement block
if(speed > 65)
/* statements below executed only if speed > 65 is true */
{
printf(“You’re speeding!n”);
printf(“Slow down!n”);
printf(“Keep speed below 65 MPHn”);
}
IF-ELSE statement - Double Selection
 Syntax
if(expression) /*if expression is TRUE (i.e., NOT EQUAL to zero) */
statement1; /* execute this statement */
else /* else execute the following statement */
statement2;
Notes:
 If expression is non-zero, statement1 is executed, then the
program continues with the statement after statement2,
i.e., statement2 is skipped
 If expression is equal to zero, statement1 is skipped and
statement2 is executed, then the program continues with
the statement after statement2
IF-ELSE statement example
 Pseudocode (notice indentation!)
If speed is greater than 65 mph
print “Over speed limit!”
else
print “Within speed limit”
 C code
if(speed > 65)
printf(“Over speed limit!n”);
else
printf(“Within limitn”);
Compound Condition - &&
 Logical operators for more complex
decisions
 Logical AND operator && (double ampersand)
 if(switch1 = = 0 && switch2 = = 1)
turn on the motor
 The condition evaluates to TRUE if and only if BOTH
expressions on either side of && evaluate to TRUE
 Note operator precedence
 Otherwise condition evaluates to FALSE
 Beware of ‘short circuit evaluation’
 Make the condition most likely to be FALSE the left-
most condition
Compound Condition - | |
 Logical operators for more complex
decisions, cont.
 Logical OR operator | | (double vertical bar)
 if(switch1 = = 0 || switch2 = = 1)
turn on the motor
 The condition evaluates to TRUE if one or the other
or both expressions on either side of && evaluate to
TRUE
 Note operator precedence
 Otherwise condition evaluates to FALSE
 Beware of ‘short circuit evaluation’
 Make the condition most likely to be TRUE the left-most
condition
Grade Determination for Overall Percentage (OP)
OP >= 90 ‘A’
80 <= OP < 90 ‘B’
70 <= OP < 80 ‘C’
60 <= OP < 70 ‘D’
OP < 60 ‘F’
Nesting selection structures
 Selection structures can
be stacked and nested
to handle more
sophisticated
decision/action
functionality
 Ex. Figuring grades
 Pseudocode 
Notes:
 “an else is always
associated with the
nearest previous if”
(Darnell & Margolis, 1996)
 Use braces ({ })to clarify
the association of the
else for other situations
where the decision
structure is more
complicated
Adapted from Deitel & Deitel, C How to Program, 3rd
ed., p.
64
Nesting If/else – C Code – Two Ways
Or
Adapted from Deitel & Deitel, C How to Program, 3rd
ed., p.
64
SWITCH
 Good when faced with
testing multiple
alternatives that depend
on a single variable
 The test is done once
 Must be an integral
expression
 int or char
 NOT float, double
 case items must be
constant integral
expressions
 No variables
 The structure is very
organized and readable
Adapted from Deitel & Deitel, C How to Program, 6th
ed., p. 111
SWITCH - Flowchart
Practice - 1
 Pair up with someone next to you that you do
not know
 Develop an algorithm for:
 Finding and printing out the largest of two numbers
 (3 min) One person work on the pseudocode,
the other on a flowchart
 (1 min) Compare pseudocode and flowchart
 (3 min) Write out the algorithm in C
Practice - 2
 Develop an algorithm for the ignition
control in a car:
Requirements:
 The starter will only start when:
 Key must be in the ignition slot
 Transmission selector must be in ‘Park’
 Key must be turned to ‘Start’ position
 The starter is energized with the statement
starter_on();
References
 Darnell, P. A. & Margolis, P. E. (1996) C, a
software engineering approach, 3rd ed.,
Springer, New York.
 Cheng, H. H. (2010). C for Engineers and
Scientists: An Interpretive Approach,
McGraw-Hill, New York.
 Deitel, H. M. & Deitel, P. J. (2001). C How
to Program, 3rd ed., Prentice-Hall, New
Jersey.
Nesting selection structures
 Selection
structures can be
stacked and nested
to handle more
sophisticated
decision/action
functionality
/* File: ifc.c */
#include <stdio.h>
int main ()
{
int i;
i = 10;
if(i==2 || i == 4)
{
printf("i = 2 or 4n");
}
else if(i == 10)
{
printf("i = 10n");
}
else
{
printf("i = %dn", i);
}
return 0;
}
Adapted from H. Cheng chap05.ppt, slide 12
Operators Operations Associativity
::
() [] left to right
Function_name() right to left
. -> left to right
‘ ! ` ++ -- + - *
&(type) sizeof
right to left
* / % .* ./ left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
&& left to right
^^ left to right
|| left to right
?: right to left
= += -= *= /= %= |=
<<= >>=
right to left
, left to right
Adapted from H. Cheng chap04.ppt, slide 5
For More Info Visit :
www.techora.net

More Related Content

PPTX
Control Flow Statements
Tarun Sharma
 
PPTX
Java if else condition - powerpoint persentation
Maneesha Caldera
 
PDF
10. switch case
Way2itech
 
PPTX
Input and Output In C Language
Adnan Khan
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
Operators in java
Then Murugeshwari
 
PPTX
Programming in c Arrays
janani thirupathi
 
Control Flow Statements
Tarun Sharma
 
Java if else condition - powerpoint persentation
Maneesha Caldera
 
10. switch case
Way2itech
 
Input and Output In C Language
Adnan Khan
 
Control statements in c
Sathish Narayanan
 
C Programming: Control Structure
Sokngim Sa
 
Operators in java
Then Murugeshwari
 
Programming in c Arrays
janani thirupathi
 

What's hot (20)

PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
Unification and Lifting
Megha Sharma
 
PPTX
Loops c++
Shivani Singh
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
Operators in c programming
savitamhaske
 
PPTX
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
PDF
Java conditional statements
Kuppusamy P
 
PPT
Variables in C Programming
programming9
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Operator.ppt
Darshan Patel
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPTX
Branching statements
ArunMK17
 
PPTX
Presentation on-exception-handling
Nahian Ahmed
 
PDF
Datatypes in python
eShikshak
 
DOC
CS8391 Data Structures Part B Questions Anna University
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPTX
Selection Statements in C Programming
Kamal Acharya
 
PPTX
C++ IF STATMENT AND ITS TYPE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
PPTX
Programming in C Presentation upto FILE
Dipta Saha
 
PPTX
Variable, constant, operators and control statement
Eyelean xilef
 
PPT
Basics of c++ Programming Language
Ahmad Idrees
 
Java exception handling
BHUVIJAYAVELU
 
Unification and Lifting
Megha Sharma
 
Loops c++
Shivani Singh
 
Methods in Java
Jussi Pohjolainen
 
Operators in c programming
savitamhaske
 
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
Java conditional statements
Kuppusamy P
 
Variables in C Programming
programming9
 
Functions in c
sunila tharagaturi
 
Operator.ppt
Darshan Patel
 
Loops and conditional statements
Saad Sheikh
 
Branching statements
ArunMK17
 
Presentation on-exception-handling
Nahian Ahmed
 
Datatypes in python
eShikshak
 
CS8391 Data Structures Part B Questions Anna University
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Selection Statements in C Programming
Kamal Acharya
 
Programming in C Presentation upto FILE
Dipta Saha
 
Variable, constant, operators and control statement
Eyelean xilef
 
Basics of c++ Programming Language
Ahmad Idrees
 
Ad

Similar to The Three Basic Selection Structures in C++ Programming Concepts (20)

PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
PPT
3. control statements
amar kakde
 
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
PDF
Chap 5 c++
Widad Jamaluddin
 
PPT
Control statments in c
CGC Technical campus,Mohali
 
PPTX
Ch05-converted.pptx
ShivamChaturvedi67
 
PPTX
C Programming Language Step by Step Part 5
Rumman Ansari
 
PPTX
C Programming Language Part 5
Rumman Ansari
 
PPTX
Control Structures.pptx
ssuserfb3c3e
 
PPT
03a control structures
Manzoor ALam
 
PPT
Ch3.1
aamirsahito
 
PDF
Selection & Making Decisions in c
yndaravind
 
PPTX
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
PPT
Control structures in C++ Programming Language
Ahmad Idrees
 
PDF
Ch05.pdf
ShivamChaturvedi67
 
PPTX
BLM101_2.pptx
ssuser4fbfbf
 
PPTX
C Unit-2.ppt
Giri383500
 
PPTX
Control Structures, If..else, switch..case.pptx
doncreiz1
 
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
3. control statements
amar kakde
 
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
Chap 5 c++
Widad Jamaluddin
 
Control statments in c
CGC Technical campus,Mohali
 
Ch05-converted.pptx
ShivamChaturvedi67
 
C Programming Language Step by Step Part 5
Rumman Ansari
 
C Programming Language Part 5
Rumman Ansari
 
Control Structures.pptx
ssuserfb3c3e
 
03a control structures
Manzoor ALam
 
Selection & Making Decisions in c
yndaravind
 
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
Control structures in C++ Programming Language
Ahmad Idrees
 
BLM101_2.pptx
ssuser4fbfbf
 
C Unit-2.ppt
Giri383500
 
Control Structures, If..else, switch..case.pptx
doncreiz1
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Ad

Recently uploaded (20)

PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
PPTX
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PPTX
AIRLINE PRICE API | FLIGHT API COST |
philipnathen82
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
Winning Business in a Slowing Economy, How CPQ helps Manufacturers Protect Ma...
systemscincom
 
PPT
Overview of Oracle Receivables Process.ppt
nbvreddy229
 
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Rise With SAP partner in Mumbai.........
pts464036
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
devilbrown689
 
Hire Expert Blazor Developers | Scalable Solutions by OnestopDA
OnestopDA
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
AIRLINE PRICE API | FLIGHT API COST |
philipnathen82
 
Exploring AI Agents in Process Industries
amoreira6
 
Winning Business in a Slowing Economy, How CPQ helps Manufacturers Protect Ma...
systemscincom
 
Overview of Oracle Receivables Process.ppt
nbvreddy229
 
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Rise With SAP partner in Mumbai.........
pts464036
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Emergency Mustering solutions – A Brief overview
Personnel Tracking
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 

The Three Basic Selection Structures in C++ Programming Concepts

  • 1. Lecture 3: Control Structures - Selection Author : Aamir Saleem Ansari Web : www.techora.net
  • 2. The Plan for Today  Review from last two weeks  Flowcharts  Pseudocode  Data types  Variables and Constants  Structure of a C program  Formatted output: printf( )  Operators and their precedence  Review control structures  Sequence  Selection  Repetition  Selection structures  If  If/else  Switch  Relational operators  Selection structure example
  • 3. Learning Objectives  Apply concepts for developing algorithms, using variables, and structuring a C program  Explain what is meant by a control structure  Explain the three basic types of control structures  Determine the result of relational comparisons  Apply the if and if/else control structures
  • 4. Control Structures - Review  All programs can be written in terms of three control structures (like building blocks)  Sequence  ‘Built-in’ to C  Unless otherwise directed, one statement after the next is executed  Selection (three types)  Depending on a condition, select between one statement or another  If var1 is greater than 10, do this…, else do that…  Repetition (three types)  Depending on a condition, execute one or more statements repeatedly
  • 5. Selection Structure Overview  Three kinds of selections structures  if (also called, ‘single-selection’)  if condition is true Perform action  if condition is false, action is skipped, program continues  if/else (also called, ‘double-selection’)  if condition is true Perform action  else (if condition is false) Perform a different action (this will be skipped if condition is true)  switch (also called ‘multiple-selection’)  Allows selection among many actions depending on the integral value of a variable or expression
  • 6. Single Selection IF - Flowchart TRUE FALSE Speed > 65 connector flow line decision symbol action symbol Print “You’re speeding”
  • 7. Operations Associativity :: () [] left to right Function_name() right to left . -> left to right ‘ ! ` ++ -- + - * &(type) sizeof right to left * / % .* ./ left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right && left to right ^^ left to right || left to right ?: right to left = += -= *= /= %= |= <<= >>= right to left , left to right Relational Operators Important for constructing the decision expression 5 < 7 result is ____ 5 > 7 result is _____ 7 <= 7 result is ____ 8 >= 7 result is ____ 5 == 5 result is ____ 5 == 7 result is ____ var1 = 7 result is____ 5.0 == 5 result is ___ 6 != 5 result is ____ Adapted from H. Cheng chap04.ppt, slide 5 Practice
  • 8. Double-Selection IF - Flowchart TRUE Speed > 65 FALSE Print “Over speed limit” Print “Within speed limit”
  • 9. Adapted from Deitel & Deitel, C How to Program, 6th ed., p. 111 SWITCH - Flowchart
  • 10. IF statement (single-selection)  Syntax if(expression) /* if expression is TRUE (i.e., NOT EQUAL to zero) */ statement1; /* then execute this statement */ statement2; /* execute this statement next*/  Notes  Indent the statements for clarity  Can have multiple statements  Enclose a ‘block’ of statements using { } (curly braces) if( x <= 2 ) { statement1; statement2; } statement3;
  • 11. IF statement example  Pseudocode (notice indentation!) If speed is greater than 65 mph print “You’re speeding!”  C code if(speed > 65) printf(“You’re speeding!n”);  C code with multiple statement block if(speed > 65) /* statements below executed only if speed > 65 is true */ { printf(“You’re speeding!n”); printf(“Slow down!n”); printf(“Keep speed below 65 MPHn”); }
  • 12. IF-ELSE statement - Double Selection  Syntax if(expression) /*if expression is TRUE (i.e., NOT EQUAL to zero) */ statement1; /* execute this statement */ else /* else execute the following statement */ statement2; Notes:  If expression is non-zero, statement1 is executed, then the program continues with the statement after statement2, i.e., statement2 is skipped  If expression is equal to zero, statement1 is skipped and statement2 is executed, then the program continues with the statement after statement2
  • 13. IF-ELSE statement example  Pseudocode (notice indentation!) If speed is greater than 65 mph print “Over speed limit!” else print “Within speed limit”  C code if(speed > 65) printf(“Over speed limit!n”); else printf(“Within limitn”);
  • 14. Compound Condition - &&  Logical operators for more complex decisions  Logical AND operator && (double ampersand)  if(switch1 = = 0 && switch2 = = 1) turn on the motor  The condition evaluates to TRUE if and only if BOTH expressions on either side of && evaluate to TRUE  Note operator precedence  Otherwise condition evaluates to FALSE  Beware of ‘short circuit evaluation’  Make the condition most likely to be FALSE the left- most condition
  • 15. Compound Condition - | |  Logical operators for more complex decisions, cont.  Logical OR operator | | (double vertical bar)  if(switch1 = = 0 || switch2 = = 1) turn on the motor  The condition evaluates to TRUE if one or the other or both expressions on either side of && evaluate to TRUE  Note operator precedence  Otherwise condition evaluates to FALSE  Beware of ‘short circuit evaluation’  Make the condition most likely to be TRUE the left-most condition
  • 16. Grade Determination for Overall Percentage (OP) OP >= 90 ‘A’ 80 <= OP < 90 ‘B’ 70 <= OP < 80 ‘C’ 60 <= OP < 70 ‘D’ OP < 60 ‘F’ Nesting selection structures  Selection structures can be stacked and nested to handle more sophisticated decision/action functionality  Ex. Figuring grades  Pseudocode  Notes:  “an else is always associated with the nearest previous if” (Darnell & Margolis, 1996)  Use braces ({ })to clarify the association of the else for other situations where the decision structure is more complicated Adapted from Deitel & Deitel, C How to Program, 3rd ed., p. 64
  • 17. Nesting If/else – C Code – Two Ways Or Adapted from Deitel & Deitel, C How to Program, 3rd ed., p. 64
  • 18. SWITCH  Good when faced with testing multiple alternatives that depend on a single variable  The test is done once  Must be an integral expression  int or char  NOT float, double  case items must be constant integral expressions  No variables  The structure is very organized and readable
  • 19. Adapted from Deitel & Deitel, C How to Program, 6th ed., p. 111 SWITCH - Flowchart
  • 20. Practice - 1  Pair up with someone next to you that you do not know  Develop an algorithm for:  Finding and printing out the largest of two numbers  (3 min) One person work on the pseudocode, the other on a flowchart  (1 min) Compare pseudocode and flowchart  (3 min) Write out the algorithm in C
  • 21. Practice - 2  Develop an algorithm for the ignition control in a car: Requirements:  The starter will only start when:  Key must be in the ignition slot  Transmission selector must be in ‘Park’  Key must be turned to ‘Start’ position  The starter is energized with the statement starter_on();
  • 22. References  Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3rd ed., Springer, New York.  Cheng, H. H. (2010). C for Engineers and Scientists: An Interpretive Approach, McGraw-Hill, New York.  Deitel, H. M. & Deitel, P. J. (2001). C How to Program, 3rd ed., Prentice-Hall, New Jersey.
  • 23. Nesting selection structures  Selection structures can be stacked and nested to handle more sophisticated decision/action functionality /* File: ifc.c */ #include <stdio.h> int main () { int i; i = 10; if(i==2 || i == 4) { printf("i = 2 or 4n"); } else if(i == 10) { printf("i = 10n"); } else { printf("i = %dn", i); } return 0; } Adapted from H. Cheng chap05.ppt, slide 12
  • 24. Operators Operations Associativity :: () [] left to right Function_name() right to left . -> left to right ‘ ! ` ++ -- + - * &(type) sizeof right to left * / % .* ./ left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right && left to right ^^ left to right || left to right ?: right to left = += -= *= /= %= |= <<= >>= right to left , left to right Adapted from H. Cheng chap04.ppt, slide 5
  • 25. For More Info Visit : www.techora.net

Editor's Notes

  • #5: So far your programs have simply been a grouping of statements, which are executed one after the next from the beginning of the program to the end. This, ‘one after the next’ flow of a program is called a sequence structure. Such simple, sequential programs have their place, but there is much more that can be done by applying several other control structures to control the flow of which statements are executed. In this lecture, we are going to look into the first of the other two control structures: selection
  • #7: If the speed is greater than 65, take the action to print a message. If the speed is not greater than 65, just keep going on in the program. What does Speed &amp;gt; 65 evaluate to if Speed == 72 ?
  • #8: Discuss these
  • #9: Note that if Speed &amp;gt; 65 is true, then the message “Over speed limit is printed”, and then the program continues. If Speed &amp;gt; 65 is not true, then the message “Within limit” is printed.
  • #11: Note: an expression is an entity that evaluates to a single number. An expression is TRUE if it is non-zero. Example if(7), is the expression true or not? The block of statements is called a ‘compound’ statement. Note how the curly brackets are aligned, and how the statements in the block are indented, so that it makes it obvious where the block begins, where it ends, and which statements make up the block. Note comments. Two styles will work for most C compilers: /* */ the clean C version and // the C++ version
  • #13: Note that in the case of the single selection IF, there is no way without a goto, to skip over statements in the implied else. So in single-selection IF, if the decision expression evaluates to non-zero, the statement after the IF is executed, then the program resumes after the statement. If the decision expression evaluates to zero, then the statement immediately after the IF is skipped, and the program resumes with the next statement. With IF-ELSE, the ELSE acts like a shield to prevent the alternative statement(s) from being executed if the decision expression is non-zero. You are probably safer to always use IF-ELSE. For single-selection, just put a semicolon after the ELSE: If(expression) take this action ELSE don’t do anything (;)
  • #15: Note that evaluation of the condition continues until the truth or falsity of the condition is determined. Thus, if switch1 is not equal to zero, the compound condition is false, and the second relational test will not be done! Back to the driving example: IF speed is less than or equal to 65 AND greater than 60 print “You’re doing fine” ELSE IF speed is greater than 65 print, “You’re speeding” ELSE print, “You’re driving too slow” END IF END IF
  • #16: Note that evaluation of the condition continues until the truth or falsity of the condition is determined. Thus, if switch1 is not equal to zero, the compound condition is true, and the second relational test will not be done! See p. 118 in D&amp;D