Search

1. C++ if, if...else and Nested if...else

In this article, you will learn to create decision making statements in a C++ program using different forms of if..else statement.

Contents
if Statement
if...else Statement
Nested if...else Statement
Conditional Operator

C++ if Statement

if (testExpression) 
{
   // statements
}
The if statement evaluates the test expression inside parenthesis.
If test expression is evaluated to true, statements inside the body of if is executed.
If test expression is evaluated to false, statements inside the body of if is skipped.

How if statement works?


Flowchart of if Statement


Example 1: C++ if Statement

// Program to print positive number entered by the user
// If user enters negative number, it is skipped
 
#include <iostream>
using namespace std;

int main() 
{
    int number;
    cout << "Enter an integer: ";
    cin >> number;

    // checks if the number is positive
    if ( number > 0) 
    {
        cout << "You entered a positive integer: " << number << endl;
    }

    cout << "This statement is always executed.";
    return 0;

}
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
Output 2
Enter a number: -5
This statement is always executed.

C++ if...else

The if else executes the codes inside the body of if statement if the test expression is true and skips the codes inside the body of else.
If the test expression is false, it executes the codes inside the body of else statement and skips the codes inside the body of if.

How if...else statement works?


Flowchart of if...else


Example 2: C++ if...else Statement

// Program to check whether an integer is positive or negative
// This program considers 0 as positive number

#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
    {
        cout << "You entered a negative integer: " << number << endl;
    }

    cout << "This line is always printed.";
    return 0;
}
Output
Enter an integer: -4
You entered a negative integer: -4.
This line is always printed.

C++ Nested if...else

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

Syntax of Nested if...else

if (testExpression1) 
{
   // statements to be executed if testExpression1 is true
}
else if(testExpression2) 
{
   // statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3) 
{
   // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true
}
.
.
else 
{
   // statements to be executed if all test expressions are false
}

Example 3: C++ Nested if...else

// Program to check whether an integer is positive, negative or zero

#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 line is always printed.";
    return 0;
}
Output
Enter an integer: 0
You entered 0.
This line is always printed.

Conditional/Ternary Operator ?:

A ternary operator operates on 3 operands which can be used instead of a if...else statement.
Consider this code:
if ( a < b ) {
   a = b;
}
else {
   a = -b;
}
You can replace the above code with:
a = (a < b) ? b : -b;
The ternary operator is more readable than a if...else statement for short conditions.

2. C++ for Loop

Loops are used in programming to repeat a specific block of code. In this tutorial, you will learn to create a for loop in C++ programming (with examples).


Loops are used in programming to repeat a specific block until some end condition is met. There are three type of loops in C++ programming:
  1. for loop
  2. while loop
  3. do...while loop

C++ for Loop Syntax

for(initializationStatement; testExpression; updateStatement) {
    // codes 
}

How for loop works?

  1. The initialization statement is executed only once at the beginning.
  2. Then, the test expression is evaluated.
  3. If the test expression is false, for loop is terminated. But if the test expression is true, codes inside body of for loop is executed and update expression is updated.
  4. Again, the test expression is evaluated and this process repeats until the test expression is false.

Flowchart of for Loop in C++


Example 1: C++ for Loop

// C++ Program to find factorial of a number
// Factorial on n = 1*2*3*...*n

#include <iostream>
using namespace std;

int main() 
{
    int i, n, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for (i = 1; i <= n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;
}

Output
Enter a positive integer: 5
Factorial of 5 = 120
In the program, user is asked to enter a positive integer which is stored in variable n (suppose user entered 5). Here is the working of for loop:
  1. Initially, i is equal to 1, test expression is true, factorial becomes 1.
  2. i is updated to 2, test expression is true, factorial becomes 2.
  3. i is updated to 3, test expression is true, factorial becomes 6.
  4. i is updated to 4, test expression is true, factorial becomes 24.
  5. i is updated to 5, test expression is true, factorial becomes 120.
  6. i is updated to 6, test expression is false, for loop is terminated.
In the above program, variable i is not used outside of the for loop. In such cases, it is better to declare the variable in for loop (at initialization statement).
#include <iostream>
using namespace std;

int main() 
{
    int n, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;
}