Conditional structure in C++ language

Conditional structure in C++ language

Conditional structure:

A statement that used to control the flow of execution on the base of some conditions of a program called conditional structure. Different conditional structures are the following:

  • ‘if’ structure
  • ‘if-else’ structure
  • Multiple ‘if-else-if’ structure
  • Nested ‘if’ structure
  • ‘if’ statement:

In the C++ language, ‘if’ is a keyword that used to execute statements on condition/conditions. If the condition will true then statements execute that are in the body of ‘if’. And if condition will false then statements not execute that are in the body of ‘if’. Syntax is:

if(condition)

   statement;

We use curly braces if there is a need to execute multiple statements after conditions. So the syntax for the statements are:

if(condition) {

   statement 1;

   statement 2;

   ..

   statements n;

}

The drawback of this structure is that we cannot work with more than one cases. Example:

#include<iostream.h>

using namespace std;

int main(){

int msrks=66;

if(marks=>49.5)

cout<<“Congratulations! you have passed”;

return 0;

}

  • ‘if-else’ structure:

‘if-else’ statement is another type of ‘if’ structure. This appropriate when we need to execute one block if the condition is true and execute 2nd block if condition will false. The main feature of this structure is that both blocks never be executed/ skipped.

if(condition)

   statement;

else

    statement;

Conditional structure

Example:

#include<iostream.h>

using namespace std;

int main(){

int msrks=66;

if(marks=>49.5)

cout<<“Congratulations! you have passed”;

else

cout<<“ooooh! you failed”;

return 0;

}

  • Multiple ‘if-else-if’ structure:

Multiple ‘if-else-if’ structure used when we need o to choose only one block from many bocks of the statement. In this structure, the compiler will check conditions one by one. If one condition will true then statements will execute of that condition. All other conditions will skip without checking. If all conditions will false than statements of else pat will execute. Syntax is:

if(condition)

   statement;

                       else if(condition)

    statement;

else

     statement;

Here is the example of this structure:

#include<iostream.h>

using namespace std;

int main(){

int msrks=66;

if(marks=>84.5)

cout<<“Congratulations! you have got A+ grade “;

else if(marks=>79.5)

cout<<“Congratulations! you have got A grade”;

else if(marks=>74.5)

cout<<“Congratulations! you have got B+ grade”;

else if(marks=>69.5)

cout<<“Congratulations! you have got B grade”;

else if(marks=>64.5)

cout<<“Congratulations! you have got a B- grade”;

else if(marks=>59.5)

cout<<“Congratulations! you have got C+ grade”;

else if(marks=>54.5)

cout<<“Congratulations! you have got a C grade”;

else if(marks=>49.5)

cout<<“Congratulations! you have got D grade”;

else

cout<<“ooooh! you failed”;

return 0;

}

  • Nested ‘if’ structure:

Use of ‘if’ statement within another ‘if’ statement is called nested ‘if’ structure. In this structure, the inner condition will be check if the outer will be true. Syntax is:

if(condition){

      if(condition)

         statement;

      else

          statement;

}

else

    statement

Example:

#include<iostream.h>

using namespace std;

int main(){

int a=45, b=34, c=87;

if(a>c){

if(a>b)

cout<<a<<” is greater “;

else

cout<<b<<” is greater”;

}

else{

if(c>b)

cout<<c<<” is greater “;

else

cout<<b<<” is greater”;

}

return 0;

}

If you face any problem or query then contact us.

We have the collection of other topics of the C++ course and technology updates. Also, like our facebook page

Summary
Review Date
Reviewed Item
Conditional structure in C++ language
Author Rating
51star1star1star1star1star

Add a Comment

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