Decision making in C++

1. Control statement:
  • All the instructions are executed sequentially by default when no repetitions of some calculations are necessary.
  • In some situation, we may have to change the execution order of statements based on condition or to repeat a set of the statement until certain conditions are met.
Tyes:
  1. If statement
  2. If – else statement
  3. Nested if....else statement
  4. If ....else ladder
  5. Switch case
1. If statement:
It is used to check the condition and if true the condition then it executes.
Syntax:

 if (condition)
{
          True statement;
}

Example program for if:

#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
cout<<"\n Enter a value:");
cin>>a;
if (a>=5)
    {
          cout<<"\n The entered number "<<a<<is greater than 5";
     }
getch();
}

Output:
if condition:
False case:
2. If – else statement:
It is used to check both conditions i.e. true and false.
It executes only one condition at a time.
Syntax:

if (condition)
    {
          True statement
     }
else
    {
          false statement;
     }

Example program for if-else:

#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
cout<<"\n Enter a value:");
cin>>a;
if (a>5)
      {
   cout<<"\n The entered number "<<a<<" is greater than 5";
       }
else
      {
  cout<<"\n The entered number "<<b<<is less than 5";
      }
getch();
}

Output:
if condition:
else condition:
3. Nested if....else statement:
Multiple if-else conditions are validating i.e. within condition check first after that execute.
Syntax:

if(condition 1)
   {
          if(condition 2)
              {
                   True statement2;
               }
          else
              {
                   False statement2;
               }
   }
else
    {
          False statement1;
     }

Example program for nested if...else:

#include<iostream.h>
#include<conio.h>
void main()
{
int  a,b;
clrscr();
cout<<"\n Enter a result:\n 1.pass\n 2.fail\n press 1 or 2\n");
cin>>a;
if (a==1)
     {
 cout<<"\n Enter total mark:");
cin>>b;
if(b>450)
   {
cout<<"\n You can get first group i.e. maths group");
    }
else
   {
cout<<"\n You can get all group except the first group");
   }
      }
else
    {
cout<<"\n Passed student only can join the 11th standard");
     }
getch();
}

Output:
if-if condition
if-if-else case:
else condition:
4. The if...else ladder:
It is similar to nested if but it checks continually.
Syntax:

if(condition)
   {
          Statement1;
    }
else if(condition 2)
   {
          Statement2;
    }
else if(condition 3)
   {
          Statement3;
    }
else
   {
          Statement4;
   }

Example program for if...else ladder:

#include<iostream.h>
#include<conio.h>
void main()
{
int  a,b,c;
clrscr();
cout<<"\n Enter 3 values: ");
cin>>a>>b>>c;
if( (a>b) &&(a>c))
  {
cout<<"\n Biggest value "<<a<<" is a";
  }
else if(b>c)
  {
cout<<"\n Biggest value "<<b<<" is b";
   }
else
  {
cout<<"\n Biggest value "<<c<<"  is c";
   }
     getch();
}

Output:
if condition:
else if  condition:
else condition:
5. Switch case:
It is used to execute a particular group of a statement .
Syntax:

 switch(expression)
  {
          case condition 1:
          statement1;
          break;
          case condition 2:
          statement2;
          break;
          .........
          .........
          default
          statements
          break;
  }

Example program for switch case:

#include<iostream.h>
#include<conio.h>
void main()
{
int  choice;
clrscr();
cout<<"\n Enter a choice 1 or 2";
cin>>choice;
switch(choice)
  {
case 1:
cout<<"\n You are chooses option 1 ";
break;
case 2:
cout<<"\n You are chooses option 2 ";
break;
default:
cout<<"\n You can only entered 1 or 2 choice ";
break;
  }
getch();
}

Output:
Case 1:
Case 2:
Default case:
2. Branching and looping:
It is used to execute continually until the condition becomes false.
Types:
  • While
  • Do while
  • For
1. While:
It first checks the condition then executes the result until becomes false.
Syntax:

 while (condition)
{
          Statements;
}

Example program for while condition:

#include<iostream.h>
#include<conio.h>
void main()
{
int  i=1,sum=0;
clrscr();
while(i<=5)
      {
sum=sum+i;
i++;
      }
cout<<"\n The sum of numbers upto 5 is : "<<sum;
getch();
}

Output:
2. do while:
It is first to perform some operation after that check the conditions.
It's opposite to the while condition.
Syntax:

do
{
          Statements;
}
while (condition);

Example program for do while condition:

#include<iostream.h>
#include<conio.h>
void main()
{
int  i=1,sum=0;
clrscr();
do
  {
sum=sum+i;
i++;
  }
while(i<=5);
cout<<"\n The sum of numbers upto 5 is : "<<sum;
getch();
}

Output:
3. for loop:
It is used to execute a set of instructions repeatedly until the condition becomes false.
Syntax:

 for(initialization ;condition ;increment or decrement)
{
          Statement;
}

Example program for for loop:

#include<iostream.h>
#include<conio.h>
void main()
{
int  i;
clrscr();
for(i=1;i<=10;i++)
    {
cout<<i<<endl;
     }
getch();
}

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me