Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.

Similar presentations


Presentation on theme: "Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary."— Presentation transcript:

1 Chapter 9 IF Statement Bernard Chen

2 If Statement The main statement used for selecting from alternative actions based on test results It’s the primary selection tool in Python and represents the Logic process

3 Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”

4 Outline Simple If statement If... else statement If… else if … else statement

5 Simple If statement It takes the form of an if test if :

6 Simple If statement if 4>3: print “it is true that 4>3” if 1: print “true” a=10 if a==10: print “a=10”

7 If... else statement It takes the form of an if test, and ends with an optional else block if : else:

8 If... else statement if 3>4: print “3 is larger than 4” else: print “3 is NOT larger than 4” if 0: print “true” else: print “false”

9 If... else statement a=10 if a==10: print “a=10” else: print “a is not equal to 10” a=10 if a!=10: print “a=10” else: print “a is not equal to 10” (also try >, < )

10 If… else if … else statement It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if : elif : else:

11 If… else if … else statement a=10 if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

12 If… else if … else statement example number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block... else: print 'No, it is a little lower than that' # you must have guess > number to reach here

13 Some More Logic Expression and >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False

14 Some More Logic Expression or >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True


Download ppt "Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary."

Similar presentations


Ads by Google