Home >>C++ Tutorial >C++ if-else

C++ if-else

C++ If else

For making a Condition based Project we need if statement in C++. In order to test the condition, if statement is used in C++ programming.
Here are the various types of if statements in C++:

  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder

1. If statement in C++

This statement in C++ tests the condition and is only executed if the condition is found to be true.
Here is the syntax:

if(condition)
{    
//code to be executed    
}  

Here is an example for you to understand:

#include <iostream>  
using namespace std;  
int main () 
{  
   int num = 3;    
	if (num % 2 != 0)    
	{    
		cout<<"Given Number is odd number";    
	}   
   return 0;  
}  
Output : Given Number is odd number

Here is another example to test given number is positive or not

#include <iostream>  
using namespace std;  
int main () 
{  
   int num = 3;    
	if (num >= 0)    
	{    
		cout<<"Given Number is positive number";    
	}   
   return 0;  
}  
Output : Given Number is positive number

2. If-else statement in C++

This statement also works like all the statements in C++. If the provided condition is true then it is executes the if section and if that fails then it executes the else statement.

Here is the syntax:

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

Here is an example to check given number is even or odd number

#include <iostream>  
using namespace std;  
int main () 
{  
   int num = 4;    
	if (num % 2 != 0)    
	{    
		cout<<"Given Number is odd number";    
	} 
	else
	{
		cout<<"Given Number is even number";
	}
   return 0;  
}  
Output : Given Number is even number

Here is another example to test given number is negative or positive

#include <iostream>  
using namespace std;  
int main () 
{  
   int num = 3;    
	if (num >= 0)    
	{    
		cout<<"Given Number is positive number";    
	}   
	else
	{
		cout<<"Given Number is negative number";
	}
   return 0;  
}  
Output : Given Number is positive number

3. If-else-if statement in C++

This statement in C++ is verifies two conditions, if statement is executed when the provided condition is true and else statement is executed when the condition is false.
Here is an example that will make you understand it better:

Here is another example to test given number is negative or positive

#include <iostream>  
using namespace std;  
int main () 
{  
   int num = 3;    
	if (num == 1)    
	{    
		cout<<"Today is monday";    
	}   
	else if(num == 2) 
	{
		cout<<"Today is tuesday";
	}
	else if(num == 3) 
	{
		cout<<"Today is Wednesday";
	}
	else if(num == 4) 
	{
		cout<<"Today is Thursday";
	}
	else if(num == 5) 
	{
		cout<<"Today is friday";
	}
	else if(num == 6) 
	{
		cout<<"Today is Saturday";
	}
	else if(num == 7) 
	{
		cout<<"Today is sunday";
	}
	else
	{
		cout<<"wrong input";
	}
	
   return 0;  
}  
Output : Today is Wednesday

4. if-else-if ladder in C++

This statement in C++ generally executes one condition from the multiple statements.
Here is the syntax:

syntax:
if(condition1)
{    
//code to be executed if condition1 is true    
}
else if(condition2)
{    
//code to be executed if condition2 is true    
}    
else if(condition3)
{    
//code to be executed if condition3 is true    
}    
...    
else
{    
//code to be executed if all the conditions are false    
}    

No Sidebar ads