Consider the following programme
If.cpp
3.1
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 cout<<"Choose an integer"<<endl; 9 int number; 10 cin >> number; 11 12 int remainder=number%2; 13 14 if(remainder==0) 15 cout<<"even"<<endl; 16 else 17 cout<<"odd"<<endl; 18 19 return 0; 20 }
In line 3.1.10 this programme contains the input stream cin, this is like the output stream cout, but the pipe goes the other way, from the screen back to the programme. It allows input; when the programme gets to the cin command it waits until a number is inputed and then gives that value to number using the stream operator >>. There is a novel arithmetic operation at line 3.1.12, in addition to the obvious addition, +, subtraction, -, multiplication , * and division, /, c++ has a modulus operator % which finds a remainder, so a%b is the remainder when a is divided by b and, for example, 15%4 returns 3.
These technicalities over with, the important part of the programme is at lines 3.1.14-17 where there is an if statement. Here are two runs of the programme.
3.2
1 g++ If.cpp -o if 2 $ ./if 3 Choose an integer 4 42 5 even 6 $ ./if 7 Choose an integer 8 69 9 odd
lines 3.2.4 and 3.4.8 are my input, the first time I chose an even number and the computer replied even, the second time an odd number and the computer replied odd. This works because the if statement at line 3.1.14 evaluates a boolean expression remainder==0 and the programme executes line 3.1.15 if the expression is true and line 3.1.17 if it is false. In the boolean expression we have ==; this means equals, it has the two = signs to distinguish it from the assignment operator. Using only one = when you mean to use two is an error that is easy to make and very hard to spot. Now, if the boolean expression is true, the statement after if is executed, if it is false, the statement after else executes. Further choices can be added using one or more else if and further statements by using braces.
Bigger.cpp
3.3
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 cout<<"Choose a big integer"<<endl; 9 int big; 10 cin >> big; 11 12 cout<<"Choose a small integer"<<endl; 13 int small; 14 cin >> small; 15 16 if(big<small) 17 { 18 cout<<"big is smaller than small: swapping"<<endl; 19 int temp=small; 20 small=big; 21 big=temp; 22 } 23 else if(big==small) 24 cout<<"equal"<<endl; 25 else 26 cout<<"big is bigger than small"<<endl; 27 28 cout<<"\nbig: "<<big<<" small: "<<small<<endl; 29 30 return 0; 31 }
So, if big<small is true, the programme executes the lines between the braces at line 3.3.17 and line 3.3.22; these lines swap big and small so that they are the right way around, note in passing that the int introduced in line 3.3.19 to do the swap only exists inside the scope of the braces, that is between line 3.3.17 and line 3.3.22. If big<small is false, the programme checks the next condition else if(big==small), if big==small is true then the statement at line 3.3.24 is executed, there is only one statement here so no braces are needed. Finally if neither of these is true, the statement following the else, line 3.3.26 is executed. Often you only need to do something if the boolean expression is true and nothing if it is false and, in fact, there is no syntactical requirement for the else statement if it is not needed.
There are other relational operators
Relational.cpp
3.4
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 cout<<"Choose the first integer"<<endl; 9 int first; 10 cin >> first; 11 12 cout<<"Choose the second integer"<<endl; 13 int second; 14 cin >> second; 15 16 if(first!= second) 17 cout<<"the integers are not equal"<<endl; 18 19 if(first>= second) 20 cout<<"the first integer is not less than the second"<<endl; 21 19 if(first<= second) 20 cout<<"the first integer is not greater than the second"<<endl; 29 30 return 0; 31 }
The != in line 3.4.16 is not equal, so (a!=b) will return true if a is not equal to b. The less than or equal to and greater than or equal to operators in lines 3.4.19 and 3.4.16 have the obvious meanings. The exclamation mark is used more generally for negation, so if expression is true !expression is false and vice versa.
Negation.cpp
3.5
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 bool aTrueThing=true; 9 10 if(aTrueThing) 11 cout<<"aTrueThing is true"<<endl; 12 13 if(!aTrueThing) 14 cout<<"not aTrueThing is true"<<endl; 15 else 16 cout<<"not aTrueThing is false"<<endl; 17 }
which gives
3.6
1 aTrueThing is true 2 not aTrueThing is false
Negation, !, is a logical operator; there are two more: && for and and || for or. They have their usual logical meaning: (a&&b) is true if a is true and b is true, it is false otherwise. (a||b) is true if a is true or b is true, so it is only false if a and b are both false.
Logic.cpp
3.7
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 cout<<"Choose the first integer"<<endl; 9 int first; 10 cin >> first; 11 12 cout<<"Choose the second integer"<<endl; 13 int second; 14 cin >> second; 15 16 if(first>0&&second>0) 17 cout<<"the integers are both positive"<<endl; 18 else if(first>0||second>0) 19 cout<<"the first multiplied by the second will be negative"<<endl; 20 else 21 cout<<"the integers are both negative"<<endl; 22 23 return 0; 24 }
A boolean expression with an && is evaluated in a lazy way: in (a&&b) the expression b is not evaluated if a is false, there is no need, since if a is false so must (a&&b) be false as well. This can be useful if evaluating b could cause an error when a is false.
In fact, it is harder than you might think to find simple statement that causes a run-time error, modern compilers give programmes that can deal gracefully with division by zero. The following contrived illustrative example exploits the fact that all statements return a value, so when you have a cout statement, as well as printing to screen it returns a value which casts to true; when you output the value of a boolean, it prints as 1 for true and 0 for false. Anyway, the point is checking the second boolean expression will print if the second part of the boolean expression is evaluated, something that only happens if number is positive.
LazyAnd.cpp
3.8
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 cout<<"Choose a number"<<endl; 9 int number; 10 cin >> number; 11 12 cout<<bool(cout<<"successfully outputing casts to true"<<endl)<<endl; 13 14 if(first>0&&cout<<"checking the second boolean expression"<<endl) 15 cout<<"The number is positive"<<endl; 16 17 return 0; 18 }
Exercises 3