the statement - gramercydata3.5 logical operators operator description and will return true when...

28
The if Statement Chapter 3

Upload: others

Post on 27-Jul-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Agenda1. Relational operators: >, <, <=, >=, ==, !=

2. The if-elif-else statement

3. Logical operators: and or not

Page 3: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not
Page 4: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

3.1 The if Statement

Page 5: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program Output

Page 6: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Indentation: Whitespace Matters!1 tab = 4 spaces

Page 7: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Indentation examples

Page 8: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

3.2 The if-else Statement

Page 9: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

if-else statement

Page 10: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program output

Page 11: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

if-else statement w boolean

Page 12: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

if-else statement w boolean, version 2

Page 13: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

3.3 String comparison

Page 14: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program output

Page 15: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

String comparison is case sensitive

Page 16: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program Output

Page 17: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

3.5 Logical Operators

Operator Description

and will return true when both conditions are true.

or will return true when at least one condition is true.

not If the condition is true, not operator will make it false.

Page 18: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Logical and

Page 19: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program output

Page 20: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Logical or

Page 21: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program output

Page 22: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Operator not

Page 23: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Program output

Page 24: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

3.4 The if-elif-else Statement

Page 25: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Testing a series of conditions

Page 26: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Age:infant, child, teenager, adult. Write a program that asks a user to enter a person’s age. The program should then display a message if the person is an infant, child, teenager or adult.

Page 27: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

Solution -where’s the bug?

Page 28: The Statement - GramercyData3.5 Logical Operators Operator Description and will return true when both conditions are true.or will return true when at least one condition is true. not

3.4 Nested if-elif-else statements