CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C++ for Engineers and Scientists Third Edition
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming Logic and Design, Second Edition, Comprehensive
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Flow of Control Part 1: Selection
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Control statements Mostafa Abdallah
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
The Ohio State University
Chapter 4 – C Program Control
More on the Selection Structure
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 6: Conditional Statements and Loops
Topics The if Statement The if-else Statement Comparing Strings
Pages:51-59 Section: Control1 : decisions
Selection Statements.
Boolean Expressions to Make Comparisons
CHAPTER 4: Conditional Structures
The Selection Structure
Pages:51-59 Section: Control1 : decisions
Lecture 9: Implementing Complex Logic
Presentation transcript:

CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Flow of Execution  Every algorithm has a logic flow  There is a start, steps that happen in chronological order, and an end  There is a graphical way to describe program flow  Understanding control flow is essential to creating and testing an implementation of an algorithm Figure 4.1: This chart has a one directional flow (each step is performed once before the next), but some algorithms can have multiple possible execution flows (c) 2012 Ophir Frieder et al

Conditional flow: a certain condition must be met to perform the next step After doing the first step, follow the path that matches the given condition, then the rest of the flow is one directional Flow of Execution: Multiple Path Logic Flow (Figure 4.2) Decision Step (c) 2012 Ophir Frieder et al

Conditional Control  A condition is an expression defined using relational and Boolean operators  A condition has a Boolean value, either True or False irb(main):001:0> 5 == 5 => true irb(main):002:0> 5 == 6 => false irb(main):003:0> 5 <= 5 => true irb(main):004:0> 5 != 5 => false (c) 2012 Ophir Frieder et al

Relational Operators (Table 4.1) Relational OperatorsMeaning ==Is equal to !=Not equal to <Less than >Greater than <=Less than or equal to >=Greater than of equal to (c) 2012 Ophir Frieder et al

Conditional Control  The “!” operator is the negation of a condition or Boolean value  “!” can work on any true or false statement or conditional  Usually referred to as “not”  Boolean operators operate on Boolean values, creating expressions that evaluate to True or False  Operators include: and, or, not  The results of the operators are described by truth tables (c) 2012 Ophir Frieder et al

Truth Tables for “and” and “or” (Table 4.2) ABA and BA or B A && BA || B true false true falsetruefalsetrue false (c) 2012 Ophir Frieder et al

