1 / 17

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;

Download Presentation

True or False: Boolean Expression

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. Exercises 1. bool f1=false; bool f2=true; bool f3=false; What is the value of the following expression? (!(!f3&&f1))||f2 2. inti= 3; int j=10; int k=20; What is the value of the following expression (j!=i) && (i<5)||(j<10)||(k>=20)

  3. false expr true stmt 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 statement

  4. 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 styles if ( z == 0) { x = 0; y = 0; } if ( z == 0) { x = 0; y = 0; }

  5. false true z==0 x = 1; y = 1; x = 0; y = 0; If-else statement if ( expression ) statement1 else statement2 if (z == 0) { x = 0; y = 0; } else { x = 1; y = 1; }

  6. Exercise Take 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 rest Exercise 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

  7. 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;}

  8. 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? }

  9. 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)

  10. In class exercises 1. 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;

  11. 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

  12. false x>0 true Body “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

  13. 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; }

  14. Do-While Loop • Very 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 once • Format: do Statement while ( conditionExpr); false Body x>0 true true x>0 Body false While Loop Do-While Loop

  15. 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; }

  16. 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 • }

More Related