C++ if and if-else statement

Page Contents:

If-statement in C++
If-else statement in C++
Nested if-else Statements
If-else Ladder or if-else if

If-statement in C++

If-statement structure in C++
If-statement structure in C++

The syntax for if-statement in C++

if(condition)
{
statement;
}

The if structure is a single-entry/single-exit structure. Here we see that the above diagrams for the remaining control structures also contain initial states, transition arrows, action states that indicate actions to perform, decision symbols(with associated guard condition) that indicate decisions to be made and final states. This is consistent with the action/decision model of programming we have been emphasizing. The if selection structure performs an indicated action only when the condition is true.

Program for if control structure:

include<iostream>
 using std::cout;
 int main()
 {
 int grade=62;
 if(grade>=60)
 {
 cout<<"This is if control Structure Program";
 }
 return 0;
 }

OUTPUT:

 This is if control Structure Program 

If-else statement in C++

IF-Else in C
If-Else in C
  • This is a very important statement used to check the condition and accordingly execute a set of statements, based on whether the condition is true or false.
  • In an if-statement, the first (or only) statement is executed if the condition is true and the second statement (if it is specified) is executed otherwise
  • If a condition evaluates to something different from a boolean, it is -if possible – implicitly converted to a bool
  • This implies that any or pointer expression can be used as a condition
  • If the condition specified in the if the statement is false then the statement of else will be executed
  • SYNTAX of the if-else statement:
if(condition)
 {
 statement 1;
 -
 }
  else
 {
 -
 statement 2;
 -
 }
  • The logical operators && || ! are most commonly used in these conditions
  • The operator && and || will not evaluate their second argument unless doing so is necessary
  • A branch of an if-statement cannot be just a declaration. if we need to introduce a name in a branch, it must be enclosed in a block

1.Example for If-else statement(Display even number)

#include<iostream> 
using namespace std;
  int main () 
 {
  int num = 10;
  if (num % 2 == 0)
  {
  cout<<"It is even number"; 
 }
  return 0;
  } 
 OUTPUT:
 It is even number

2. Example for if-else statement(Display odd number)

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

3. Find out the smaller number from two different number

 #include<iostream>
 using namespace std;
 int main()
 {
 float x,y,min;
 cout << "Enter two different numbers :\n"; if(cin>>x && cin>>y)
 {
 if(x<y)
 min=x;
 else
 min=y;
 cout<< "\nThe smaller number is:"<<min << endl;
 }
 else
 cout<< "\nInvalid Input !" << endl;
 return 0;
 }
 OUTPUT:
 Enter two different numbers:
 7.5  5.7
 The smaller number is : 5.7

The if-else statement can be used to choose between two conditional statements

When the program is run, the expression is first evaluated and the program control branches accordingly. If the result is true, statement1 is executed and statement2 is executed in all other cases, provided an else branch exists. If there is no else and expression is false, then control jumps to the statement following the if statement

Nested if-else Statements:

  • Multiple if-else statements can be nested
  • But not every if the statement has an else branch. To solve the resulting problem, an else branch is always associated with the nearest preceding if statement that does not have an else branch is always associated with the nearest preceding if statement that does not have an else branch

Syntax of the Nested if-else statement :

if (condition1)  
{    
// Executes when condition1 is true    
if (condition2)     
{      
 // Executes when condition2 is true    
} 
} 
  • In this example, the else branch belongs to the second if, as indicated by the fact that the statement has been indented
  • However, you can use a code block to redefine the association of an else branch

Program for Nested if-else in C++

#include<iostream> 
int main() 
{ 
 int i = 10; 
 
if (i == 10)  
{      
if (i < 15) 
printf("i is smaller than 15\n");      
if (i < 12)          
printf("i is smaller than 12\n");
else
printf("i is greater than 15");  }  return 0; 
 } 

OUTPUT:

i is smaller than 15 i is smaller than 12

Write a program on direction changing game

#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
char dir='a';
int x=10,y=10;
cout<< "Type Enter to quit\n";
while(dir!='\r')     //until enter is typed
{
cout<<"\nYour location is"<<x<<","<<y;
cout<<"\nPress direction key(n,s,e,w):";
dir=getche();  //get character
if(dir=='n')   //go to north
y--;
else
if(dir=='s')    //go to south
y++;
else
if(dir='e')    //go to east
x++;
else
if(dir=='w')   //go to west
x--;
}  //end while
return 0;
}  //end main

When the game starts, you find yourself on the barren moor. You can go one “unit” north, south, east, or west, while the program keeps track of where you are and reports your position, which starts at coordinates 10,10. Unfortunately, nothing exciting happens to your character, no matter where you go, the moor stretches almost limitlessly in all directions, as shown above

OUTPUT:

Your location is 10,10
Press direction key (n,s,e,w):n
Your location is 10,9
Press direction key (n,s,e,w):e
Your location is 11,9
Press direction key(n,s,e,w):

You can press the Enter key to exit the program.

This program may not cause a sensation in the video arcades, but it does demonstrate one way to handle multiple branches. It uses an if statement nested inside an if…else statement, which is nested inside another if…else statement, which is nested inside yet another if…else statement. If the first test condition false, the second one is examined, and so on until all four have been checked. If anyone proves true, the appropriate action is taken changing the x and y coordinate and the program exits from all the nested decisions. Such a nested group of if…else statement is called a decision tree.

If-else Ladder or if-else if

The If-else Ladder or if-else if the statement is used for the problem of multiple cases of a particular condition. In the if-else Ladder, the first condition is checked, if the first condition is true then the statement inside the if Control Structure is executed. But if the condition is False then it goes to else statement which contains if statement. The statement is executed if this condition is true. Else it again goes to the else and again checks the condition associated with this if statement. Hence if one condition is satisfied no other else is checked thereafter.

Always remember that in if-else Ladder condition else contain if-else statement

The syntax for if-else Ladder or if-else if

if(condition)
{
statements;
}
else
{
if(condition)
{
statements;
}
else
{
if(condition)
{
statements;
}
}
}
--
-----
------- so on...

Program for if-else Ladder

#include <iostream> 
using namespace std;   
int main () 
{    
int marks = 89;    
if( marks >= 70 )    
{        
cout << "First class" << endl;    
}    
else if( marks >= 50)    
{     
cout << "Second class" << endl;    
}    
else if( marks >= 40)    
{       
cout << "Third class" << endl;    
}    
else    
 {        
cout << "fail" << endl;    
}     
 return 0; 
} 

OUTPUT:

First class

Related Article

Recommended Post:

Translate »