Page 64 - Digicode_AI_class_8
P. 64

In the previous chapter, you have learned what are variables, data types in programming, sequence,
            loop, bug, event and collections. In this chapter, you will learn about control structures, functions,
            arrays and sequencing with loops and conditions.

            What are Control Structures?

            Control structure is a coding construct that is used to decide the flow of a program based on the
            parameters  given to  it.  Usually,  a  control  structure  accepts  and  analyses  a  variable  to  make  a
            decision on which direction to proceed. So, the control structure is a basic decision-making process
            in computing.
            •   Sequential: Statements in a program are executed sequentially or step-by-step in an order in
               which the instructions are written.

            •   Selection/Conditional: It is used to test a condition in a program. It takes decision to execute
               one statement/operation over another statement/operation depending on the condition.
            •   Iteration: It executes set of statements for a certain number of times till the given condition is
               true. Example: Loops

            On the basis of the above three control structures, programming has derived many types of control
            statements to suit different needs.
            Understanding IF-ELSE and ELSE-IF statements


            Every day, you take many decisions depending on the situation. For example, if you are given a
            number and you need to check if the number is even or odd, you will check this by dividing the
            number with 2. If the remainder is 1, the number is odd and if the remainder is 0, then the number
            is even.
            IF-ELSE Statement

            Let us look at an example first.

            Example: To check if a  number  is even  or  odd  by  using  if-else      Information Literacy
            statement.
            Flowchart:
                                              Start



                                           Read Num1




                                          Is the number      No
                                                                    The number is odd
                                          divisible by 2?

                                                  Yes


                                       The number is even



                                               End

                 62    DigiCode AI-VIII
   59   60   61   62   63   64   65   66   67   68   69