CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Advertisements

Software Engineering Table-driven methods General control issues.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 8, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
CIS162AD - C# Decision Statements 04_decisions.ppt.
Programming Logic and Design Sixth Edition
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.
Chapter 3 Making Decisions
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Problem Solving with Decisions
CIS162AD - C# If Statements 04_decisions.ppt. CIS162AD2 Overview of Topic  Pseudocode  Flow Control Structures  Flowcharting  If, If-Else  Nested.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
Chapter 4 The If…Then Statement
Introduction to C++ Programming Language
Selection—Making Decisions
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
ITM 352 Flow-Control: if and switch
Chapter 4: Decision Structures and Boolean Logic
CSC115 Introduction to Computer Programming
CS005 Introduction to Programming
Conditional Statements
Lesson 8: Boolean Expressions and "if" Statements
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Control Structure Senior Lecturer
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
CSC530 Data Structure - Decision
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
Chapter 4: Decision Structures and Boolean Logic
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Logical Operations In Matlab.
Three Special Structures – Case, Do While, and Do Until
Herbert G. Mayer, PSU CS Status 8/2/2013
Chapter 5: Control Structure
Conditional Logic Presentation Name Course Name
Controlling Program Flow
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
ECE 103 Engineering Programming Chapter 19 Nested Loops
Controlling Program Flow
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
CHAPTER 5: Control Flow Tools (if statement)
Making decisions with code
Computer Programming Basics
Selection—Making Decisions
ECE 103 Engineering Programming Chapter 22 Selection
Chapter 4: Decision Structures and Boolean Logic
Control Structures.
Presentation transcript:

CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

Syllabus If Statement Simple If Statement Multi-Branch If Statement Nested If Statements Select Case Statement

If Statement VBA uses Boolean expressions, along with If statements, to control the flow of execution of a program If condition Then ‘ condition is Boolean expression action1 ‘ can be multiple statements Else action2 ‘ can also be If Statements End If

If Statement Flowchart yes no Condition true? Action 2 Action 1 The arrows are one-way streets. Only one action can (and must) be performed. Branches flow back together

If Statement Example If varA > varB Then max = varA min = varB Else max = varB min = varA End If Note: In real life we would use the Max and Min functions

If Statement Example If price >= discountShippingPrice Then ‘ compute shipping charge, with a discount for more expensive orders If price >= discountShippingPrice Then shippingCharge = price * discountShippingChargeRate Else shippingCharge = price * regularShippingChargeRate End If totalCharge = price + shippingCharge

If Statement Options ‘ The Else part can be omitted: If condition Then action1 End If ‘ There can be multiple ElseIf branches If condition1 Then ‘if condition1 is True do action1, end ElseIf condition2 Then ‘if condition1 is False & condtion2 is T, do action2 action2 Else ‘else is still optional action3 EndIf

Single Branch If Statement ‘ compute shipping charge if applicable shippingCharge = 0 If price < freeShippingPrice Then shippingCharge = price * shippingChargeRate End If price = price + shippingCharge ‘ if the If statement interior is not executed ‘ then shippingCharge is 0

Single Branch If Statement yes no Condition true? Action

Multiple Branch If Statement ‘ Phrase thank you message based on tip size tipPercent = (tipAmount/baseCharge) * 100 If tipPercent < 15 Then txtThankYou.Text = “Thanks. Grumble” ElseIf tipPercent < 20 Then txtThankYou.Text = “Thanks a lot” ElseIf tipPercent < 25 Then txtThankYou.Text = “Thank you very much! Have a nice day.” Else ‘we know the tip is at least 25% txtThankYou.Text = “Thank you!! Have a GREAT Day!” End If

Multiple Branch If Statement y Condition 1 true? Action 1 n y Action 2 Condition 2 true? n y Condition 3 true? Action 3 n Else Action

Nesting: If’s inside of If’s You can nest entire If statements inside the If or Else part of another If statement Nesting more than one or two deep is strongly discouraged! It makes the program hard to read and understand Try to use Elseif or more complex conditions instead

Nested If’s Example ‘ Select title based on language and gender If language = “French” Then If gender = “Female” Then title = “Mademoiselle” Else title = “Monsieur” Endif ElseIf language = “English” Then If gender = “Female” Then title = “Miss” title = “Mister” EndIf title = “” ‘no title in this case

Converting to ElseIfs ‘ Select title based on language and gender If language = “French” And gender = “Female” Then title = “Mademoiselle” ElseIf language = “French” And gender = “Male” Then title = “Monsieur” ElseIf language = “English” And gender = “Female” Then title = “Miss” ElseIf language = “English” And gender = “Male” Then title = “Mister” Else title = “” ‘ it’s usually best to have an else case EndIf

Select Case Statements The Select Case Statement can be used when there are multiple options compared to the same reference value and the cases can be expressed by contsants It can simplify program structure It makes the logical structure of the program clear when a nested if or long Else If structure might not

Case Statement Example ‘ position is a variable with value between 1 and 50 Select Case position Case 1 txtOutcome.Text = “Win” ‘several lines could go here Case 2 txtOutcome.Text = “Place” Case 3 txtOutcome.Text = “Show” Case 4,5 txtOutcome.Text = “Close but no cigar” Case Else txtOutcome.Text = “Out of the money” End Select

Conditionals Overview We’ve looked at program elements that let us write conditions: Boolean constants True and False Comparison operators to form Boolean expressions Boolean operators to build more complex expressions; truth tables to check them If statements, three types (with Else, with no Else, with ElseIfs) Nested If’s Select Case statements