C++ Selection Statements

Statements are executed sequentially in a C++ program. It is also known as normal flow of control. But the normal flow of the program can be changed depending on the conditional statements. This statement is also called selection statement.

C++ provides two types of selection Statement: if and switch. In some conditions we use the conditional operator ?: instead of the if statement.

C++ If Statements

If statement is used to test a particular situation, if the condition evaluates to true, a course of action is performed, i.e. a statement or set of statement is executed. If the situation evaluates to false, then a course of action is ignored.

Syntax :

if (expression)
    statement 1;

If the expression evaluates to true then the statement 1 will be executed and if the expression evaluates to false the statement 1 will be ignored.



Example :
if(ch == '')
  space++;


Output :

It will check whether the character variable stores a space; If it does, the number of spaces will increase by 1.


C++ If-Else

What if there is another course of action to be followed if the expression evaluate false. Then there is another form that is used : If-Else.

Syntax :

if (expression)
   statement 1;
else
   statement 2;

If the expression evaluates to true then the statement 1 will be executed and if the expression evaluates to false the statement 2 will be executed.



Example :

int a=5, b=3
if(a==b)
  a=a+b;
else
  a=a-b;
cout << a << "\n";
}


Output :
2


In this if the value of a and b are equal then the if condition evaluates to be true then the a=a+b; statement will be executed and if the value of a and b are not equal then the if condition evaluates to be false the a=a-b; statement will be executed.


C++ If-Else-If Ladder

The common programming construct in C++ is the if-else-if ladder, often called the if-else-if staircase. The syntax of If-Else-If Ladder statement is:

Syntax :

if (expression)    statement 1;
else
if (expression)    statement 2;
else
if (expression)    statement 3;
else
   statement 4;


Example :
  
int a=5, b=3
if(a>=b)
  a=a+b;
else  if(a < b)
  a=a-b;
cout << a << "\n";
}  


Output :
2

Exercise:-

1. Which of the following are incorrect statements?
If int a=10

  1) if( a==10 ) printf("IncludeHelp");
  2) if( 10==a ) printf("IncludeHelp");
  3) if( a=10 ) printf("IncludeHelp");
  4) if( 10=a ) printf("IncludeHelp");

A. 3 and 4.
B. 3 only.
C. 4 only.
D. 2,3 and 4.

View Answer


2.

char val=1;
if(val--==0)
   printf("TRUE");
else
   printf("FALSE");  
   

A. FALSE
B. Error
C. TRUE
D. None

View Answer



Program :-

C++ Program to Find Largest Number Among Three Numbers

#include <iostream>
using namespace std;
int main()
{    
    float n1, n2, n3;
    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;
    if(n1 >= n2 && n1 >= n3)
    {
        cout << "Largest number: " << n1;
    }
    if(n2 >= n1 && n2 >= n3)
    {
        cout << "Largest number: " << n2;
    }
    if(n3 >= n1 && n3 >= n2) {
        cout << "Largest number: " << n3;
    }
    return 0;
}


Output :

Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3





Visit :


Discussion


* You must be logged in to add comment.