Introduction to Programming and C++

Contents Previous Topic Next Topic

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 console in 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 or false. 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.

Program 1. Read an integer and print something as a result.

#include <iostream>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n == 0 ) {
		cout << "You entered zero." << endl;
	}

	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.

Comment: 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.

Relational Operators

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:

OperatorExampleExplanation
==n == 0are the two values equal
!=n != 0are the two values different
<n < 0is the left-hand side less than the right-hand side
<=n <= 0is the left-hand side less than or equal to the right-hand side
>n > 0is the left-hand side greater than the right-hand side
>=n >= 0is 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 xy: x is greater than (>) or equal to (=) y. Thus, > comes first. You can usually think of ! as meaning not in C++.

Else Statements

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>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n == 0 ) {
		cout << "You entered zero." << endl;
	}

	if ( n != 0 ) {
		cout << "You entered " << n << " which is not zero." << endl;
	}

	return 0;
}

To avoid this, we can add an else clause, that is, a sequence of statements which is run if the test returns false. This is demonstrated in Program 3.

Program 3. Read an integer and print something as a result.

#include <iostream>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n == 0 ) {
		cout << "You entered zero." << endl;
	} 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.

Program 4. A program which comments on the sign of the entered integer.

#include <iostream>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n == 0 ) {
		cout << "You entered zero." << endl;
	} 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;
}

It is sometimes convenient, and possible, to write Program 4 as is shown in Program 5.

Program 5. A modified Program 4.

#include <iostream>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n == 0 ) {
		cout << "You entered zero." << endl;
	} else if ( n > 0 ) {
		cout << "You entered " << n << " which is positive." << endl;
	} else {
		cout << "You entered " << n << " which is negative." << endl;
	}

	return 0;
}

You can read Program 5 as:

"Prompt the user for an integer.
 Store whatever the user enters in the variable n.
 If n equals zero, indicate this,
 else if n is greater than zero, indicate this,
 else indicate that n is negative."

Example

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.

Program 6. Reading and commenting on a character.

#include <iostream>

using namespace std;

int main() {
	char input = 0;

	cout << "Please enter a character (only the first will be read): ";
	cin >> input;

	if ( ( input >= 'a' ) && ( input <= 'z' ) ) {
		cout << "You entered a lower-case letter." << endl;
	} else if ( ( input >= 'A' ) && ( input <= 'Z' ) ) {
		cout << "You entered an upper-case letter." << endl;
	} else if ( ( input >= '0' ) && ( input <= '9' ) ) {
		cout << "You entered a number." << endl;
	} else {
		cout << "You entered the non-alpha-numeric character "
		     << intput << "." << endl;
	}

	return 0;
}

Recall previously that && means and, 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' and the input is less than or equal to 'z'".

Example

If we want to print out whether the magnitude of an integer n is large (|n| > 100), we could write this as is shown in Program 7.

Program 7. Commenting on large integers.

#include <iostream>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n < -100 ) {
		cout << "You entered a big negative integer." << endl;
	} else if ( ( n >= -100 ) && ( n <= 100 ) ) {
		cout << "You entered a small integer." << endl;
	} else if ( n > 100 ) {
		cout << "You entered a big positive integer." << endl;
	}

	return 0;
}

Here, we make a few observations:

By the second test, we are guaranteed that n >= -100. Therefore, we can replace the line

	} else if ( ( n >= -100 ) && ( n <= 100 ) ) {

with

	} else if ( n <= 100 ) {

Similarly, once we get to the third test, we are guaranteed that the number must be greater than 100, and therefore we can replace

	} else if ( n > 100 ) {

with

	} else {

This results in more readable code, as is shown in Program 8.

Program 8. Modifications to Program 7.

#include <iostream>

using namespace std;

int main() {
	int n = 0;

	cout << "Please enter an integer: ";
	cin >> n;

	if ( n < -100 ) {
		cout << "You entered a big negative integer." << endl;
	} else if ( n <= 100 ) {
		cout << "You entered a small integer." << endl;
	} else {
		cout << "You entered a big positive integer." << endl;
	}

	return 0;
}

Questions

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."


Contents Previous Topic Top Next Topic