What is the Conditional Structure in C/C++| if else and switch

By | July 20, 2015

read what is the Conditional Structure in C/C++?What is the performance of Conditional Structure?

hello friends today i will continue my programming articles.What is the Conditional Structure in C/C++ and its all types of structure like(if,if else etc)? A brief Explanation of Conditional Structure with a program as a Example.

Conditional Structure:


In this Programming Technique block of statements can be executed and other is skipped. In the Technique the specific condition are use to judge that in which whose block of statement can be executed and whose block of statement can be skipped. There are three structures that is use in this technique are as follows…

i.       if   structures

ii.      if else structures

iii.     nested if structures

iv       switch structure.

If Structure:

If Structure is one of the Conditional Structure. In which a statement or set of statements can be executed if the condition is true. If the condition is not true then nothing happens.Syntax of the if Structure is as follows…

Syntax:

if (Condition)         {

.          Set of Statements;
}

For Example:

if Structure.[howpk.com]

if Structure.[howpk.com]

#include<iostream.h>                    /*header

#include<conio.h>                                 files*/

void main()        {        clrscr();

int n;                            //Declaration of (n)

cin>>n;                       //taking value from the user

if(n>=0)            {         cout>>”The number is Positive”;        //Output Statement
}

getch();

}

If Else Structure:

In if else Structure “if the condition is true the then a statement or set of statement can be executed but if the condition is false then another statement or set of statement can be executed”. It means that in if else Structure the output is not empty we have a block of statements which is executed may be the condition is true and the statements is first one or may be the condition is false and statements is another. The Syntax of if else Structure is as follows…

Syntax:

if(condition)     {

.          Set of Statements;

}else                  {

.          Set of Statements;

}

Note:

Else is added to work about the nearest if condition.

For Example:

ifelse Structure[howpk.com]

if else Structure[howpk.com]

#include<iostream.h>                        /*header

#include<conio.h>                                  files*/

void main()            {            clrscr();

int n;                               //Declaration of (n)

cin>>n;                          //Taking input from the user

if(n>=o)         {     cout<<“The number is Positive”;       /*Output

}  else              {     cout<<“The number is Negative”;           Statements*/

}

getch();

}

nested if Structure:

In nested if Structure one if condition is inner the another if condition. Its means that we have to if conditions one is outer if and other is inner if. If the both condition are true then a statement or set of statements can be executed otherwise if outer condition is false then the inner condition is not executed. The Syntax of nested if  is as follows…

Syntax:

if(condition)          {

.              Set of Statements;

.              if(condition)         {

.              Set of Statements;

.              }

}

For Example:

#include<iostream.h>          /*header

#include<conio.h>                      files*/

nested if Structure[howpk.com]

nested if Structure[howpk.com]

void main()          {   clrscr();

int n;                       //Declaration of (n)

cin>>n;                  //Taking input From the user

if(n>=0)               {                   //Outer if

.            cout<<“The number is Positive”;        //Output Statement

.              if(n%2==0)    {                        //inner if

.               .          cout<<“The number is Even”;}

}

Switch Structure:

In switch Structure a value or expression’s result are compare to each cases and If the case is matched the set of statement can be executed.If all the cases is not matched then the default case is executed.The break statement is use in every end of the case to stop the switch statement.”The one biggest advantage of switch structure is that we have many cases which is to be compared”. The Syntax of the Switch Structure is as follows…

Syntax:

switch(value,expression)  {

case 1:   {      Set of Statements;

.                     break;      }

case2:   {      Set of Statements;

.                    break;       }

case3:    {      Set of Statements;

.                       break;     }

…          …               …              …

…          …               …              …

…          …               …              …

default:    {      Set of Statements;

.                          break;     }

}

conditional structure-switch[howpk.com]

conditional structure-switch[howpk.com]

Author: Awais Shafique

Hello My name is Awias.I am Student of BSCS in GCUF. I am the Senior Contributor of this website.