Boolean Expressions (Conditions) Boolean Expressions: Boolean expressions evaluate to either True or False. Boolean expressions are constructed using relational operators, which allow for comparison between values to determine whether the overall expression is True or False. When comparing values of type double, avoid using == because the computer may handle this is mathematically incorrect due to round off error. Use < or > instead. When comparing values of type char or string, uppercase and lowercase letters are not equivalent. Relational Operators: Operator What it means = = is equal to? < is less than? > is greater than? <= is less than or equal to? >= is greater than or equal to?!= is not equal to? Example: Boolean Expression Evaluates To hello = = HELLO false 5= = 5 true 3 < 10 true 3 > 2 true 5< = 5 true 5>=6 false 2! = 2 false Logical Operators: Logical operators allow you to combine two or more Boolean expressions together in order to evaluate more complex conditions. Logical operators include And, Or, and Not conditions. Operator Symbol in C++ And && Or Not!
Truth Tables for Each Logical Operator: Boolean Exp #1 Boolean Exp #2 Exp #1 && Exp #2 True True True True False False AND False True False False False False Boolean Exp #1 Boolean Exp #2 Exp #1 Exp #2 True True True True False True OR False True True False False False Boolean Exp True False! Exp False True NOT Examples: Expression Evaluates To hello = = HELLO && A == A false 5= = 5 3<1 true 3 < 10 &&!(4>9) true!(3 > 2) false 5< = 5 && 6<10 && 9==9 true 5>=6 && a == a 2>3 false 2! = 2 && 3!=3 false
Selection Statements: If Selection Statements In programming, a Selection Statement refers to some block of code that makes a decision in order to control the flow of a program. The decision is based on some condition(s) being true, which the program will evaluate during runtime. General idea: Example 1: 1. Ask the user for his/her age. 2. Collect that information from the user. 3. If the user s age is greater than or equal to 16, state that the user may be eligible to receive a driver s license. 4. Otherwise, state that the user is not eligible to receive a driver s license. Example 2: 1. Ask the user to enter a letter grade. 2. Collect the information from the user. 3. If the user has entered any of the letters: A, B, C, or D, state that the user has passed. 4. Otherwise, state that the user has failed. If Statement: Format: if( Boolean expression) The code executes such that IF the Boolean expression evaluates to TRUE, then the statement executes. If the Boolean expression evaluates to FALSE, then the statement does NOT execute. Note: if you want to execute MORE THAN 1 STATEMENT based on your condition, then you need to wrap those statements in curly brackets. Also note that because there will now be a whole lot more curly brackets, you should start labeling the closing curlies with some sort of indicator of which bracket started them (as shown in Example 2).
Example 1: Code: int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license.";
Example 2: int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license."<<endl; cout<<"however, if you do get a license, Ms. Petr will get more white hairs!";
If Else Statement: Format: if( Boolean expression) else Example: Code: When else is added to an if statement, it means in all other cases. So If the Boolean expression belonging to the If clause is False, then the statements following the Else clause will execute. int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license."<<endl; cout<<"however, if you do get a license, Ms. Petr will get more white hairs!"; else cout<<"better find a ride kiddo."; Careful: dangling else if there are no curly braces, the compiler will assume that the else goes with the if directly before it (regardless of your indentation).
If Else If Else Statement: Format: if(expression) else if(expression) else if(expression) else Example: Code: Using if..else if else statements will ensure ONLY one of the choices executes. Sometimes a sequence of if s will accomplish the same result, but when you use a ladder of separate if statements, if more than one of them has a condition that evaluates to TRUE, then MORE THAN ONE of them will execute. int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license."<<endl; cout<<"however, if you do get a license, Ms. Petr will get more white hairs!"; else if(age==15) cout<<"you are old enough to get a learner's permit."; else cout<<"better find a ride kiddo.";
Video: Getting Started with If Statements in C++: http://www.youtube.com/watch?v=smw_1usvbds&feature=youtu.be