Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 231 Lab 2.

Similar presentations


Presentation on theme: "CSE 231 Lab 2."— Presentation transcript:

1 CSE 231 Lab 2

2 Topics to cover key condition : Statement Statement
Decisions: Decision is how programs make choices if , elif, and else Repetition: Repeat until a condition is met while loop for loop (optional) condition ends all headers key condition : Keyword Statement Indentation “suite” of statements Statement

3 Passed #What gets printed? Decisions with if grade = 3.5
print(“Passed”) #What gets printed? Passed

4 #nothing #What gets printed? Decisions with if grade = 0.5
print(“Passed”) #What gets printed? #nothing

5 Decisions with if/else
grade = 3.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Passed :)

6 Decisions with if/else
grade = 0.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Not Passed :’(

7 Decisions with if/elif/else
grade = 0.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Not so good

8 Decisions with if/elif/else
#What gets printed? grade = 3.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) Excellent ...but why not “Good” as well, since 3.5 >= 2.0?

9 Decisions with if/elif/else
grade = 2.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Good

10 Repetition with while answer = input(“Please say Hi:”)
while answer != “Hi”: print(“You do not want to say Hi ?”) answer = input(“Let’s try again… Say Hi:”) print(“Cool, let’s continue then.”) >Please say Hi: no >You do not want to say Hi ? >Let’s try again… Say Hi: no >You do not want to say Hi ? >Let’s try again… Say Hi: Hi >Cool, let’s continue then.

11 1 2 why not 3? #What gets printed? Repetition with for n = 3
for i in range(n): print(i) #What gets printed? 1 2 why not 3?

12 2 3 4 why start at 2? #What gets printed? Repetition with for
for i in range(2,5): print(i) #What gets printed? 2 3 4 why start at 2?

13 1 3 5 7 why only odds? #What gets printed? Repetition with for
for i in range(1,9,2): print(i) #What gets printed? 1 3 5 7 why only odds?

14 Should I use while or for
use this when you know how many times you want to iterate something e.g., to print “hello” 5 times for i in range(5): print(“hello”) while: when you want to continue looping until some condition is met when we do not know how many times we need to loop e.g., continue program until user inputs “Hi” answer = input() while answer != “Hi”:

15 Break vs Continue statement
#What gets printed? for letter in 'Python': if letter == 'h': break print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t for letter in 'Python': if letter == 'h': continue print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n

16 Break vs continue The break statement in Python terminates the current loop and resumes execution at the next statement, The continue statement in Python returns the control to the beginning of the while loop.


Download ppt "CSE 231 Lab 2."

Similar presentations


Ads by Google