SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Control Structures
OBJECTIVES
• In this Lecture you will learn how to:
– Make decisions
• Using :
– simple if
– If…else statement
– If…else if statement
– Use logical operators
2
Intro to Programming with C++
Sir Noble
Program Control and Control Structures
– An algorithm is a procedure for solving a
problem in terms of
• the actions to execute and
• the order in which the actions execute
– Program control
• Specifying the order in which statements (actions)
execute.
– Control Structures
• Simple control statements
3
Intro to Programming with C++
Sir Noble
The Simple if Statement
• Allows statements to be conditionally executed or
skipped over
• Models the way we mentally evaluate situations:
– "If I do not have the minimum CGPA required, I will not
graduate."
– "If I have codeblocks installed on my machine, I will be able
to write and debug simple c++ programs."
• If condition is true, body of the if statement
executes
• If condition is false, body of the if statement does
not execute
4
Intro to Programming with C++
Sir Noble
The Logical flow of Simple If
Decision
5
Intro to Programming with C++
Sir Noble
Decision Making: Equality and Relational
Operators
• The if Statement
– Syntax
– Boolean expression is any C++
expression that can be evaluated
– Statement is any C++ statement or block
of statements that you want to execute
when the boolean expression evaluates
as true.
6
if ( boolean expression )
Statement;
Intro to Programming with C++
Sir Noble
7
Simpe If Statement
Intro to Programming with C++
Sir Noble
8
Example of Simpe If Statement
Intro to Programming with C++
Sir Noble
9
Example of Simpe If Statement
Intro to Programming with C++
Sir Noble
Expanding the if Statement
• To execute more than one statement as part of an if
statement, enclose them in curly braces{ }:
if (gpa >= 3.75)
{
cout << “ GPA is ”<< << gpa << endl;
cout <<“Congrats! You are a first class
student” << endl;
}
• { } creates a block of code
10
Intro to Programming with C++
Sir Noble
Nested if Statements
• An if statement that is nested inside another
if statement
• Nested if statements can be used to test more
than one condition
11
Intro to Programming with C++
Sir Noble
Nested if Statements - Example
12
Intro to Programming with C++
Sir Noble
The if/else statement
• Provides two possible paths of execution
• Performs one statement or block if the
expression is true, otherwise performs
another statement or block.
13
Intro to Programming with C++
Sir Noble
The if/else statement
• General Format:
if (expression)
statement1; // or block
else
statement2; // or block
14
True
False
Intro to Programming with C++
Sir Noble
if/else-What Happens
To evaluate:
if (expression)
statement1;
else
statement2;
• If the expression is true, then statement1 is executed
and statement2 is skipped.
• If the expression is false, then statement1 is skipped
and statement2 is executed.
• For example, consider this pseudocode
if student’s grade is greater than or equal to 40
Print “Passed”
else
Print “Failed”
15
Intro to Programming with C++
Sir Noble
16
Example If - else Statement
Intro to Programming with C++
Sir Noble
Blocks
• Blocks/Compound Statement
– Set of statements within a pair of braces
– Used to include multiple statements in an if body
– Example
– Without braces
cout << "You must take this course again" << endl;
always executes
17
if ( studentGrade >= 60 )
cout << "Passed“ << endl;
else
{
cout << "Failed“ << endl;
cout << "You must take this course
again“ << endl;
}
Intro to Programming with C++
Sir Noble
The if/else if Statement
• Tests a series of conditions until one is found to
be true
• Often simpler than using nested if/else
statements
• Can be used to model thought processes such as:
If I have ‘chaw’ money, I will buy pizza,
else, if it is not ‘chaw’, I will buy kenkey and fish,
else, no ‘chop chop’
18
Intro to Programming with C++
Sir Noble
if/else if Format
19
Intro to Programming with C++
Sir Noble
The if/else if Statement in
Program
20
Intro to Programming with C++
Sir Noble
Logical Operators
• Logical operators
– Allows for more complex conditions
• Combines simple conditions into complex conditions
• Used to create relational expressions from other relational
expressions
• C++ logical operators
– && (logical AND)
– || (logical OR)
– ! (logical NOT)
21
Intro to Programming with C++
Sir Noble
Logical Operators
• Logical AND (&&) Operator
– It takes two expressions as operands and creates an
expression that is true only when both sub expressions
are true.
– Consider the following if statement
if ( temperature < 20 && minutes > 12 )
cout << “The temperature is in the danger
zone” << endl;
• Combined condition is true
– If and only if both simple conditions are true
• Combined condition is false
– If either or both of the simple conditions are false
22
Intro to Programming with C++
Sir Noble
23
You must provide complete expressions on both sides of the &&
operator. For example, the following is not correct because the condition
on the right side of the && operator is not a complete expression.
temperature > 0 && < 100
The expression must be rewritten as
temperature > 0 && temperature < 100
Intro to Programming with C++
Sir Noble
Logical Operators
24
expression1 expression2 expression1 && expression2
false false false
false true false
true false false
true true true
Intro to Programming with C++
Sir Noble
Logical Operators
• Logical OR (||) Operator
– It takes two expressions as operands and creates an
expression that is true when either of the sub-
expressions are true.
– Consider the following if statement
if ( temperature < 20 || temperature > 100 )
cout << “The temperature is in the danger zone” << endl;
• Combined condition is true
– If either or both of the simple conditions are true
• Combined condition is false
– If both of the simple conditions are false
25
Intro to Programming with C++
Sir Noble
Logical Operators
26
expression1 expression2 expression1 || expression2
false false false
false true true
true false true
true true true
Intro to Programming with C++
Sir Noble
The logical || Operator in
Program
27
Intro to Programming with C++
Sir Noble
The logical || Operator in
Program
28
Intro to Programming with C++
Sir Noble
Logical Operators
• Logical NOT (!) Operator
– It takes an operand and reverses its truth or
falsehood.
– If the expression is true, the ! Operator returns false
and if the expression is false, it returns true
– Consider the following if statement
if ( !(temperature > 100) )
cout << “You are below the maximum temperature” << endl;
29
Intro to Programming with C++
Sir Noble
Logical Operators-Examples
int x = 5, y = -7, z = 9;
(x > y) && (y > z) True/False
(x > y) && (z > y) True/False
(x <= z) || (y == z) True/False
(x <= z) || (y != z) True/False
!(x >= z) True/False
30
Intro to Programming with C++
Sir Noble
Exercise – if-else…if-else
31
• A program that calculates the charges for
membership in a Sanhedrin Gym. The gym has three
membership packages to choose from: bronze
membership, silver membership and gold
membership. The program presents a menu that
allows the user to choose the desired package and
then calculates the cost of the membership.
Membership rates
Bronze membership: GHC 30.00 per month
Silver membership: GHC 40.00 per month
Gold membership: GHC 50.00 per month
Intro to Programming with C++
Sir Noble

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
C++ exception handling
C++ exception handlingC++ exception handling
C++ exception handling
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Ch4 Expressions
Ch4 ExpressionsCh4 Expressions
Ch4 Expressions
 
