Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”

Similar presentations


Presentation on theme: "CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”"— Presentation transcript:

1 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

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

3 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

4 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

5 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

6 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

7 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

8 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

9 Single Branch If Statement
yes no Condition true? Action

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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


Download ppt "CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”"

Similar presentations


Ads by Google