Top Banner
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and), || (or) and ! (not) Also can create logical expressions with relational operators: >= greater than or equal to; != not equal or inequality = = equal or equivalent Boolean type: bool Example in puTTy (boolean.cpp)
17

True or False: Boolean Expression

Dec 31, 2015

Download

Documents

True or False: Boolean Expression. Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions:  && (and), || (or) and ! (not) Also can create logical expressions with relational operators:  >= greater than or equal to; - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: True or False: Boolean Expression

True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions:  && (and), || (or) and ! (not)

Also can create logical expressions with relational operators:  >= greater than or equal to; != not equal or inequality= = equal or equivalent

Boolean type:  bool

Example in puTTy (boolean.cpp)

Page 2: True or False: Boolean Expression

Exercises

1. bool f1=false;bool f2=true;bool f3=false;

What is the value of the following expression?

(!(!f3&&f1))||f22. int i= 3;

int j=10;int k=20;

What is the value of the following expression (j!=i) && (i<5)||(j<10)||(k>=20)

Page 3: True or False: Boolean Expression

Simple “if” Statement if (boolean expression) statement

The compiler will evaluate the expression, and try to figure out if it is true or false

If true, the statement after the ( ) is executed

If not, the statement is skipped, and execution continues after this statementexpr

stmt

true

false

Page 4: True or False: Boolean Expression

Compound statement

Often, however, we want to do a series of actions in response to the conditional expression

In this case, we use a compound statement if (boolean expression) { statements }

Two stylesif ( z == 0) { x = 0; y = 0;}

if ( z == 0) { x = 0; y = 0;}

Page 5: True or False: Boolean Expression

If-else statement

if ( expression )statement1

else statement2

z==0

x = 0;y = 0;

x = 1;y = 1;

true false

if (z == 0) { x = 0; y = 0;}else { x = 1; y = 1;}

Page 6: True or False: Boolean Expression

ExerciseTake actions while reading the following segment of pseudo code:Exercise 1: if (your age%2==0) stand on your right foot; else stand on your left foot; hop three times; sit down and restExercise 2: if (your age <=50) if (your age%2==0) stand up else site and watch (question to ponder: who will site and watch? The people who age over

50? Or the people whose age under 50 and age is an odd number?)use appropriate indention and { }, and it will make the

association explicit

Page 7: True or False: Boolean Expression

Example#include <iostream>using namespace std;int main () { int value1=0, value2=0;

cout << “Please enter two numbers ? “; cin >> value1 >> value2;

if (value1 < 0) value1 = -value1; if (value2 < 0) value2 = -value2; ; if (value1 > value2) { cout << “__________ “ << value1 << endl; } else { cout << “__________“ << value2 << endl; }Return 0;}

Page 8: True or False: Boolean Expression

Nested If Statement

Any statement can be part of the statement portion of the “if” Thus, “if” statements can

be nested. if you use nested if-else statements, the else is associated with the closest if (unless in different compound statements)

Example: finding the smallest of 3 numbers

int value1=0, value2=0, value3=0;int smallest = 0;

cout << “Please enter 3 integers ? “;cin >> value1 >> value2 >> value3;

if (value1 < value2) { if (value1 < value3) smallest = value1; else smallest = value3;}else { can you fill in the code in this session?}

Page 9: True or False: Boolean Expression

if..else if..else if..else if (expression)

action

else if (expression)

action

else if (expression) action

else action

The multi version appears often enough that it has it's own special form:  The switch statement

Example in puTTy (selection.cpp)

Page 10: True or False: Boolean Expression

In class exercises1. Write the boolean expression of:

1) a ≠b or a ≤ b

2) 20<x<30

2. Write a segment of code to test if integer x is multiple of 3 and 5 or not. If it is, output “yes”, if not, output “no”.

3. What will the following code do? char c; cin>>c; if ( c>=‘a’&& c<=‘z’)

c=c-32; if (c>=‘A’ && c<=‘Z’) c=c+32;

Page 11: True or False: Boolean Expression
Page 12: True or False: Boolean Expression

while Loop (iteration)Repeated Jobs:

– Your teacher told you: “write ‘I am sorry’ on the blackboard 100 times!”

Like if-statements, loops are controlled by a Boolean condition. The code inside the loop will continue to execute over and over until the exit condition is satisfied.

– May mean that the loop body executes zero times if the exit condition is satisfied when the loop first is executed

– You may get an infinite loop if your program never satisfies the exit condition

Page 13: True or False: Boolean Expression

“while” Loops Body

The body of the loop just continues to iterate while the iterateCondition is satisfied. When it fails, execution continues at the statement immediately following the while loop x>0

true

false

Body

Page 14: True or False: Boolean Expression

While Loops Example

#include <iostream>#include <string>using namespace std; int main () { string inString = "junk"; int count = 0; cout << "Enter several lines "; cin >> inString;

while ( inString != "junk") { count++; cin >> inString; }

cout << "You entered " << count << " words before 'junk'" <<endl;}

Page 15: True or False: Boolean Expression

Do-While LoopVery similar to a While loop, with one exception

The exit condition is placed at the end of the loop, and not evaluated until after the loop body has been executed

Thus, the loop body always executes at least onceFormat: do Statement while ( conditionExpr);

x>0

true

false

Body

While Loop Do-While Loop

Body

x>0 true

false

Page 16: True or False: Boolean Expression

Do-While Loop Example

#include<iostream>using namespace std;int main() {

char ans;do{

cout<<“hello\n”;cout<<“Do you want another greatings?\n”;

<<“Press y for yes and n for no\n”; << “and then press return:”;

cin>>ans;} while (ans==‘y’ || ans ==‘Y’);

cout<<“good bye\n”;return 0;

}

Page 17: True or False: Boolean Expression

ITEM Mnemonic  i = 1;                                          -- Initialize

the loop control variable (lcv)while (i != 10) {                          -- Test the

lcv        -- do something                  -- Execute

the action (the reason for the loop to exist)        i++;                                    -- Modify

the lcv}