2nd presantation
2nd presantation2nd presantation
2nd presantation
 
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsNotes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
 
Bakery algorithm in operating system
Bakery algorithm in operating systemBakery algorithm in operating system
Bakery algorithm in operating system
 
Loop c++
Loop c++Loop c++
Loop c++
 
Bakery algorithm
Bakery algorithmBakery algorithm
Bakery algorithm
 
Behavioral modelling in VHDL
Behavioral modelling in VHDLBehavioral modelling in VHDL
Behavioral modelling in VHDL
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
C++ loop
C++ loop C++ loop
C++ loop
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Path Testing
Path TestingPath Testing
Path Testing
 

Andere mochten auch

Element of programming assignment 1 gideon
Element of programming assignment 1 gideonElement of programming assignment 1 gideon
Element of programming assignment 1 gideonTony Apreku
 
Lecture 1 uml with java implementation
Lecture 1 uml with java implementationLecture 1 uml with java implementation
Lecture 1 uml with java implementationthe_wumberlog
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variablesTony Apreku
 
Two Step Equations Jeopardy
Two Step Equations JeopardyTwo Step Equations Jeopardy
Two Step Equations Jeopardykwest
 
Time series-mining-slides
Time series-mining-slidesTime series-mining-slides
Time series-mining-slidesYanchang Zhao
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingHaris Bin Zahid
 
Ruby Programming Language - Introduction
Ruby Programming Language - IntroductionRuby Programming Language - Introduction
Ruby Programming Language - IntroductionKwangshin Oh
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby BasicsSHC
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course IntroductionIntro C# Book
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 