Example: Boolean Expressions irb(main) :001:0> !false =>true irb(main) :002:0> !(true or false) => false irb(main) :003:0> first = true => true irb(main) :004:0> second = false => false irb(main) :005:0> (first and second or !(first and second) => true (c) 2012 Ophir Frieder et al

Conditional Flow: If Statements  Ruby uses an if statement for basic conditional control flow (Example 4.3) 1 if (condition) 2 # section 1 3 end  Input value of 11 (Example 4.4): 1 # if a number is even, print out "Even" 2 puts "Enter a number” 3 number = gets.to_i 4 if (number % 2 == 0) # evaluates to false 5 puts "Even” # does not execute 6 end Section 1 is executed when the condition evaluates to true or is skipped when the condition evaluates to false (c) 2012 Ophir Frieder et al

Conditional Flow: If-Then-Else Statements  Provides a second flow option  If the original condition is not met, then the second flow option is taken (Example 4.5) 1 if (condition) 2 # section 1 executes if true 3 else 4 # section 2 executes if false 5 end (c) 2012 Ophir Frieder et al

Example of Program that Determines Prices of Movie Tickets (Example 4.6) 1 puts "Enter the customer's age: “ 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 if (age < 12) 7 cost = 9 8 else 9 cost = end # Print out the final cost 13 puts "Ticket cost: " + cost.to_s (c) 2012 Ophir Frieder et al

If-Else Statement Logic Flow  To test the program, input one value for each logic flow option  Test the edge or boundary conditions (most errors occur here) Figure 4.4 (c) 2012 Ophir Frieder et al

Movie Ticket Example: Input Value of 8 (Example 4.6) 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 if (age < 12) # evaluates to true 7 cost = 9 # so the If portion Executes 8 else 9 cost = 18 # This portion DOES NOT 10 end # Print out the final cost 13 puts "Ticket cost: " + cost.to_s (c) 2012 Ophir Frieder et al

Movie Ticket Example: Input Value of 25 (Example 4.6) 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 if (age < 12)# Evaluates to false 7 cost = 9 # This DOES NOT execute 8 else 9 cost = 18 # Executes 10 end # Print out the final cost 13 puts "Ticket cost: “ + cost.to_s (c) 2012 Ophir Frieder et al

Movie Ticket Example: Input Value of 12 (Figure 4.9) 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 if (age < 12) # Evaluates to false 7 cost = 9 8 else 9 cost = 18 # Executes 10 end # Print out the final cost 13 puts "Ticket cost: " + cost.to_s The correct outcome should be 9 because a child is considered 12 or under, so the program is incorrect. To correct the error (“bug”), the conditional test in the program needs to be “ age <= 12 ” (c) 2012 Ophir Frieder et al

Elsif Statements  Conditional flow can have more than two flow options  There are various ways to implement a multi-flow control  One of them is using an elsif statement (c) 2012 Ophir Frieder et al

Only the first condition that evaluates to true gets executed Elsif Statement Logic Flow (Figure 4.5) (c) 2012 Ophir Frieder et al

Program that Discounts Tickets for Children & Senior Citizens (Example 4.9) 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 if (age <= 12) 7 cost = 9 8 elsif (age >= 65) 9 cost = else 11 cost = end # Print out the final cost 15 puts "Ticket cost: " + cost.to_s Note: The program needs another condition for senior citizens (c) 2012 Ophir Frieder et al

Review: Original Movie Ticket Program (Example 4.6) 1 puts “Enter the customer's age:” 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 if (age <= 12) 7 cost = 9 8 else 9 cost = end # Print out the final cost 13 puts "Ticket cost: " + cost.to_s (c) 2012 Ophir Frieder et al

Alternatives 1 puts "Enter the customer's age:” 2 # Get an integer age value from the user 3 age = gets.to_i 4 cost = 18 5 # Determine the cost based on age 6 if (age <= 12)then cost = 9 end 7 # Print out the final cost 8 puts "Ticket cost: " + cost.to_s 1 puts "Enter the customer's age:” 2 # Get an integer age value from the user 3 age = gets.to_i 5 # Determine the cost based on age 6 if (age<=12)then (cost=9)else (cost = 18 ) end 12 # Print out the final cost 13 puts "Ticket cost: " + cost.to_s Alternative SyntaxAlternative Program SYNTACTIC SUGAR IN ACTION: Alternative syntax designed for ease of programming and readability (c) 2012 Ophir Frieder et al

Case Statements  The case statement handles multiple options  Alternative to if-elsif statements  Useful for a large number of options  Case statements evaluate in order  Only the first when clause that evaluates to true gets executed  If none evaluates to true, then the else clause is executed Figure 4.11: 1 case 2 when (expression1) 3 # section 1 4 when (expression2) 5 # section 2 6 else 7 # section 3 8 end (c) 2012 Ophir Frieder et al

Movie Ticket Program: Rewritten Using a Case Statement (Example 4.12) 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 case 7 when (age <= 12) 8 cost = 9 9 when (age >= 65) 10 cost = else 12 cost = end # Print out the final cost 16 puts "Ticket cost: " + cost.to_s (c) 2012 Ophir Frieder et al

Debugging: Incorrect Movie Ticket Program (Example 4.13) Example 1: The cost will always be 9 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 5 # Determine the cost based on age 6 case 7 8 when (age = 12) then # Always evaluates to true 9 cost = 9 10 when (age >= 65) then 11 cost = else 13 cost = end # Print out the final cost 17 puts "Ticket cost: " + cost.to_s # '=' is assignment NOT equality test '==' (c) 2012 Ophir Frieder et al

Debugging: Incorrect Movie Ticket Program (Example 4.14) Example 2: 1 puts "Enter the customer's age: " 2 # Get an integer age value from the user 3 age = gets.to_i 4 # DEBUG 5 puts age 6 7 # Determine the cost based on age 8 case 9 # '=' is assignment NOT equality test '==' 10 when (age = 12) then 11 cost = 9 12 when (age >= 65) then 13 cost = else 15 cost = end (c) 2012 Ophir Frieder et al

Debugging  Uses puts statements to help identify errors.  Show variable values where they are not changing Example 4.14 cont’d: 17 # DEBUG 18 puts age # Shows that age always equals # Print out the final cost 21 puts "Ticket cost: " + cost.to_s (c) 2012 Ophir Frieder et al

Debugging: Alternatives  Programs can also be debugged using constants  In each section, there is an if statement with a debugging constant as the flag  The flag determines whether a put statement is executed  When a section is judged to be correct, the constant is set to false  There is no need to check variables  The debug output should be fully descriptive  puts “debug – age” + age.to_s  NOT puts “age” (c) 2012 Ophir Frieder et al

Debugging (Example 4.15) 1 # Flag for debugging (change the false when finished debugging) 2 DEBUG_MODULE_1 = true # Initialize and define a flag constant as true. 3 4 puts "Enter the customer's age: " 5 # Get an integer age value from the user 6 age = gets.to_i 7 8 # Determine the cost based on age 9 if DEBUG_MODULE_1 # Changed to false if this section is correct 10 puts age # Prints age if the section is still # not debugged 11 end 12 case 13 # '=' is assignment NOT equality test '==' 14 when (age = 12) then 15 cost = 9 (c) 2012 Ophir Frieder et al

Debugging (Example 4.15 Cont’d) 16 when (age >= 65) then 17 cost = else 19 cost = end 21 if DEBUG_MODULE_1 # Changed to false if # this section is correct 22 puts age # prints age if the section is # still not debugged incorrect 23 end # Print out the final cost 26 puts "Ticket cost: " + cost.to_s (c) 2012 Ophir Frieder et al

Summary  Every program follows a control flow, which is determined by the logic flow of its algorithms  Logic and control flow can often be one directional or conditional  The relational operators are the key operators to creating conditional flows  Another way to create conditional flow is by employing if, elsif, and case statements (c) 2012 Ophir Frieder et al