// Lab 4 Lecture // ---------------------- Relational Operators -------------------------------- // // we can use comparison and relational operators to determine // whether a condition is true or false. // // == is the "equal to" operator // != is the "not equal to" operator // > is the "greater than" operator // < is the "less than" operator // >= is the "greater than or equal to" operator // <= is the "less than or equal to" operator // Examples: // (1 == 3) will return false // (1 > 0) will return true // (1 != 4) will return true // ----------------------- IF Statements -------------------------------------- // So far we have written programs that are limited to a // linear sequence of execution. IF statements are the first tool we // will learn which allows us to control the flow of our program. // We will now be able to control whether code gets executed or not. // Syntax // if (condition) // { // // statements inside braces run only if the condition is true // } #include #include using namespace std; int main() { ifstream fin; int num1 = 10; int num2 = 5; int num3 = 20; int grade = 90; char lettergrade; // Here is a simple if statement using curly brackets. // Good style dictates that we should indent everything // inside of the if statement (PLEASE DO THIS). if (num1 < grade) // (10 < 90) { cout << num1 << " is less than " << grade << endl; num1 += 5; cout << "num 1 is now " << num1 << endl << endl; } // if you only have one statement inside the IF statement you // do not need curly braces if (num1 > num3) // (10 > 20) cout << num1 << " is greater than " << num3 << endl; // you can also fine tune your if statements by nesting them if (num1 != num3) // (10 != 20) { cout << num1 << " is not equal to " << num3; if(num1 < num2) // (10 < 5) { cout << " and " << num1 << " is less than " << num2 << endl << endl; } if(num1 > num2) // (10 > 5) { cout << " and " << num1 << " is greater than " << num2 << endl << endl; } } // ------------------------- Common Mistakes ---------------------------------- // Do NOT put a semicolon after your IF statement. // This mistake will cause your IF statement to not work. if (num1 == num3); cout << num1 << " is equal to " << num3 << endl << endl; // Another common mistake. // = is the assignment operator. // == is the comparison operator. if (num3 = num1) cout << num3 << " is equal to " << num1 << endl << endl; // Another tip is to be very careful with your braces. It is very // easy to forget one, or add an extra brace on accident. This is // where using good style is crucial to helping you avoid wasting // time debugging syntax errors. // ---------------------- ELSE IF Statements ---------------------------------- // The ELSE IF statement are pretty straight forward. // If your IF statement is false, then it will check the following // ELSE IF statement's condition. This will repeat until one // of the statement's condition is true, or if they are all false // none of the statements will be executed. // However, if several conditions are true, it will only execute the // first true condition it comes to, skipping the rest. // Example, all are true but only the first will execute. if (1 == 1) { cout << "First" << endl << endl; } else if (2 == 2) { cout << "Second" << endl << endl; } else if (3 == 3) { cout << "Third" << endl << endl; } // --------------------- ELSE Statements -------------------------------------- // ELSE Statements // the "else" statement will only execute if // all of the previous IF and ELSE IF statements // were false. if (100 <= 4) { cout << "will never execute" << endl << endl; } else { cout << "Inside Else" << endl << endl; } // Here is an example of IF-ELSE IF statemnts grade = 95; if (grade < 60) { lettergrade = 'f'; } else if (grade < 70) { lettergrade = 'd'; } else if (grade < 80) { lettergrade = 'c'; } else if (grade < 90) { lettergrade = 'b'; } else { lettergrade = 'a'; } cout << "Your final grade is: " << lettergrade << endl << endl; // ----------------------- Logical Operators ---------------------------------- // && and || and ! are logical operators and can be used to evaluate // logical expressions. // && - Logical AND operator will return true only when both sides are true // true && true = true // true && false = false // false && true = false // false && false = false if ( 1 < 3 && 5 == 5) cout << "both expressions are true" << endl << endl; // || - Logical OR operator will return true if either of the sides is true // true || true = true // true || false = true // false || true = true // false || false = false if ( 10 == 5 || 1 != 0) cout << "one or both of the expressions are true" << endl << endl; // ! - NOT operator will flip the logical value. // !true = false // !false = true if ( !(1 == 5) ) cout << "NOT operator flipped a false operation to true." << endl << endl; // ----------------------- File Error Detecting ---------------------------------- // On a final note. Always use an if statement to check if you // successfully opened a file or not. If the file successfully opens it will // return 'true', otherwise 'false'. Therefore, we can use the NOT operator to // detect "if NOT successful" and terminate the program. fin.open("results.txt"); if (!fin) { cout << "ERROR - Failed to open file. Terminating program.\n"; return -1; // terminate program } return 0; }