Andere mochten auch (13)

C++ for beginners
C++ for beginnersC++ for beginners
C++ for beginners
 
Element of programming assignment 1 gideon
Element of programming assignment 1 gideonElement of programming assignment 1 gideon
Element of programming assignment 1 gideon
 
Lecture 1 uml with java implementation
Lecture 1 uml with java implementationLecture 1 uml with java implementation
Lecture 1 uml with java implementation
 
Lecture 1 oop
Lecture 1 oopLecture 1 oop
Lecture 1 oop
 
UML constructs
UML constructs UML constructs
UML constructs
 
Lecture 2 variables
Lecture 2 variablesLecture 2 variables
Lecture 2 variables
 
Two Step Equations Jeopardy
Two Step Equations JeopardyTwo Step Equations Jeopardy
Two Step Equations Jeopardy
 
Time series-mining-slides
Time series-mining-slidesTime series-mining-slides
Time series-mining-slides
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Ruby Programming Language - Introduction
Ruby Programming Language - IntroductionRuby Programming Language - Introduction
Ruby Programming Language - Introduction
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course Introduction
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 

Ähnlich wie Lecture 3 control_structures_i

Ähnlich wie Lecture 3 control_structures_i (20)

Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Programming note C#
Programming note C#Programming note C#
Programming note C#
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
White boxvsblackbox
White boxvsblackboxWhite boxvsblackbox
White boxvsblackbox
 
Control structures c2 c3
Control structures c2 c3Control structures c2 c3
Control structures c2 c3
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Chaptfffffuuer05.PPT
Chaptfffffuuer05.PPTChaptfffffuuer05.PPT
Chaptfffffuuer05.PPT
 
Selection
SelectionSelection
Selection
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 

