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)
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
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)
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)
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
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;}
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;}
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
Example#include <iostream>using namespace std;int main () { int value1=0, value2=0;
cout << “Please enter two numbers ? “; cin >> value1 >> value2;
if (value1 < value2) { if (value1 < value3) smallest = value1; else smallest = value3;}else { can you fill in the code in this session?}
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)
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;
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
“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
While Loops Example
#include <iostream>#include <string>using namespace std; int main () { string inString = "junk"; int count = 0; cout << "Enter several lines "; cin >> inString;