Note on Program control structure

  • Note
  • Things to remember

INTRODUCTION

The structures which regulate the order in which program statements are executed are called Control Structures. There are 3 types of control structure. They are:

  • Sequence:
    It is the set of program instructions which follow one another and are to be executed unconditionally (not dependent on any program conditions). Instructions are put in a predefined sequence (just like a queue in a cinema hall) and the next instruction is executed by CPU only after the execution of the previous instruction (C never comes before B).


    Fig: Sequential Statement
    Fig: Sequential Statement
    Source: www.slideshare.net


  • Selection:
    It is the set of instructions which are to be executed conditionally i.e. they are executed based on a condition that can be either true or false. Commonly used logic for selection are if condition, if else condition, if else if condition, nested if else condition and switch case condition.

  1. If condition:
    If condition is used in case the given problem has only one condition and only one action. Considering either true or false part, if the given condition is true then the statement will be executed. Otherwise, the control exits from the condition.


    Fig: if condition
    Fig: if condition
    Source: www.ustudy.in

    Example: Check the number is positive.

    Algorithm

    Step 1: start

    Step 2: input N

    Step 3: if (n>0), output is positive

    Step 4: end


    Figure:


    j

  2. If else condition:
    This condition is used if the problem has one condition but two alternative actions. Here, if the condition is true, statement 1 will be executed; otherwise ,statement 2 will be executed.


    p


    Example: Find the greatest between 2 numbers.

    Algorithm

    Step 1: start

    Step 2: input X, Y

    Step 3: if (X>Y)

    output X is greatest
    else
    output Y is greatest

    Step 4: end


    Figure:

    f


  3. If else if condition:
    Also known as ladder type if else, we can use this condition if the given problem has more than one interrelated conditions with their respective actions. Here, on a check, if condition 1 is true then, statement 1 is executed. Otherwise, condition 2 is checked and if it is true, statement 2 is executed and so on for next conditions. If all conditions are false, then the last statement will be executed.


    p

    Example: Find the greatest among 3 numbers.

    Algorithm

    Step 1: start

    Step 2: input P, Q, R

    Step 3: if (P>Q && P>R)
    output P is greatest
    else if (Q>R)
    output Q is greatest
    else
    output R is greatest

    Step 4: end


    Figure:


    d


  4. Nested if else condition:
    Nested if else condition is an entire if-else statement which is written within the body of if part or else part of another if else statement. This condition is used when a condition is to be checked that is inside another condition at a time in the same program, to make a decision.

    k

    Example: Find the greatest 2 positive numbers.

    Algorithm

    Step 1: start

    Step 2: input X, Y

    Step 3: if (X>0 && Y>0)
    {
    if (X>Y)
    output X is greatest
    else

    output Y is greatest
    }
    output X or Y may be
    negative

    Step 4: end

    Figure:

    d

  5. Switch case condition:
    If the given problem has one condition and respective more than two actions, then in this type of case scenario, we can use Switch case condition. It is the multiple branching statements which checks the value of the variable to the case value and then, the statements that are associated with it will be executed. If any expression does not match any of the case value, then the default statement will be executed.

    s



Example: Write an algorithm and draw a flowchart which takes the integer value 1 to 7 and prints respective day.

Algorithm

Step 1: start

Step 2: switch (day)
{
case 1: output Sunday
break
case 2: output Monday
break
case 3: output Tuesday
break
case 4: output Wednesday
break
case 5: output Thursday
break
case 6: output Friday
break
case 7: output Saturday
break
default: output out of range

}

Step 4: end

Figure:


s

  • Iteration:These are the computer instructions which are to be performed repeatedly and conditionally i.e. loop statements are driven by the loop condition. Commonly used logic for iteration are while loop, do while loop and for a loop.

  1. While loop:
    In this loop, first, the condition is checked by the computer and if the condition turns out to be true, then the statement inside the loop is executed. This process is repeated and the value of increment and decrement operator is always changing. When the condition is false, the loop stops.


    Algorithm Syntax
    Initialization
    while (condition)
    {
    statements
    ……………….
    ……………….
    increment/ decrement
    }


    k

    Example: Write an algorithm and draw a flowchart to print 1 to 10.


    Algorithm

    Step 1: start

    Step 2: I=1

    Step 3: while (I<=10)
    {
    output I
    I++
    }

    Step 4: stop

    Figure:

    x



  2. Do while loop:
    In this loop, first, the computer checks the initial value; second executes the statements inside the loop and finally, checks the condition. The process is repeated for next pass, if the condition is true. Otherwise, the loop stops. If the condition is initially false, it will execute for at least one time.

    Algorithm Syntax

    Initialization
    do {
    statements
    ………………..
    ………………..
    increment/ decrement
    } while (condition)

    f

    Example: Write an algorithm and draw a flowchart to print 100 to 1.

    Algorithm

    Step 1: start

    Step 2: A=100

    Step 3: do {
    output A
    A++
    } while (A>1)

    Step 4: stop

    Figure:
    f

  3. For loop:
    It is the most commonly used loop. It consists of 3 expressions; initialization, condition and counter, which are defined within a statement.

    Algorithm Syntax

    for (initialization; condition; counter)
    {
    statements
    ……………….
    ……………….
    }
    Where, initialization is starting point,
    the condition is stopping point and increment/ decrement is a counter.

    fl

    Write an algorithm and draw a flowchart to print a multiplication table of 7.

    Algorithm

    Step 1: start

    Step 2: for (i=1; i<10; i++)
    {
    m= i*7
    output m
    }

    Step 4: stop

    Write an algorithm and draw a flowchart to print a multiplication table of 7.

    Algorithm

    Step 1: start

    Step 2: for (i=1; i<10; i++)
    {
    m= i*7
    output m
    }

    Step 4: stop

    j

    (Koirala & Shrestha, 2015)

    Bibliography

    Koirala, H., & Shrestha, R. K. (2015). Computer Science I. Anamnagar, Kathmandu: Buddha Publication.

    Adhikari, Deepak et.al., Computer Science- XI, Asia Publication Pvt. Ltd, ktm












Program Control Structure & its types:

  • Sequence.
  • Selection; if condition, if else condition, if else if condition, nested if condition and switch case condition.
  • Iteration; while loop, do while loop and for loop.

 

 

.

Very Short Questions

0%

DISCUSSIONS ABOUT THIS NOTE

No discussion on this note yet. Be first to comment on this note