peg200/saidatul rahah 1. selection criteria › if..else statement › relational operators ›...

24
PEG200/Saidatul Rahah 1

Upload: oswin-smith

Post on 20-Jan-2016

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

PEG200/Saidatul Rahah 1

Page 2: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Selection Criteria › if..else statement› relational operators› logical operators

The if-then-else Statement Nested if Statements The switch Statement Applications

PEG200/Saidatul Rahah 2

Page 3: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

if-else statement: implements a decision structure for two alternativesSyntax:if (condition)statement executed if condition is true;elsestatement executed if condition is false;

3

true

false

PEG200/Saidatul Rahah

Page 4: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Relational expression: compares two operands or expressions using relational operators

4PEG200/Saidatul Rahah

Page 5: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Relational Operators:› True 1› False 0

PEG200/Saidatul Rahah 5

Expression Value Interpretation

Comment

“Hello”>”Goodbye” 1 true The first ‘H’ in ‘Hello’ is greater than ‘G’ in ‘Goodbye’

“SMITH” > “JONES” 1 True The first ‘S’ in SMITH is greater than ‘J’ in JONES.

“Behop”>”Beehive” 1 True The 3rd char., ‘h’ is “Behop” is greater than the 3rd char. ‘e’ in “Beehive”

Page 6: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Logical Operators

PEG200/Saidatul Rahah 6

AND && condition is true only if both expressions are true

OR || condition is true if either one or both of the expressions is true

NOT ! changes an expression to its opposite state; true becomes false, false becomes true

Page 7: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

p q p&&q p||q !p

T T T T F

T F F T F

F T F T T

F F F F T

PEG200/Saidatul Rahah 7

Page 8: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Precedence and Associativity of Operators (Table 4.2 page 190)

PEG200/Saidatul Rahah 8

Page 9: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Using parentheses, determine the value of the following expression, assuming

Note: Show your work

6 % 2 * 4 > 5 || 2 % 2 * 6 < 7 && 4 > 2

PEG200/Saidatul Rahah 9

Page 10: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

1pt parentheses ((((6 % 2) * 4) > 5) || (((2 % 2) * 6) < 7) && (4 >

2)))

(((0 * 4) > 5 ) || ((0 * 6) < (7 && 1))((0 > 5) || (0 < 7 && 1)(0 || 1 && 1) 2pt for the working flow

0 || 1 1 1pt for answer

PEG200/Saidatul Rahah 10

Page 11: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

if-else performs instructions based on the result of a comparison

Place statements on separate lines for readability

Syntax:

PEG200/Saidatul Rahah 11

truefalse

Page 12: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

#include <iostream>int main(){    int number = 5;    int guess;    cout << "I am thinking of a number between 1 and 10" << endl;    cout << "Enter your guess, please ";    cin >> guess;    if (guess == number)    {       

 cout << "Incredible, you are correct" << endl;    }   else    {        cout << "Sorry, try again" << endl;    }

return 0;}

PEG200/Saidatul Rahah 12

Page 13: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Compound statement: a sequence of single statements contained between braces, creating a block of statements

Any variable declared within a block is usable only within that block

Scope: the area within a program where a variable can be used; a variable’s scope is based on where the variable is declared

PEG200/Saidatul Rahah 13

Page 14: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

PEG200/Saidatul Rahah 14

Page 15: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Write the appropriate if statements for each of the following conditions:› If the slope is less than 0.5 set the variable

flag to zero, else set flag to one.› If x is greater than y and z is less than 20,

read in a value for p.› If distance is greater than 20 and it less

than 35, read in a value for time.

PEG200/Saidatul Rahah 15

Page 16: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Nested if statement: an if-else statement completely contained within another if-else

PEG200/Saidatul Rahah 16

true

falsetrue

truefalse

false

Page 17: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

General form of an if-else chain

PEG200/Saidatul Rahah 17

if(gender ==‘f’) cout<< “Female”;

else if (gender==‘m’) cout<< “Male”;

else cout<< “An invalid code for gender was entered”;

Page 18: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

PEG200/Saidatul Rahah 18

#include <iostream>int main(){ int number = 5;    int guess;

    cout << "I am thinking of a number between 1 and 10" << endl;    cout << "Enter your guess, please ";    cin >> guess;    if (guess == number)    {        cout << "Incredible, you are correct" << endl;    }    else if (guess < number)    {        cout << "Higher, try again" << endl;    }    else     {        cout << "Lower, try again" << endl;    }   return 0;}

Page 19: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

An angle is considered acute if it less than 90 degrees, obtuse if it greater than 90 degrees, and a right angle if it is equal to 90 degrees. Using this information, write a C++ program that accepts an angle, in degrees, and displays the type of angle corresponding to the degrees entered.

PEG200/Saidatul Rahah 19

Page 20: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

switch statement: provides for one selection from many alternatives

switch keyword starts the statement; is followed by the expression to be evaluated

case keyword identifies a value to be compared to the switch expression; when a match is found, statements in this case block are executed

All further cases after a match is found are executed unless a break statement is found

default case is executed if no other case value matches were found (is optional)

PEG200/Saidatul Rahah 20

Page 21: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

PEG200/Saidatul Rahah 21

S2 SnS1

switch (gender){

case ‘f’: case ‘F’:cout<< “Female”;break;

case ‘m’: case ‘M’:cout<< “Male”;break;

default:cout<< “An invalid code

for gender was entered”;}

Page 22: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Rewrite the following if-else chain using a switch statement:

if (factor==1)

pressure =25.0;else if (factor==2)

pressure =36.0;else if (factor==3)||if (factor==4)||if(factor==5)

pressure =49.0;

PEG200/Saidatul Rahah 22

Page 23: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

• Using the assignment operator (=) instead of the relational operator (==) for an equality test

• Assuming a structural problem with an if-else causes the error instead of focusing on the data value being tested

• Using nested if statements without braces to define the structure

PEG200/Saidatul Rahah 23

Page 24: PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if

Write a program to input a student IQ and gender. Display “Intelligent Male” if the student is a male with IQ of at least 100. Display “Not so intelligent Male” if the student is a male with IQ is less than 100. Display the same message for female students as well.

Thank You for Listening….. PEG200/Saidatul Rahah 24