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 taken 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"
#include <iostream> using 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 true
Another example:
Ex:
#include <iostream>using 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) ;}
Output:
[amittal@hills chapter4]$ ./a.out
true
false
true
false
It 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.
Regular If statements
File: "dec2.cpp"
#include <iostream>using namespace std ;int main(){ int x1 = 5, y1 = 5 ; if ( x1 == y1 ) cout << "x1 and y1 are equal." << endl; return(0) ;}
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: "dec3.cpp"
#include <iostream>using 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) ;}
Output:
$ g++ dec3.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.
#include <iostream>using 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) ;}
Output:
$ g++ dec4.cpp ; ./a.exe
File: "dec5.cpp"
#include <iostream>using namespace std ;int main(){ int x1 = 5, y1 = 15 ; if ( x1 == y1 ) ; cout << "x1 and y1 are equal." << endl; return(0) ;}
Output:
$ g++ dec5.cpp ; ./a.exe
x1 and y1 are equal.
We 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.
File: "dec6.cpp"
#include <iostream>using namespace std ;int main(){ int x1 = 5, y1 = 15 ; if ( x1 = 6 ) cout << "If statement." << endl; return(0) ;}
Output:
$ g++ dec6.cpp ; ./a.exe
If statement.
In 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.
If else statements
File: "dec7.cpp"
#include <iostream>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) ;}
Output:
$ g++ dec7.cpp ; ./a.exe
x1 is equal to y1
The "else" is associated with the closest "if" above it.
File: "dec8.cpp"
#include <iostream>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) ;}
What 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.
File: "dec9.cpp"
#include <iostream>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) ;}
We can use "else if" statements . Ex:
#include <iostream>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;}
To catch the condition not satisfied by any of the if statements we can place an else statement at the bottom of the if block.
#include <iostream>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;}
We cannot place the "else" statement by itself in the middle.
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";
The above code produces a compiler error. We can compare characters . A character type is an integer value.
Ex:
#include <iostream>using namespace std ;#include <iostream>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 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.
The above program could also have been written as:
#include <iostream>using namespace std ;#include <iostream>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;}
The '0' has been replaced with the number 48 .
Comparisons with floating point numbers
Ex:
#include <iostream>
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;
}
Output:
[amittal@hills chapter4]$ ./a.out
Both a and b are the same.
a and b are the same.
The above shows how unpredictable floating point comparisons are. If we add a a very small amount then a round off is done.
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"
#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;}
Output:
$ ./a.exe
Enter a number from 1 to 3:2
Number is 2.
The 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.
File: "sw2.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;}
$ ./a.exe
Enter a number from 1 to 3:1
Number is 1.
Number is 2.
After 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.
It is possible to have multiple statements in a single case statement.
File: "sw3.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 ; 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;}
Some text.
Logical operators allow us to combine multiple conditions.
File: "l1.cpp"
#include <stdio.h>#include <iostream>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;}
$ ./a.exe
Enter 3 numbers:5
10
15
x1 is less than 10 and y1 is greater than 5.
x1 is less than 10 or y1 is greater than 5.
File: "c1.cpp"
#include <stdio.h>#include <iostream>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;}
$ ./a.exe
Enter x1::2z1:5y1:3z1:5 $ ./a.exe Enter x1::12z1:20y1:6474892z1:4
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 ;
1)
Rewrite "sw2.cpp" using only "if else" statements to accomplish the same logic.
2) 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 using the conditional operator.
3) Write a program that asks the user for a number 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.
Write the program using only if statements and then write it using only switch statement.
4)
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.
1)
#include <iostream>
using namespace std;
int main()
{
int age ;
cout << "Enter your age:" ;
cin >> age ;
if ( age < 20 )
cout << "Teenager" << endl ;
else if ( age >= 20 && age < 30 )
cout << "Career" << endl ;
else if ( age >= 30 && age < 40 )
cout << "Family kids" << endl ;
else if ( age >= 40 && age < 50 )
cout << "Mid life crisis." << endl ;
else
cout << "Retirement Plans" << endl ;
return ( 0 ) ;
}
Relational operators > x > y x is greater than y < x < y x is less than y >= x >= y x is greater than or equal to y ≤ x <= y x is less than or equal to y == x == y x is equal to y != x != y x is not equal to y Logical Operators && AND || OR