if else statements in c++

if…else – Nested if…else | C++ Programming

In this tutorial, we will learn about the if, if…else and Nested if…else statements to create decision-making programs with the help of examples.

The if…else statement is used in computer programming to run one block of code under one set of conditions and another set of block code under a different set of conditions.

:: As an example, issuing grades (A, B, C) depending on a student’s performance.

  • If the percentage is greater than 90%, give the student an A.
  • If the percentage is greater than 70, give a B.
  • If the percentage is greater than 60, give a C.

In C++, there are three types of if…else statements.

  1. if condition
  2. if…else condition
  3. if…else else…if condition

if statement in C++

If the statement has the following syntax:

if (condition) {

}

  • The code inside the body of if is executed if the condition evaluates to true.
  • The code inside the body of if is skipped if the condition evaluates to false.

C++ supports the following mathematical logical conditions:

Less thana < b
Less than or equal toa <= b
Greater thana > b
Greater than or equal toa >= b
Equal toa == b
Not Equal toa != b
Developer’s Dome

The conditional statements in C++ are as follows:

ifIf a specified condition is true, a block of code will be executed.
elseif the same condition is false, else to define a code block to be performed
else ifIf the initial condition is false, use else if to define a new condition to test.
switchthe switch is used to specify a large number of alternative code blocks to be executed.
#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;
    }
    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.

When the user enters 5, the condition number > 0 is assessed as true, and the if statement is executed.

Output 2:

Enter a number: -5
This statement is always executed.

The condition number > 0 is assessed to false when the user enters -5, and the statement inside the body of the if is not executed.

if…else in C++

An optional else clause can be added to the if statement. Its syntax is as follows:

if (condition) {
    // block of code if condition is true
}
else {
    // block of code if condition is false
}

If the condition is true, then

  • If the code in the body of the if statement is performed, the code in the body of the otherwise statement is bypassed.
  • If the condition is found to be false,
  • else executes the code contained within its body.
  • The code in the body of the if statement is not executed.
#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 1:

Enter an integer: 2
You entered a positive integer: 2
This line is always printed.

The condition number >= 0 is present in the above program. The condition evaluates true if the number is larger than or equal to 0.

We’ve arrived at number four. As a result, the situation is correct. As a result, the if statement inside the body gets executed.

Output 2:

Enter an integer: -2
You entered a negative integer: -2
This line is always printed.

Nested If Else:-

In C, a nested if statement is one that is the target of another if statement. An if statement inside another if statement is referred to as nested if statements. Yes, both C and C++ allow us to nest if statements within if statements, allowing us to put one if statement inside another.

Syntax:

if (condition 1)
{
// Executes when condition 1 is true
if (condition 2)
{
// Executes when condition 2 is true
}
}

// C program to illustrate nested-if statement
#include <stdio.h>

int main() {
	int i = 10;

	if (i == 10)
	{
		
		if (i < 16)
		printf("i is smaller than 16\n");
		if (i < 12)
			printf("i is smaller than 12 too\n");
		else
			printf("i is greater than 16");
	}

	return 0;
}

Output:

i is smaller than 16
i is smaller than 12 too

Using Ternary Operator

The C++ ternary operator allows you to build a single-line if statement. The result of a ternary operator can be either true or false. The ternary operator is used to solve this problem. To make your code easier to read, the C++ ternary operator is used to replace simple if…else statements.

#include <iostream>
using namespace std;

int main()
{int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
}

Output:

Good evening

if-else-if ladder

A user can choose from a variety of alternatives here. The if statements in C are executed in a top-down fashion. The statement associated with that if is performed as soon as one of the conditions controlling the if is true, and the rest of the C else-if ladder is bypassed. The final else line will be executed if none of the conditions are true.

Syntax:

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

//if-else-if ladder
#include<iostream>
using namespace std;

int main()
{
	int i = 15;

	if (i == 10)
		cout<<"i is 10";
	else if (i == 15)
		cout<<"i is 15";
	else if (i == 20)
		cout<<"i is 20";
	else
		cout<<"i is not present";
}

Output:

i is 15

You may like:

Type Conversion In C++ Programming

Data Types in C++

Leave a Reply