Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The programs which we have seen until now contain a sequence of statements which are executed. In
some cases, we need to be able to control what happens.
Program 1 introduces two new concepts: cin, or onsole which reads information from the keyboard,
and a selection statement, that is, the statement is able to select whether a piece of code is executed.
The cin statement waits for the user to type some characters at the prompt. Once the user types
something and presses Enter, cin tries to convert the input into the given type, in this case, an integer.
After this, we have the keyword if followed by a statement in parentheses: ( ). The statement is a test
which either evaluates to true orfalse. If the statement evaluates to true, the statements inside the
following braces {} are executed, otherwise they are skipped. In Program 1 we test if the integer the user
entered is 0, in which case, the statement starting with cout is printed. Otherwise, nothing happens.
#include <iostream>
int main() {
int n = 0;
cin >> n;
if ( n == 0 ) {
}
return 0;
In the CPU, if the test n == 0 fails, that is, returns false, then the instructions which print to the screen
are skipped.
What happens when you try to enter something which is not an integer? Try 5, 5a, and a.
È Note that I use a lot of spaces. This makes the source code more readable. If you get used to
hitting the space bar after every variable name, after every bracket, etc., you very quickly begin doing
this automatically. You will thank yourself afterwards.
In the preceeding example, we used == to test if n was equal to 0. This is not assignment: if we wanted
to assign 0 to n, we should use n = 0.
There five other operators which test the relationship between two values:
< n<0 is the left-hand side less than the right-hand side
<= n <= 0 is the left-hand side less than or equal to the right-hand side
> n>0 is the left-hand side greater than the right-hand side
>= n >= 0 is the left-hand side greater than or equal to the right-hand side
If you get confused between using >= or =>, always remember how you say
ш :
is greater than (>) or
equal to (=) . Thus, > comes first. You can usually think of ! as meaning in C++.
c
In Program 1, we only print something if the statement is true. We could write a second if statement to
test the alternative case, as is shown in Program 2, however, this could be tedious.
Program 2. Asking the user for an integer and printing an appropriate comment.
#include <iostream>
int main() {
int n = 0;
cin >> n;
if ( n == 0 ) {
if ( n != 0 ) {
cout << "You entered " << n << " which is not zero." << endl;
return 0;
To avoid this, we can add an clause, that is, a sequence of statements which is run if the test
returns false. This is demonstrated in Program 3.
#include <iostream>
using namespace std;
int main() {
int n = 0;
cin >> n;
if ( n == 0 ) {
} else {
cout << "You entered " << n << " which is not zero." << endl;
return 0;
We can add further tests to be more responsive to what the user enters, as is shown in Program 4.
#include <iostream>
int main() {
int n = 0;
cin >> n;
if ( n == 0 ) {
} else {
if ( n > 0 ) {
cout << "You entered " << n << " which is positive." << endl;
else {
// n cannot be zero
cout << "You entered " << n << " which is negative." << endl;
return 0;
#include <iostream>
int n = 0;
cin >> n;
if ( n == 0 ) {
} else if ( n > 0 ) {
cout << "You entered " << n << " which is positive." << endl;
} else {
cout << "You entered " << n << " which is negative." << endl;
return 0;
As another example, recall that it was mentioned that a char stores a number between 0 and 255. Thus,
each character may be interpreted as a number. Take a look at Program 6, try to determine what will
happen, and then run it.
The operands to the logical AND operator need not be of the same type, but they must be of integral or
pointer type. The operands are commonly relational or equality expressions.
The first operand is completely evaluated and all side effects are completed before continuing
evaluation of the logical AND expression.
The logical OR operator (||) returns the boolean value if either or both operands is and
returns
otherwise. The operands are implicitly converted to type bool prior to evaluation, and the
result is of type bool. Logical OR has left-to-right associativity.
The operands to the logical OR operator need not be of the same type, but they must be of integral or
pointer type. The operands are commonly relational or equality expressions.
The first operand is completely evaluated and all side effects are completed before continuing
evaluation of the logical OR expression.
The second operand is evaluated only if the first operand evaluates to false (0). This eliminates needless
evaluation of the second operand when the logical OR expression is true.
#include <iostream>
int main() {
char input = 0;
cout << "Please enter a character (only the first will be read): ";
} else {
return 0;
Recall previously that && means V, that is, both the first and second statements must be true. Thus,
the first selection statement may be read as "if the input is greater than or equal to 'a' the input is
less than or equal to 'z'".
If we want to print out whether the magnitude of an integer is large (|| > 100), we could write this as
is shown in Program 7.
#include <iostream>
int main() {
int n = 0;
cin >> n;
if ( n < -100 ) {
return 0;
By the second test, we are guaranteed that n >= -100. Therefore, we can replace the line
with
Similarly, once we get to the third test, we are guaranteed that the number must be greater than 100,
and therefore we can replace
with
} else {
#include <iostream>
int main() {
int n = 0;
cin >> n;
if ( n < -100 ) {
} else {
return 0;
1. Write a program similar in structure to Program 5, however, prompt he user for a real number and
store it as a double. Next, indicate whehter the number is less than, equal to, or greater than
у 3.1415926535897932385. Note that C++ does not use unicode, and therefore, you must use the
letters pi to represent .
2. Suppose that in Program 7 that both the first and last clauses of printed out "You entered a big
integer." whether it was big and negative or big and positive. Use or (||) to combine the two tests into a
single test and then, in the else clause, simply print "You entered a samll integer."