C – Decision Making Branching Looping

Decision making, Branching, Looping statements

Control Statements (Two types)

  • C program is executing sequentially line by line from the first statement to last statement.
  • But we can change this sequentially execution in c, by using control statements
  • Types
    • Unconditional statements (goto statement)
      • It will change the execution order without any checking process
      • Syntax : goto label_name_position; label_name_position : ArunEworld;
      • Rules
        • label should be a valid identifier.
        • label should be end with colon :
        • Does not require to declare a label .
    • Conditional statements (Two types)
      • It will change the execution order with checking process.
      • types
        • Decision making statements(five types)
        • Looping statements (Three types)
      • Note : return statement or return keyword cannot be used with conditional operators(terynary).

Note : C programming language assumes True and False

  • True = non-zero and non-null. // eg: 1, Arun, True
  • False = zero or null. //eg : 0, nil, false

Decision Making statements (five types)

  • It will change the execution order with checking process.
  • types
    • Simple if Statements
    • if- elsestatements
    • else ...if ladder statements
    • nested if statements
    • switch statements

Simple if Statements

  • It will change the execution order from one place to another place (or) skip the execution of group of statements (or) Execute the group of statements with checking one condition.
  • Syntax : if(Boolean_expression) goto label;(or) if(Boolean_expression) statement; (or)
  • Note : statement(s) will execute if the Boolean expression is true.
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples
      • if (a<b) big =1;
      • if (a=1) goto ArunEworld;
  • Example Programs
    • Evaluate_mark_of_students_using_if_statement.c
    • Evaluvate_sale_tax_using_if_statement.c

if- else statements

  • Execute the group of statement if test condition is true, else test condition is false it will execute the another group of statements
  • Syntax : if(Boolean_expression) statement_1; else statement_2; (or)

     
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples
      • if (a<b) big =b; else big =a;
      • if (a=1) goto ArunEworld; else goto Arun;
      • if (a<b) { s=a+b; d =a-b; } else m = a+b;
  • Example Programs
    • check_given_marks_fail_or_pass_using_if_else_statement.c
    • Biggest_number_using_if_statement.c

else ...if ladder statements

  • else..if is can use to checking more than one conditions, Execute the group of statement if test condition_1 is true, else..if test condition_2 is true it will execute the another group of statements, else above two test condition is false execute another group of statements
  • Syntax :

(or)

  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples

       

      • if (a>b) goto ArunEworld; elseif(b>a) goto Arun; else goto Eworld;
      • if (a<b) { s=a+b;d =a-b; } eslesif(b>a){ d =a-b; b= a-b;} else {m = a+b; a=a+b;}
  • Example Programs
    • Display_Day_using_if_elseif_statement.c

nested if statements

  • Syntax :
  • Examples : Biggest_of_three_numbers_using_nested-if.c

switch statements

  • group of if..else statements collection is called switch statement.
  • Syntax :
  • Rule
    • Expression must have an integral or enumerated type.
    • Constant-expression must be the same data type as the variable in the switch.
  • Example: Display_Day_using_switch_statement.c

nested switch

  • Syntax :

     

Looping control statements (Three types)

While Loop

  • Syntax : while(condition) //The condition may be any expression, and true is any nonzero
  • value. The loop iterates while the condition is true.

 

Do..while Loop

  • Syntax :

     
  • Rules:
    • condition is true while statement break the do loops

for Loop

  • Syntax :

     
  • Rules:
    • init – The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.You are not required to put a statement here, as long as a semicolon appears.
    • condition – Next, the condition is evaluated. If it is true, the body of the loop is executed.If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop.
    • increment – After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement.This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

nested for loops

  • Syntax :

     

nested while loops

  • Syntax :

     

Infinite Loop

  • A loop running continuously for indefinite number of times is called infinite loop.

Infinite For Loop:


  •  

Infinite While Loop:


  •  

Infinite Do-While Loop:


break

  • Break can appear only with in the looping control and switch statement.
  • The purpose of the break is to bring the control out from the used blocks.
  • Syntax : break;

continue

 

  • Syntax : continue;

Goto Statement

Excersize

After the execution of above statement, the control is transferred to statement number
A. 8
B. 11
C. 16
D. 20

  • Answer: Option B (Explanation: It is a computed GO TO statement. Since J COKE = 3 + 1 = 4, the control is transferred to statement 11).

 


 

 

 


 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: