Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 201 if-else statement

Similar presentations


Presentation on theme: "CMPT 201 if-else statement"— Presentation transcript:

1 CMPT 201 if-else statement

2

3 Syntax for if-else if (condition) Yes_Statement else No_Statement
Chooses between two alternative actions

4 Boolean Expression The type bool Two possible values: true or false
bool flag; Two possible values: true or false 1 < 2 2 > 3 number > 10 num1 < num2

5 Boolean Expression if (number > 10) //when it is true cout<<“The number is greater than 10\n”; else //when it is false cout<<“The number is less than 10\n”;

6 Comparison Operators > greater than < less than
>= greater than or equal to <= less than or equal to == equal to != not equal to a = 2; if (a == 2)

7 Exercises Read an integer from the user input, print a message showing if the number is even/odd.

8

9 Omitting the else part .... if (score < 60) score = 60;
cout<<“Your final score is ”<<score;

10 Exercise: Guess a number

11 The And Operator If score is greater than 0 and less than 100...
(score > 0) && (score < 100) is true only if both are true

12 The Or Operator (score1 >= 60) || (score2 >= 60)
is true if one or both are true

13 The Not Operator !(score > 60) is equivalent to score <= 60

14 What is the result of the following expression
!( (y < 3) || (y > 7) ) when y is 8?

15 Precedence Rules

16 if ((score > 0) && (score < 100))
even though the inner parentheses are not required x + 1 > 2 || x + 1 < -3 ((x + 1 ) > 2) || ((x + 1) < -3) Arithmetic operators (highest) Comparison operators Logical operators

17 Note if (x < z < y ) // Wrong!!
cout<<“z is between x and y”; if ((z > x) && (z < y))

18 Short-Circuit Evaluation
exp1 && exp2 if (((slices/students)>=2) && (students! =0)) cout<<“Each student may have two slices of pizza!”; if ((students!=0) && ((slices/students)>=2)) cout<<“Each student may have two slices of pizza!”;

19 Nested Statements

20 Guess a Number (Validate the User Input)

21 Guess a Number

22 Find the right match! if (fuel < 0.75) if (fuel < 0.25) cout<<“Fuel very low. Caution!”; else cout<<“Fuel over 3/4. Don’t stop now.”;

23 if (fuel < 0.75) { if (fuel < 0.25) cout<<“Fuel very low. Caution!”; } else cout<<“Fuel over 3/4. Don’t stop now.”;

24 Multiway if-else Sometimes things can not be solved in two ways.
Driver license example: age<16: no way.. 16<=age<18: youth license 18<=age<70: standard license age>=70: special license

25 Multiway if-else syntax
if (condition_1) Action_1; else if (condition_2) Action_2; ... else if (condition_N) Action_N; else Action_For_All_Other_Cases;

26 Under 16: You have to wait Between 16 and 18: You may have a youth license Between 18 and 70: You may have a standard license Over 70: You need a special license

27 Exercise Write a program to print the grade result based on the user input (score). Be sure to validate the user input. A B 80-90 C 70-80 D 60-70 F <60

28 The Conditional Operator
Can be used to create short if/else statements Format: expr ? expr : expr; x<0 ? y=10 : z=20; First Expression: Expression to be tested 2nd Expression: Executes if first expression is true 3rd Expression: Executes if the first expression is false

29 x > 100? a = 0 : a = 1 a = x > 100 ? 0 : 1

30 Blocks and Scope Scope of a variable is the block in which it is defined.

31 Type Casting static_cast<Type>(Value) double -> int
static_cast<int>(9.2) static_cast<int>(number)

32 Type Casting (cont.) double m; m = static_cast<double>(y2-y1)
Useful for floating point division using ints: double m; m = static_cast<double>(y2-y1) /(x2-x1); Useful to see int value of a char variable: char ch = 'C'; cout << ch << " is " << static_cast<int>(ch);

33 Comparing Characters Characters are compared using their ASCII values.
'A' < 'B' The ASCII value of 'A' (65) is less than the ASCII value of 'B'(66) How could you determine if a character is a capital letter? Lowercase letters have higher ASCII codes than uppercase letters, so 'a' > 'Z'

34 Homework 2 Due on Tuesday, October 4


Download ppt "CMPT 201 if-else statement"

Similar presentations


Ads by Google