Lecture 3 control_structures_i

  • 2. OBJECTIVES • In this Lecture you will learn how to: – Make decisions • Using : – simple if – If…else statement – If…else if statement – Use logical operators 2 Intro to Programming with C++ Sir Noble
  • 3. Program Control and Control Structures – An algorithm is a procedure for solving a problem in terms of • the actions to execute and • the order in which the actions execute – Program control • Specifying the order in which statements (actions) execute. – Control Structures • Simple control statements 3 Intro to Programming with C++ Sir Noble
  • 4. The Simple if Statement • Allows statements to be conditionally executed or skipped over • Models the way we mentally evaluate situations: – "If I do not have the minimum CGPA required, I will not graduate." – "If I have codeblocks installed on my machine, I will be able to write and debug simple c++ programs." • If condition is true, body of the if statement executes • If condition is false, body of the if statement does not execute 4 Intro to Programming with C++ Sir Noble
  • 5. The Logical flow of Simple If Decision 5 Intro to Programming with C++ Sir Noble
  • 6. Decision Making: Equality and Relational Operators • The if Statement – Syntax – Boolean expression is any C++ expression that can be evaluated – Statement is any C++ statement or block of statements that you want to execute when the boolean expression evaluates as true. 6 if ( boolean expression ) Statement; Intro to Programming with C++ Sir Noble
  • 7. 7 Simpe If Statement Intro to Programming with C++ Sir Noble
  • 8. 8 Example of Simpe If Statement Intro to Programming with C++ Sir Noble
  • 9. 9 Example of Simpe If Statement Intro to Programming with C++ Sir Noble
  • 10. Expanding the if Statement • To execute more than one statement as part of an if statement, enclose them in curly braces{ }: if (gpa >= 3.75) { cout << “ GPA is ”<< << gpa << endl; cout <<“Congrats! You are a first class student” << endl; } • { } creates a block of code 10 Intro to Programming with C++ Sir Noble
  • 11. Nested if Statements • An if statement that is nested inside another if statement • Nested if statements can be used to test more than one condition 11 Intro to Programming with C++ Sir Noble
  • 12. Nested if Statements - Example 12 Intro to Programming with C++ Sir Noble
  • 13. The if/else statement • Provides two possible paths of execution • Performs one statement or block if the expression is true, otherwise performs another statement or block. 13 Intro to Programming with C++ Sir Noble
  • 14. The if/else statement • General Format: if (expression) statement1; // or block else statement2; // or block 14 True False Intro to Programming with C++ Sir Noble
  • 15. if/else-What Happens To evaluate: if (expression) statement1; else statement2; • If the expression is true, then statement1 is executed and statement2 is skipped. • If the expression is false, then statement1 is skipped and statement2 is executed. • For example, consider this pseudocode if student’s grade is greater than or equal to 40 Print “Passed” else Print “Failed” 15 Intro to Programming with C++ Sir Noble
  • 16. 16 Example If - else Statement Intro to Programming with C++ Sir Noble
  • 17. Blocks • Blocks/Compound Statement – Set of statements within a pair of braces – Used to include multiple statements in an if body – Example – Without braces cout << "You must take this course again" << endl; always executes 17 if ( studentGrade >= 60 ) cout << "Passed“ << endl; else { cout << "Failed“ << endl; cout << "You must take this course again“ << endl; } Intro to Programming with C++ Sir Noble
  • 18. The if/else if Statement • Tests a series of conditions until one is found to be true • Often simpler than using nested if/else statements • Can be used to model thought processes such as: If I have ‘chaw’ money, I will buy pizza, else, if it is not ‘chaw’, I will buy kenkey and fish, else, no ‘chop chop’ 18 Intro to Programming with C++ Sir Noble
  • 19. if/else if Format 19 Intro to Programming with C++ Sir Noble
  • 20. The if/else if Statement in Program 20 Intro to Programming with C++ Sir Noble
  • 21. Logical Operators • Logical operators – Allows for more complex conditions • Combines simple conditions into complex conditions • Used to create relational expressions from other relational expressions • C++ logical operators – && (logical AND) – || (logical OR) – ! (logical NOT) 21 Intro to Programming with C++ Sir Noble
  • 22. Logical Operators • Logical AND (&&) Operator – It takes two expressions as operands and creates an expression that is true only when both sub expressions are true. – Consider the following if statement if ( temperature < 20 && minutes > 12 ) cout << “The temperature is in the danger zone” << endl; • Combined condition is true – If and only if both simple conditions are true • Combined condition is false – If either or both of the simple conditions are false 22 Intro to Programming with C++ Sir Noble
  • 23. 23 You must provide complete expressions on both sides of the && operator. For example, the following is not correct because the condition on the right side of the && operator is not a complete expression. temperature > 0 && < 100 The expression must be rewritten as temperature > 0 && temperature < 100 Intro to Programming with C++ Sir Noble
  • 24. Logical Operators 24 expression1 expression2 expression1 && expression2 false false false false true false true false false true true true Intro to Programming with C++ Sir Noble
  • 25. Logical Operators • Logical OR (||) Operator – It takes two expressions as operands and creates an expression that is true when either of the sub- expressions are true. – Consider the following if statement if ( temperature < 20 || temperature > 100 ) cout << “The temperature is in the danger zone” << endl; • Combined condition is true – If either or both of the simple conditions are true • Combined condition is false – If both of the simple conditions are false 25 Intro to Programming with C++ Sir Noble
  • 26. Logical Operators 26 expression1 expression2 expression1 || expression2 false false false false true true true false true true true true Intro to Programming with C++ Sir Noble
  • 27. The logical || Operator in Program 27 Intro to Programming with C++ Sir Noble
  • 28. The logical || Operator in Program 28 Intro to Programming with C++ Sir Noble
  • 29. Logical Operators • Logical NOT (!) Operator – It takes an operand and reverses its truth or falsehood. – If the expression is true, the ! Operator returns false and if the expression is false, it returns true – Consider the following if statement if ( !(temperature > 100) ) cout << “You are below the maximum temperature” << endl; 29 Intro to Programming with C++ Sir Noble
  • 30. Logical Operators-Examples int x = 5, y = -7, z = 9; (x > y) && (y > z) True/False (x > y) && (z > y) True/False (x <= z) || (y == z) True/False (x <= z) || (y != z) True/False !(x >= z) True/False 30 Intro to Programming with C++ Sir Noble
  • 31. Exercise – if-else…if-else 31 • A program that calculates the charges for membership in a Sanhedrin Gym. The gym has three membership packages to choose from: bronze membership, silver membership and gold membership. The program presents a menu that allows the user to choose the desired package and then calculates the cost of the membership. Membership rates Bronze membership: GHC 30.00 per month Silver membership: GHC 40.00 per month Gold membership: GHC 50.00 per month Intro to Programming with C++ Sir Noble

Hinweis der Redaktion

  1. The trailing else clause is optional, but it is best used to catch errors.