Decision Making and Branching

Decision Making and Branching

  • C program is made up of statements known as instructions which is executed during the running process.

Types of Statements:

1.Selection/ Conditional/Branching statements: They decide the flow of the statements which is based on the evaluation condition.

Example: if, if else, and switch statement

2. Iteration/loop Statements: These are used to run a particular block of statements repeatedly, which form a loop.

Example: for loop, while loop, do-while loop

3. Jump Statement: These are used to make the flow of your statement from one point to another

Example: break, continue, goto and return

4. Label Statement: These are used to as a target/ way point for jump and selection statement.

Example: case and label

5. Expression Statement: Any valid expression makes an expression statement

Example: c=a+b;

6.Block Statement: A group of statement which are binded together to form a logic are called block statement

Example:

{

Statement1

Statement2

……………..

Statement3

}

Control-statement

 1. if statement:

  • It is basically a two way decision statement and is used in conjunction with an expression

Syntax:
if (test expression/condition) {
statement/s to be executed if test expression is true;
}

  • The if statement checks whether the text expression inside parenthesis () is true or not. If the test expression is true(Non-Zero), statement/s inside the body of if statement is executed but if test is false, statement/s inside body of if is ignored.
  • Conditional statement is not ended with semicolon
  • flowchart-if-programming_0Program for Demonstrations:
  •  
     
  • if statement by default evaluates just next statement or statement block
  • If more than one statements after if statement need to be a part of if statement, those should be inside {}.

C- Program to find out the largest among three numbers using if statement

2.if-else statement:

  • The if…else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false.

Syntax:

 

flowchart-if-else-programmingDemo Program:

  •  If more than one statement is there in between if and else part, then that need to be placed  within { }.

3.Nested if else

  • If if-else statement is present in-side one if statement or else statement, it is called nested if-else statement.

Syntax:

nested

Finding biggest from 3 numbers using nested if-else statement

Dangling else Problem:

  • This is a classic problem created when there is no matching else found for every if. This problem basically occurs when nested if else statement is used with out {}’s.
  • We know that “else is always paired with most recent if”

Example:

    •  In the above case ‘else’ can not be applied to either of the ‘if’ statement
    • Solution to the dangling else problem:  Use of proper {}’s is needed in nested if else statement
    • Proper code for the above problem

Hanging if Problem:

  • This is a problem which arises when more than one statements is present between the if and else part without curly braces in if else statements.

 

  •  Here printf(“Bharat”); is a hanging if statement, because of that there will be an error: “misplaced if”
  • Solution to the hanging if problem: More than one statement in if block should have to be kept in {}’s.

4. if-else Ladder (if-else-if):

  • It is variety of nested if else statement where multi-way/ multi path decision in taken

Syntax Of Else If Leader:

  • The condition is evaluated from the top of ladder to downwards. As soon as the true condition is found the block of statement associated with it is executed and the control is transfered to the statement-x(skipping rest of the ladder ).
  • Only one block of statements is executed

 

Sample Input/Outputs
Enter a integer numbers:23
Number is positive
————————————–
Enter a integer numbers:-12
Number is Negative
—————————————
Enter a integer numbers:0
Number is Zero

Related programs of if else

 5.switch statement:

  • C has a built-in multi-way decision statement known as switch statement.
  • It is accompanied with two keywords “case” and “default”
  • The expression is evaluated first inside the switch resulting a value
  • If the value matches with the case value, statements under that case are evaluated and the subsequent cases is also evaluated
  • The case value must be integer or character
  • case level must be different
  • case and default can be used in any order
  • default is optional case which executed when non of the case value matches with the expression value.

Example:

Output
Inside case2
Inside case3
Inside default case

  • The actual use of switch case becomes meaning less without using the break statement
  • The break statement at the end of each case cause switch statement to exit. If break statement is not used, all statements below that case statement are also executed.

Example:

Output
Inside case2

Related Programs of switch case