System ProgrammingLearn about Decision Making in C++ Programming

Learn about Decision Making in C++ Programming

Life never goes one way. Our daily life is full of situations and conditions that forces us to make decisions. Even the simplest ones that seem to be trivial also involve some decision making. Where will I have my dinner today? At home or out at a restaurant? This is a decision! Which major should I choose for my study? Another decision (but an important one this time).

This is our life, and the computer world (and programming is the heart of computers) is not apart from all this life. For this reason, the topic of Decision Making is of utmost importance. This is going to be the subject of this article. So, charge your batteries and follow me!

The if Statement

The most popular decision-making tool really, the if statement. In its simplest form, here is the syntax of if statement:

if (CONDITION)
	single_statement;

if (CONDITION)
{
	statement1;
	statement2;
	…
	statementn;
}

So, if the code block depending on the condition of the if statement consists of a single statement, C/C++ is nice enough not to ask about curly braces { } (although we can put them without problems).

Example
The following program asks the user to enter two numbers and divides them. The program should check if the divisor is 0 (to prevent the division by zero operation which is not allowed).

1

Let’s see what we will get when executing this program.

2

This is when inputs are legal. What if the user enters 0 for the divisor?

3

Exactly as expected. Although, it is a very simple example, there are several conclusions to draw out of it:

4

• Besides to the preprocessor directive #include we used before in our previous programs, you will notice another two include statements in Lines 2 and 3. One of them inserts the header file conio.h that is necessary for the functions clrscr() and getch() to work without problems. The other include statement inserts the header file process.h which is needed for the function exit() to work.

• Line 6: declares two variables a and b of type float.

• Line 7: uses the built-in function clrscr() to clear the output window.

• Line 10: the if statement. It checks if the variable b is equal to 0.

• Lines 11 and 15: the delimiters of the if statement code block.

• Lines 12-14: the code block that will be executed if the condition evaluates to true.

o Line 12: prints an error message.
o Line 13: uses the getch() function to suspend the output window until the user presses any key.
o Line 14: uses the exit() function to exit the program.

• If the condition is not met (b is not equal to 0), Lines 16 and 17 will be executed.
• Line 16: performs the division operation and prints the results.
• Line 17: keeps the output window open until the user presses any key.

if .. else Statement

The else statement gives us the capability to execute something if the condition is not met.
Syntax

if (CONDITION)
{
	statement1;
	statement2;
	…
	statementn;
}
else
{
	statement1;
	statement2;
	…
	statementn;
}

Example
The following example accepts an integer from the user, and decides whether this integer is odd or even.

5

Let’s see it in action.

6

7

Everything is normal, except one new thing, which is using more than one statement in one line.

cout << x << " is an Even number"; getch();

C/C++ allows you to use more than one statement in a line without any problems.

Learn the Basics of C Programming Language

if .. else if Statement

What if the probabilities are more complex than just a condition to be met or not?! What if the logic of the program requires another check if the first has failed?! In this case, the if..else structure will not be sufficient. For this reason, the if..else if comes to light.

Syntax

if (CONDITION1)
{
	statement1;
	statement2;
	…
	statementn;
}
else if (CONDITION2)
{
	statement1;
	statement2;
	…
	statementn;
}
else
{
	statement1;
	statement2;
	…
	statementn;
}

I think the logic is clear enough not to require much explanation. Instead, I prefer to illustrate it by an example.

Example
Consider a program that display a menu of choices, and asks the user to enter his choice. Obviously, the choice should be a positive number, AND limited to a specific range of values. Based on the user’s choice (input integer), the program will behave accordingly.

Check the following C++ program:

8

Let’s execute it and see what we get:

• In case of wrong input:

9

• In case the user enters 1 or 2:

10

• In case the user enters 99 (to exit):

11

The only point I want to make clear in this program is the usage of compound expressions. The logical AND operator && has combined two simple relational expressions into a complex expression:

if(choice>=1 && choice<3)

This means:
Execute the if body only if the value of choice is greater than or equal to 1 AND less than 3.

Summary

• Decision Making is based on whether a condition (simple or composite) is met or not.
• if statements control the flow of the program.
• if statements use conditions to decide whether a specific block of code will be executed or not.
• There are three forms of the if condition:
o If
o If .. else
o If .. else if .. else

In the next article, we are going to continue our talk on Decision Making by discussing the switch statement and the conditional operator. See you there.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -