Page 57 - Computer Genius Class 07
P. 57

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?

                  ontrol structure is a coding construct that is used to decide the  o  o  a program  ased 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

                  et us loo  at an e ample first.
                 Example: To check if a number is even or odd by using if-else statement.

                 Flowchart:
                                                   Start



                                                Read Num1




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


                                                      Yes

                                            The number is even



                                                   End




                                                                                Advanced MakeCode Arcade      55
   52   53   54   55   56   57   58   59   60   61   62