Sunday 25 March 2018

Decision Making Statements

Many times in the program such condition occur when we want to take the exact decisions in order to make an error free program.This kind of situation can be handled using decision control instruction of cpp.It includes if-else statements and the conditional operators.The  decision control structure in cpp can be implemented using:
a) The if statements 
b) The if-else statement
c)  Nested if-else 
d) The else-if ladder

The if Statement
  The general form or syntax of if statement looks like this:
   if(condition)'
    {
      statement-1;
     }
      statement-2;
      statement-3;
     

Here, the keyword 'if' tells the compiler that what follows, is a decision control instruction.The condition following the keyword if is always enclosed within a pair of parentheses.It the condition is true, then the statement in the parenthesis are executed.It the condition is not true then the statement is not executed instead the program skips this part.


The if-else statement 
   The 'if' statement by itself will executed a single statement or a group of statement when the condition following if is true,it does nothing when the condition is false.If the condition is false then a group of statement can be executed using 'else' statement.Its syntax is as given below:

if(condition)
 {
  // statements 1
  }
   else
    {
     // statements 2
     }
    Statement3;


When, we used if-else statement, either 'if' or 'else' statement block will execute depending upon the condition given in the 'if' statement.The 'else' doesn't require condition.Execution of 'else' block also depends on if(condition). When it is false program control transfer to 'else' block.

Nested if-else
 If we write an entire if-else construct within the body of the 'if' statement or the body of an 'else' statement. This is called 'nesting' of if else.

Syntax:
 if(condition)
 {
   //statements1
   if(condition)
    {
     //statements2
     }
     else
     {
      //statements3
      }
   }
 else
 //statements4
 Here,the inner condition executed only when the outer condition of 'if' is true.This hierarchy of nesting of 'of' can be extended in deep with any number of 'if-else' statements.

The else-if ladder
  There is another way of putting multiple 'if's together when multipath decisions are involved. A multipath decision is a chain of 'if's which the statement is associated with each else in an 'if'. It takes the following general form:

if(condition1)
//statements1;
else if(condition2)
//statements2;
else if(condition3)
//statements3;
else if(condition n)
//statements n;
else
//statements-x;
                         This construct is known as else-if ladder.The condition are evaluated from top to the bottom of ladder.As soon as true condition is found, statements associated with it are executed and then program control  is transferred to the 'statements-x' , skipping the rest of the ladder.
  When all the condition become false, then final else containing default statements will be executed.This is shown in the following flowchart-


0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (745) Python Coding Challenge (198) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses