AppDividend
Latest Code Tutorials

C++ If-else Statements Example

0

If-else Statements in C++ Example Tutorial is today’s topic. When we need to execute a particular block of code only when a condition is met, we use if-else statements. When we execute statements on conditions, we call it decision making in C++. For decision making, there are four types of control statements named:

  1. If statements
  2. If else statements
  3. Nested if statements
  4. Nested if-else statements

If statements

This block of code is only executed when a condition in if block is true for every other condition the block of code doesn’t execute.

#Syntax for if statements

if(condition)
{
	block of code(statements);
}

Example

if(input_string="HI")
{
	cout<<"Hello";
}

Real-life example

Suppose we want to go out for a movie the first thing we will check before moving out is if it is raining or not(if condition) if it is raining we will not go to watch the movie(blocks inside if condition didn’t run because it is raining outside and the condition is not met).

If else statements:

The main block of code is executed if the condition inside if block is met otherwise the statements of else block gets executed automatically.

Syntax:

if(condition)
{
	statements;
}
else
{
	statements;
}

Example:

int a=5;
if(a>5)
{
	cout<<"a is greater than 5"<< endl;
}
else
{
	cout<<"a is less than 5"<< endl;
}

Real-life example

Suppose it is raining outside and if it is raining heavily we wear a raincoat otherwise we can go for an umbrella.

Nested if statements

When there is an if condition inside an if condition we call it nested if-else statements.

Syntax

if(condition)
{
	if(condition)
	{
		statements;
	}
}

Example

a=5;
if(a>0)
{
	cout<<"a is greater than 0"<<endl;
	if(a==5)
	{
		cout<<"a is equal to 5"<<endl;
	}
}

Real-life example

Suppose it is raining outside I will decide to go out only if it is raining slowly.

Nested if-else

We use nested if-else to check the multiple conditions. In the control structure, there is only one if or one else, but inside that, there are more if-else conditions present to check the hierarchy of conditions.

Syntax:

if(condition)
{
	statements;
}
else
{
	if(condition)
	{	
		statements;
	}
	else
	{
		statements;
	}
}

See the following example.

a=5;
if(a>0)
{
	if(a=5)
	{
		cout<<"Value of a is"<<a<<endl;
	}
	else
	{
		cout<<"Value of a is positive but not equal to 5"<<endl;
	}
}

Real-world example:

If we are going for a football match and we see Lionel Messi playing, we will support that team.

Flowchart

Control statements are a fundamental part of any programming language, and everyone should know of it to understand the mechanism of how the code is working and how one can change to code and give it a direction where we can reach a solution.

Flowchart

Example programs

Write a program to show the mechanism of only if statements.

#include<iostream>
using namespace std;
int main()
{
	int var=1;
	if(var==1)
	{
		cout<<"Hello everyone"<<endl;
	}
}

See the output.

 

Example programs

Write a program to show a mechanism of nested if statements.

#include<iostream>
using namespace std;
int main()
{
	int var=5;
	if(var>0)
	{
		if(var==5)
		{
			cout<<"Hello I am a positive number and my value is 5";
		}
	}
}

See the output.

 

If-else Statements in C++

Write a program to show the mechanism of if-else statements.

#include<iostream>
using namespace std;
int main()
{
	int var=5;
	if(var>0)
	{
		cout<<"Hello I am positive number"<<endl;
	}
	else
	{
		cout<<"Hello I am negative number"<<endl;
	}
}

See the output.

 

Write a program to show the mechanism of if-else statements.

Write a program to show the mechanism of nested if-else statements.

#include<iostream>
using namespace std;
int main()
{
	int var;
	cin>>var;
	if(var>0)
	{
		cout<<"Hello I am positive number"<<endl;
		if(var==5)
		{
			cout<<"hello I am positive and my value is 5"<<endl;
		}
		else
		{
				cout<<"Hello I am positive and my value is not equal to 5"<<endl;
		}
	}
	else
	{
		cout<<"Hello I am negative number"<<endl;
	}
	
}

See the output.

 

If statements in C++

Finally, If-else Statements in C++ Example Tutorial is over.

Recommended Posts

Multithreading In C++ Example Tutorial

Queues in C++ Example | C++ Standard Template Library

Multiple Multilevel and Hierarchical Inheritance in C++ Example

C++ Recursion Example | Recursion Program In C++ Tutorial

C++ Insertion Sort Example | Insertion Sort Program In C++

Polymorphism in C++ Example | C++ Polymorphism Tutorial

C++ Heap Sort Example | Heap Sort Program In C++

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.