Cpp - Selections With if-else


Demonstrates the use of if-else statements


#include <iostream> 
using namespace std; 
int main() //from  w  ww  . jav a  2  s  .c o  m
    float x, y, min; 

    cout << "Enter two different numbers:\n"; 
    if( cin >> x && cin >> y)  // If both inputs are 
    {                          // valid, compute 
      if( x < y )              // the lesser. 
          min = x; 
         min = y; 
      cout << "\nThe smaller number is: " << min << endl; 
      cout << "\nInvalid Input!" << endl; 

    return 0; 


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

if( expression ) 
[ else 
    statement2 ] 

expression is first evaluated, if the result is true, statement1 is executed and statement2 is executed in all other cases.

If there is no else and expression is false, the control jumps to the statement following the if statement.

Related Topic