What are the decision making statements in C++?

What are the decision making statements in c++?

What are the decision making statements in C++? Generally a program’s structure flows sequentially. By sequentially we mean that each instruction is executed one after another. This flow can be interrupted or controlled using the decision making statements in C++. Decision making statements help to jump at any part of the program after checking the specific condition.

Types of decision making statements

Sometimes a user needs only to test “if” a particular criteria fulfils or not or “if” a particular match meets “else” do something else. Whenever only one condition needs to be tested then simple “if” statement is used. When we have to choose amongst two choices then we use “if-else” structure. There can also be multiple options where a condition is tested and then after a particular match the corresponding statement/set of statements is/are executed.

what is the if statement?

This is the simplest decision statement and as mentioned earlier it is used for taking a single decision (yes or no)

Its syntax is as follows:

if (condition)

Here if is the reserved keyword after which we mention condition within parenthesis.

Flow chart of if statement

Flow chart of if statement
Flow chart of if statement
Examples of if statement

Let us do multiple examples for getting the idea of how if statement works.

Question No. 1

Write a program for testing either the entered number is greater than 100?

Solution

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int number;

cout<<“enter the number for test”<<endl;

cin>>number;

if(number>100)

cout<<“the number is greater than 100″<<endl;

getch();

}

There are some important things to be noticed related to if statement

  1. The body of if may consists of single statement or multiple statements. When the body of if statements consists of single statement, then we do not need to bracket it, but when it consists of multiple statements then we need to enclose them within brackets.
  2. if our program needs to add only single statement after if then it is better not to write other statements after if body as our compiler will also execute them whenever the conditions fulfils/do not fulfil. Consider the following example.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int age;

cout<<“enter age of the person”<<endl;

if(age>25)

cout<<“graduated”<<endl;

cout<<“must be employed”<<endl;

cout<<“age  is less than 25″<<endl;

getch();

}

Now in this above program we are testing if a particular person is more than 25 then he should be graduated and employed otherwise the program should print he is younger than 25.

Two problems will arise when this program will be executed.

  1. When condition is true, we want two conditions to meet the criteria. But the compiler will associate only one statement “graduated” with if structure and will print all three statements.
  2. When user enters the age variable value less than 25, the compiler will skip only one statement again and print the rest of these two statements as brackets were missing.

Correction 

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int age;

cout<<“enter age of the person”<<endl;

if(age>25)

{

cout<<“graduated”<<endl;

cout<<“must be employed”<<endl;

}

cout<<“age  is less than 25″<<endl;

getch();

}

There is still some logical error in this program. When the entered age is less than 25, then there is no problem as the output window will show message “age  is less than 25”. But when the entered age is greater than 25, then not only the body of if will be displayed on the output screen but also “age  is less than 25” message will be printed. In order to avoid this ambiguity we need to use if-else statement. I hope you got the answer of What are the decision making statements in programming?

THE ? : ALTERNATIVE TO if

  • C++ has an operator that can be alternative to if statement. The conditional operator ? :
  • This operator can be used to replace the if statement of C++.
  • if  (expression 2)  statement 1  else   statement 2
    • The above form of if else statement can be replaced as,

      expression1?expression2:expression3;

    • For example

    int c;

    if (a>b)

    c=a;

    else

    c=b;

    This can be alternatively written as,

    int c;

    c=a>b?a : b;

The if-else statement

This statement is used when we have two options: either this or that. Unlike the if expression where nothing happens when the condition fails, here we have another option when the given condition does not meet the

Flow chart of if statement

if-else flow chart
if-else flow chart

Examples of if-else statement

Question 

Write down a program for testing either an entered number is even or odd?

Sokution

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int num;

cout<<“enter the number for even odd testing”<<endl;

cin>>num;

if(num%2==0)

cout<<“number is even”<<endl;

else

cout<<“this is an odd number”<<endl;

getch();

}

Example

Write down a program for testing either the entered number is positive or negative?

Solution:

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int numb;

cout<<“enter the number for testing”<<endl;

cin>>numb;

if(numb>0)

cout<<“positive number”;

else

cout<<“negative number”;

getch();

}

Nested if-else statement

There are many applications where a user need to make a single choice amongst many given option. This is done in programming using nested if-else structure. A single if can have multiple if-else structure in it thus causing nested if-else structure.

nested if-else structure
nested if-else structure

Lets do multiple examples for getting the concept.

Example

Write down a program for assigning grades to students accordingly.

Solution

void main()

{

clrscr();

int marks;

cout<<“enter marks for assigning grades”<<endl;

cin>>marks;

if(marks<50)

cout<<“F”;

else

if(marks>=50 && marks<60)

cout<<“D”;

else

if(marks>=60 && marks<70)

cout<<“C”;

else

if(marks>=70 && marks<80)

cout<<“B”;

else

if(marks>=80 && marks<90)

cout<<“A”;

else

if(marks>=90 && marks<=100)

cout<<“A+”;

else

cout<<“invalid range of marks entered”;

getch();

}

Example

write a program for performing any of the arithmetic operation(+,-,*,/) on two numbers and store their result.

Solution:

int main()

{

clrscr();

char ch;

float a,b, result;

cout<<“Enter the two values” ;

cin>>a>>b;

cout<<“Enter the Operator [ + – * / ]”<<endl : “;

cin>>ch;

if(ch==’+’)

result=a+b;

else

if(ch==’-‘)

result=a-b;

else

if(ch==’*’)

result=a*b;

else

if(ch==’/’)

result=a/b;

else

cout<<“Unknown Operation “;

cout<<“\nThe Resultis : “<<result;

getch();

return 0;

}

An alternative approach of nested if-else structure is switch statement.

what is the switch Statement in C++?

One thought on “What are the decision making statements in C++?”

Leave a Reply

Your email address will not be published. Required fields are marked *