Decision-making is the process of determining the order in which statements should be executed based on certain conditions or of repeating a group of statements until certain specified conditions are met.
Programmers must specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is true, and optionally, additional statements to be executed if the condition is false, in order for decision making structures to work.
The general form of a typical decision-making structure found in most programming languages is shown below.
The following statements are supported by C++ for handling decision-making:
In an if
statement, a boolean expression is followed by one or more statements. Depending on the complexity of the conditions to be tested, the if statement can be implemented in a variety of ways.
The various forms are as follows:
The simplest statement for making a decision is the if statement. It is used to determine whether a specific statement or block of statements will be executed or not, i.e., whether a block of statements will be executed if a specific condition is true otherwise the block of statements is not executed.
syntax for simple if statements
if(expression)
{
statement-inside;
}
statement-outside;
If the expression is true, then 'statement-inside' is executed; otherwise, it is skipped and only 'statement-outside' is executed.
An example: Program to print a positive number entered by the user and suppose if the user enters a negative number, it will be skipped.
#include <iostream>
using namespace std;
int main() {
int number;
cout << " Please enter an integer: ";
cin >> number;
// checks whether if the given number is positive
if (number > 0) {
cout << " yes you entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
Output1:
Please enter an integer: 8 yes you entered a positive integer: 8 This statement is always executed.
When the user enters 8, the condition number > 0 is evaluated to true, and the statement contained within the body of if is executed successfully.
Output2:
Please enter an integer: -9 This statement is always executed.
When the user enters -9, the condition number > 0 is evaluated to be false, and the statement contained within the body of if is not executed.
if(expression)
{
statement-block1;
}
else
{
statement-block2;
}
If the 'expression' is true or returns true, the'statement-block1' is executed; otherwise, the'statement-block1' is skipped and'statement-block2' is executed.
If the condition is true, The code within the body of if is executed and the code within the body of else is skipped without executing.
Suppose If the condition is discovered to be false, The code contained within the body of else is executed and the code contained within the body of if is not executed.
For example: let us write a Program to check whether an integer is positive or negative and as per this program, 0 is considered a positive number.
#include <iostream>
using namespace std;
int main() {
int number;
cout << " Please enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You successfully entered a positive integer: " << number << endl;
}
else {
cout << "You successfully entered a negative integer: " << number << endl;
}
cout << "This is always printed..";
return 0;
}
Output1:
Please enter an integer: 2 You successfully entered a positive integer: 2 This is always printed..
The condition number >= 0 is present in the preceding program. If we enter a number that is greater than or equal to zero, then the condition is true.
suppose we entered an integer 2, so the condition has been met. As a result, the statement within the body of if is successfully executed.
Output2:
Please enter an integer: -1 You successfully entered a negative integer: -1 This is always printed..
suppose we entered an integer -1, so the condition has not been met. As a result, the statement within the body of else is successfully executed.
if(expression 1)
{
statement-block1;
}
else if(expression 2)
{
statement-block2;
}
else if(expression 3 )
{
statement-block3;
}
else
default-statement;
The expression is put to the test from the top to down. The statement associated with the true condition is executed as soon as it is discovered.
For an example,
let us write a C++ program to find if an integer is positive, negative, or zero using else if statements.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
} else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
} else {
cout << "You entered 0." << endl;
}
cout << "This is always printed..";
return 0;
}
Output:
Enter an integer: 2 You entered a positive integer: 2 This is always printed.. Enter an integer: -1 You entered a negative integer: -1 This is always printed.. Enter an integer: 0 You entered 0. This is always printed.
If the value is greater than zero, the code contained within the if block is executed. If the value is less than zero, the code contained within the else if block is executed. Otherwise, the code contained within the else block is executed.
We may need to use an if statement inside another if statement at some times. This is referred to as a nested if statement.
syntax for Nested if statements
if (expression) {
if (expression1) {
statement - block1;
} else {
statement - block2;
}
} else {
statement - block3;
}
If 'expression' is false or returns false, the ‘statement-block3' is executed; otherwise, execution enters the if condition and checks for 'expression 1'. Then, if 'expression 1' is true or returns true,'statement-block1' is executed; otherwise,'statement-block2' is executed.
For an example
let us write a C++ program to find if an integer is positive, negative, or zero using nested if statements.
#include <iostream>
using namespace std;
int main() {
int num;
cout << " Please enter an integer: ";
cin >> num;
// outer if condition
if (num != 0) {
// inner if condition
if (num > 0) {
cout << "yes the number is positive." << endl;
}
// inner else condition
else {
cout << "yes the number is negative." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
cout << "This is always printed.." << endl;
return 0;
}
Output:
Please enter an integer: 4 yes the number is positive. This is always printed.. Please enter an integer: -9 yes the number is negative. This is always printed.. Please enter an integer: 0 The number is 0 and it is neither positive nor negative. This is always printed..
In the above program; We accept an integer from the user and store it in the variable num. Then, we use an if...else statement to see if the num is not equal to zero.
The inner if...else statement determines whether the input number is positive, i.e. whether the num is greater than zero.
The switch statements, conditional operator statements, and goto statements which are supported by C++ for handling decision-making will be discussed in the next tutorial.