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

What's hot

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Loops Basics
Loops BasicsLoops Basics
Loops BasicsMushiii
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiPriya Chauhan
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in JavaJin Castor
 
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
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
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++ Neeru Mittal
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationBadrul Alam
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 

What's hot (20)

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by Balaguruswami
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Iteration
IterationIteration
Iteration
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
 
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
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
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++
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Nested loops
Nested loopsNested loops
Nested loops
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 

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

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
03a control structures
03a   control structures03a   control structures
03a control structuresManzoor ALam
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
3. control statements
3. control statements3. control statements
3. control statementsamar kakde
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)Prashant Sharma
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrayssshhzap
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 

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

ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Lect 3-4 Zaheer Abbas
Lect 3-4 Zaheer AbbasLect 3-4 Zaheer Abbas
Lect 3-4 Zaheer Abbas
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
What is c
What is cWhat is c
What is c
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
3. control statements
3. control statements3. control statements
3. control statements
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 

Recently uploaded

Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 

Recently uploaded (20)

Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 

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

  1. 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
  2. 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 ?
  3. Discuss these
  4. 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.
  5. 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
  6. 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 (;)
  7. 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
  8. 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