C++ language

C++ Decision Making
Previous Home Next

In C++ programming we want the program to be executed sequentially. we want a set of instruction to be executed at one time and an entirely set of instruction to be executed in another situation. in this type of situation will have to use the decision control structure.

Decision making include following statements:-

  • If statement
  • Switch statement
  • Conditional Operator statement
  • goto statement

    If Statement:-Different form of If statement are:-

  1. Simple If statement
  2. If...else statement
  3. Nested If...else statement
  4. else If statement

Simple If statement:-If the condition is true then the statement inside block executed, otherwise, if condition is false then statement inside block is ignored.

 

General form:

if (condition)
{
 statement 1;
}
 else
{
 statement 2;
}

Example:

#include <iostream.h>

int main()
{
  int x,y;
  x=12;
  y=10;
  if(x>y)
{
  cout << "x is greater then y";
}
 else
{
  cout << "y is greater then x";
}
}
Output y is greater then x

Nested if...else statement:-If condition is false,then statement inside the block3 executed.If the expression is true,then statement1 executed,otherwise statement2 executed.

//General form:

if(condition)
{
   if(condition1)
  {
     statement1;
   }
   else
  {
     statement2;
   }
}
else
 {
 statement3;
 }

Example:

#include <iostream.h>

int main()
{
 int a,b,c;
 cout << "enter value of a,b,c";
 cin >> a >> b >> c;

 if (a>b)
{
  if (a>c)
 {
  cout << "a is greatest no.";
}
  else
{
  cout << "c is greatest no.";
}
}
else
{
  if (b>c)
{
  cout << "b is greatest no.";
}
else
{
  cout << "c is greatest no.";
}
}
}

else-if statement:-else if condition is nothing but this this a way to arrange the else with the if that follows it.

if(condition 1)
{
  statement 1;
}
else if(condition 2)
{
  statement 2;
}
else if(condition 3)
{
  statement 3;
}
else
  default statement;

Example:

#include <iostream.h>

int main()
{
  int x;

  cout << "Enter marks of student";
  cin >> x;

  if (x>60)
  {
    cout << "Grade A";
  }
  else if (x>50)
  {
    cout << "Grade B";
  }
  else if (x>40)
  {
    cout << "Grade C";
  }
  else
  {
    cout << "Failed";
  } 
}

Switch Statement:-It is used to replace if else if ladder method, but can be used for replacing comparission.

//General form:

switch (<variable>)
{
   case <constant_value 1>:
   statement 1;
   braek;

   case <constant_value 2>:
   statement 2;
   break;
      .
      .
      .
      .

    default:
    statement;
}

Example:

#include <iostream.h>

int main()
{

 int x;
 cout << "Enter number";
 cin >> x;

 switch (x)
 {
   case 1:
   cout << "sunday";
   break;

   case 2:
   cout << "Monday";
    break;
    
    case 3:
   cout << "Tuesday";
   break;
     
    default:
   cout << "Invalid number";
  }
}

Conditional Operator Statement(?:):-The conditional operator(?:) is also well known as ternary operator. This operator can be use to replace if else code.

//General form:

condition ? statement 1 : statement 2
If condition is true, statement 1 is executed.
If condition is false, statement 2 is executed.

Example:

Using if else statement:                                      
                                                                                                                 
If (a>b{
  cout << "a is greater";
}
else
{
  cout << "b is greater";
}

Goto Statement:-Goto statement is a jump statement. It is used to jump from one point to other in our function. we can not jump from one function to another. Jump points for goto are marked with labele statements. Label statement can be above or below the Goto statement.

//General form:
goto label 1;
     .
     .
label 1:
     .
     .
label 2:
     .
     .
goto label 2;

Example:

#include <iostream.h>

int main (void) // Print value 0 to 9
{
a = 1; 
loop:;  // label stament
cout << "a";
a++;
if (a < 10) goto loop ; // jump statement 
retrun 0;
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: