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

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (23) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

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