Decisions
Contents
Bool Data Type
A boolean data type variable can take on the boolean value of true or false or an expression that evaluates to true or false . The expression can be of the form a1 < b1 that is composed of logical operators.It can also take on an integer value. If takes a value of 0 then it is false and if the value is anything other than 0 then the value is true.
File: dec1.cpp
#includeusing namespace std ; int main() { bool b1, b2 ; int x = 5, y = 10; b1 = x < y; b2 = y == x; cout << "b1 is " << b1 << endl; cout << "b2 is " << b2 << endl; //We need to use boolalpha to show true or false. cout << boolalpha << "b1 is " << b1 << endl; cout << "b2 is " << b2 << endl; bool b3 ; b3 = 10 ; cout << b3 << endl ; return(0) ; }
$ ./a.exe b1 is 1 b2 is 0 b1 is true b2 is false trueAnother example:
File: dec1.cpp
#includeusing namespace std ; int main() { bool b1 , b2; cout << boolalpha << endl; b1 = true ; cout << b1 << endl ; b1 = false ; cout << b1 << endl ; b1 = 10 ; cout << b1 << endl ; b1 = 0 ; cout << b1 << endl ; return(0) ; }
$ g++ dec2.cpp ; ./a.exe true false true falseIt is not a good idea to use numerical values for boolean variables as it makes the code harder to read and also it may not be easy to figure out all the values that the integer expression may take.
If statements
Regular If statementsFile: dec3.cpp
#includeusing namespace std ; int main() { int x1 = 5, y1 = 5 ; if ( x1 == y1 ) cout << "x1 and y1 are equal." << endl; return(0) ; }
$ g++ dec3.cpp ; ./a.exe x1 and y1 are equal.We are using the relational operator "==" to test if x1 equals y1 and if so print a statement out. the values are equal. The immediate statement following the "if" statement is associated with the condition. Only 1 statement is associated with the "if" statement.
File: dec4.cpp
#includeusing namespace std ; int main() { int x1 = 5, y1 = 7 ; if ( x1 == y1 ) cout << "x1 and y1 are equal." << endl; cout << "Another statement." << endl; return(0) ; }
$ g++ dec4.cpp ; ./a.exe Another statement.In the above "x1" and "y1" are not equal but we still get the second "cout" statement printed out because only the first one is associated with the "if" statement. Indentation does not make a difference to the logic and grammar of the program. We can indent our program any way we want and that will not make a difference to the program. What if we wanted to associate the second line with the "if" then we need to place the statements inside a block.
File: dec5.cpp
#includeusing namespace std ; int main() { int x1 = 5, y1 = 7 ; if ( x1 == y1 ) { cout << "x1 and y1 are equal." << endl; cout << "Another statement." << endl; } return(0) ; }
File: dec6.cpp
#includeWe can have empty statements with just a semi colon ";" and the empty statement is associated with the "if" statement so the "cout" statement is always going to get printed out.using namespace std ; int main() { int x1 = 5, y1 = 15 ; if ( x1 == y1 ) ; cout << "x1 and y1 are equal." << endl; return(0) ; } $ g++ dec6.cpp ; ./a.exe x1 and y1 are equal.
File: dec7.cpp
#includeIn the above "x1" is not equal to 6 but we still get the "cout" statement printed out. Remember the if condition can work on an integer also and if the integer is anything other than 0 then that is considered a true condition.using namespace std ; int main() { int x1 = 5, y1 = 15 ; if ( x1 = 6 ) cout << "If statement." << endl; return(0) ; }
If else statements
File: dec8.cpp
#includeThe "else" is associated with the closest "if" above it.using namespace std ; int main() { int x1 = 5, y1 = 5 ; if ( x1 == y1 ) cout << "x1 is equal to y1" << endl; else cout << "x1 is not equal to y1" << endl; return(0) ; }
File: dec9.cpp
#includeWhat does the above print ? We have a single "else" in the above program and even though it looks like it is associated with the "if ( x1 > 10 ) " statement ; it's actually associated with the inner one. Indentation does not make a difference to the logic of the program. We should however make sure that the indentation matches the logic in order keep our programs readable.using namespace std ; int main() { int x1 = 6 ; if ( x1 < 10 ) if ( x1 < 5 ) cout << "Line A." << endl; else cout << "Line B." << endl; return(0) ; }
File: dec10.cpp
#includeWe can use "else if" statements .using namespace std ; int main() { int x1 = 6 ; if ( x1 < 10 ) if ( x1 < 5 ) cout << "Line A." << endl; else cout << "Line B." << endl; return(0) ; }
File: else1.cpp
#includeTo catch the condition not satisfied by any of the if statements we can place an else statement at the bottom of the if block.using namespace std; int main() { int testScore ; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) cout << "Your grade is F.\n"; else if (testScore < 70) cout << "Your grade is D.\n"; else if (testScore < 80) cout << "Your grade is C.\n"; else if (testScore < 90) cout << "Your grade is B.\n"; else if (testScore <= 100) cout << "Your grade is A.\n"; return 0; }
File: else2.cpp
#includeWe cannot place the "else" statement by itself in the middle.using namespace std; int main() { int testScore ; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) cout << "Your grade is F.\n"; else if (testScore < 70) cout << "Your grade is D.\n"; else if (testScore < 80) cout << "Your grade is C.\n"; else if (testScore < 90) cout << "Your grade is B.\n"; else if (testScore <= 100) cout << "Your grade is A.\n"; else cout << "That is not a valid score.\n"; return 0; }
File: else3.cpp
#includeThe above code produces a compiler error. We can compare characters . A character type is an integer value.using namespace std; int main() { if (testScore < 60) cout << "Your grade is F.\n"; else if (testScore < 70) cout << "Your grade is D.\n"; else if (testScore < 80) cout << "Your grade is C.\n"; else if (testScore < 90) cout << "Your grade is B.\n"; else cout << "That is not a valid score.\n"; else if (testScore <= 100) cout << "Your grade is A.\n"; }
File: char1.cpp
#includeThe above code asks the user to enter a character and checks if it is a digit. Notice that the single character '0' is actually the number 48( from the AsCII chart). We are using the logical operator in the above program. The statement "cin.get()" asks the user to input a character.using namespace std; int main() { char ch; // Get a character from the user. cout << "Enter a digit or a letter: "; ch = cin.get(); // Determine what the user entered. if (ch >= '0' && ch <= '9') cout << "You entered a digit.\n"; return 0; }
The above program could also have been written as:
File: char2.cpp
#includeThe '0' has been replaced with the number 48 .using namespace std ; int main() { char ch; // Get a character from the user. cout << "Enter a digit or a letter: "; ch = cin.get(); // Determine what the user entered. if (ch >= 48 && ch <= 57 ) cout << "You entered a digit.\n"; return 0; }
Comparisons with floating point numbers
File: float1.cpp
#includeThe above shows how unpredictable floating point comparisons are. If we add a a very small amount then a round off is done.using namespace std; int main() { double a = 1.5; // a is 1.5. double b = 1.5; // b is 1.5. a += 0.0000000000000001; // Add a little to a. if (a == b) cout << "Both a and b are the same.\n"; else cout << "a and b are not the same.\n"; if (a > b) cout << "Both a is greater than b.\n"; else if ( a < b ) cout << "Both a is less than b.\n"; else cout << "a and b are the same.\n"; return 0; } $ g++ float1.cpp ; ./a.exe Both a and b are the same. a and b are the same.
Switch
The switch statement allow us to compare an integral value and take an action based on that. It is similar to the "if else" but has it's own options and may be more suitable for implementation of certain constructs such as menus.File: sw1.cpp
#includeThe switch statement works on "x1" in the above example and the case statements check against this value and if there is a match then the statement in the "case" block get executed. Notice that there is a "break" statement inside the "case" statements. The purpose of the "break" statement is to come out of the switch block. Let's see what happens if we remove a break statement.#include using namespace std ; int main() { int x1 ; cout << "Enter a number from 1 to 3:" ; cin >> x1 ; switch( x1 ) { case 1: cout << "Number is 1." << endl ; break ; case 2: cout << "Number is 2." << endl ; break ; case 3: cout << "Number is 3." << endl ; break ; default: cout << "Number is something else ." << endl ; } return 0; }
File: sw2.cpp
#includeAfter the statement in case 1 is executed control does not come out of the switch statement but falls down to the next statement and after the case 2 statement is executed then the control breaks out of the switch statement.#include using namespace std ; int main() { int x1 ; cout << "Enter a number from 1 to 3:" ; cin >> x1 ; switch( x1 ) { case 1: cout << "Number is 1." << endl ; //break ; case 2: cout << "Number is 2." << endl ; break ; case 3: cout << "Number is 3." << endl ; break ; default: cout << "Number is something else ." << endl ; } return 0; }
It is possible to have multiple statements in a single case statement.
File: sw3.cpp
#include#include using namespace std ; int main() { int x1 ; cout << "Enter a number from 1 to 3:" ; cin >> x1 ; switch( x1 ) { case 1: cout << "Number is 1." << endl ; cout << "Number is 1. Statement repeated" << endl ; break ; case 2: cout << "Number is 2." << endl ; break ; case 3: cout << "Number is 3." << endl ; break ; default: cout << "Number is something else ." << endl ; } return 0; }
Logical Operators
Logical operators allow us to combine multiple conditions.File: l1.cpp
#include#include using namespace std ; int main() { int x1 , y1 , z1 ; cout << "Enter 3 numbers:" ; cin >> x1 >> y1 >> z1 ; if ( x1 < 10 && y1 > 5 ) cout << "x1 is less than 10 and y1 is greater than 5." << endl ; if ( x1 < 10 || y1 > 5 ) cout << "x1 is less than 10 or y1 is greater than 5." << endl ; if ( ! x1 == 10 ) cout << "x1 is not equal to 10." << endl ; return 0; }
Conditional Operator
The conditional operator is another way to write a "if else" expression and makes the code more concise and easier to read. The format isConditional Expression ? statement1 : statement2 ;
If the Conditional Expression is true then statement1 is executed else statement2 is executed.
File: c1.cpp
#include#include using namespace std ; int main() { int x1=0 , y1=0 , z1=0 ; cout << "Enter x1::" ; cin >> x1 ; z1 = ( x1 < 10 ) ? 5 : 20 ; cout << "z1:" << z1 << endl ; x1 < 10 ? y1=3 : z1=4 ; cout << "y1:" << y1 << endl ; cout << "z1:" << z1 << endl ; ( x1 < 10 ) ? ( cout << "Part 0." ) : (cout << "Part 1\n", cout << "Part 2\n") ; return 0; }
The conditional operator is useful as a short hand notation for the "if else" statement. We can write expressions that assign a value such as: z1 = ( x1 < 10 ) ? 5 : 20 ; or write expressions that are statements such as: x1 < 10 ? y1=3 : z1=4 ;
Exercises
1)File: ex_1.cpp
#include <stdio.h> #include <iostream> using namespace std ; int main() { int x1 ; cout << "Enter a number from 1 to 3:" ; cin >> x1 ; switch( x1 ) { case 1: cout << "Number is 1." << endl ; break ; case 2: cout << "Number is 2." << endl ; break ; case 3: cout << "Number is 3." << endl ; break ; default: cout << "Number is something else ." << endl ; } return 0; }Rewrite the above using "if else" statements.
2)
File: ex_2.cpp
Write a program that asks the user for an integer and then outputs whether that value is odd or even. Write this using the if statements and then only using the switch statement. Write the program a third time using the conditional operator.
#include <stdio.h> #include <iostream> using namespace std ; int main() { int x1 ; cout << "Enter a number:" ; cin >> x1 ; return 0; }3)
File: ex_3.cpp
Write a program that asks the user for a number (Assume the number is 1 or higher ) and outputs the following depending on the value of the number: The number is less than 5. The number is equal to 5. The number is greater than 5. Use only the switch statement.
#include <stdio.h> #include <iostream> using namespace std ; int main() { int x1 ; cout << "Enter a number:" ; cin >> x1 ; return 0; }4)
File: ex_4.cpp
Write a program that takes an age and prints different things depending on the following ranges. < 20 Teenager 20 - 30 Career 30 - 40 Family Kids 40 - 50 Mid life crisis >50 Retirement plans Rewrite the above solution using switch instead of if.
#include <stdio.h> #include <iostream> using namespace std ; int main() { int x1 ; cout << "Enter a number:" ; cin >> x1 ; return 0; }
Solutions
1) File: ex_1s.cpp2) File: ex_2s.cpp
3) File: ex_3s.cpp
3) File: ex_4s.cpp