Friday, 9 September 2016

C++ decision making structure Tccicomputercoaching.com



C++ decision making structure Tccicomputercoaching.com


C++ Decision making structure allows you to make decision based on condition.
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
C++ allows following statements as decision making structure:
If statement :
This includes only one if statement which contains one if condition, if it becomes true then true statement executes.
if( expression )
{
statement-inside;
}
statement-outside;

If-else statement :
This includes one condition, if it becomes true then executes true statement and if condition false, then false statement executes.
if( expression )
{
statement-block1;
}
else
{
statement-block2;
}

Nested if-else statement :
This contains if-else with another if-else. First if condition becomes true then second if condition checks, if it will true then its true statement executes otherwise its false statement executes.
if( expression )
{
if( expression1 )
   {
statement-block1;
   }
else
   {
statement-block2;
   }
}
else
{
statement-block3;
}

Else-if Ladder statement :
This contains more than one if conditions, which condition becomes true, its statement executes.

if(expression 1)
{
statement-block1;
}
else if(expression 2)
{
statement-block2;
}
else if(expression 3 )
{
statement-block3;
}
else
default-statement;


Example:

void main( )
{
int a;
cout<< "enter a number";
cin>> a;
if( a%5==0 && a%8==0)
 {
cout<< "divisible by both 5 and 8";
 } 
else if( a%8==0 )
 {
cout<< "divisible by 8";
 }
else if(a%5==0)
 {
cout<< "divisible by 5";
 }
else
 {
cout<< "divisible by none";
 }
getch();
}


Switch statement:

This is used when u have multiple choices and u have to choose one of them based on variable or expression value.
Syntax:

switch(variable)
 {
case 1: //execute your code break;
case n: //execute your code break;
default: //execute your code break;
}
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
            clrscr();
            int a, b;
            char choice;
            cout<<"Enter any two numbers: ";
            cin>>a>>b;
            cout<<"\n";
            cout<<"1. Add\n";
            cout<<"2. Subtract\n";
            cout<<"3. Multiply\n";
            cout<<"4. Divide\n";
            cout<<"5. Nothing\n";
            cout<<"\nWhat do you want ? (1-5) ";
            cin>>choice;
            cout<<"\n";
            if(choice == '1')
            {
                        cout<<a+b;
            }
            else if(choice == '2')
            {
                        cout<<a-b;
            }
            else if(choice=='3')
            {
                        cout<<a*b;
            }
            else if(choice=='4')
            {
                        cout<<a/b;
            }
            else if(choice=='5')
            {
                        exit(1);
            }
            else
            {
                        cout<<"Wrong choice..!!..Press any key to exit..\n";
                        getch();
                        exit(2);
            }
            getch();
}
If you like this post then please share and like this post.
Visit us @ tccicomputercoaching.com
Call us @ 98256 18292.

No comments:

Post a Comment