SlideShare uma empresa Scribd logo
1 de 29
F1001 PROGRAMMING FUNDAMENTALS




    UNIT

      3

CONTROL STRUCTURE

 Sequential Control Structure
 Selection Control Structure
   Decision Control Structure




                                                30
F1001 PROGRAMMING FUNDAMENTALS


BLOCKS OF CODE
•       Every programming language consists of thousand of statements.
•       Thus, to simplify the execution of the programming language, all the statements in the programming
        language is divided into blocks of code that have specific functions.
•       Statement in a block of code is arranged sequentially in a program.
•       A block of code is a group of statements that performs one particular task.

•       The block is separated in several ways, like curly bracket “{“ and “}”, Begin and End statement and
        others.
•       Function is a block of code that has been assigned a name.
•       Function is used as reference and execution for a block of code.
•       Combination of blocks of code will produce a perfect program to be executed in a computer.
•       Since the program comes from the different blocks of code, so, the flow needs to be controlled.
•       Example of blocks of code:

                              void welcome ( )
                              {
                                                                                                BLOCK OF
    BLOCK                              printf (“****************************”);
                                       printf (“********WELCOME **********”);                     CODE
    SEPARATO                           printf (“****************************”);
    R                         }


PROGRAM FLOW
•       Program flow in computer is controlled by the control structure.
•       Control structure is a logical structure that controls the flow of instruction to be executed by computer.

•       Control structure uses single entry and single exit; meaning it has only one beginning and one ending.

•       There are three types of control structures:
        1.   Sequential control structure
        2.   Selection / decision control structure
             a)   If……endif
             b) If……else
             c)   Nested if
        3.   Looping control structure
             a)   For
             b) While
             c)   Do……while




                                                                                                               31
F1001 PROGRAMMING FUNDAMENTALS



Sequential Control Structure
   In this control, every step will be executed one by one from top to bottom.
   Every box in control structure is a process. Every process is done sequentially.
   Format:
                  Pseudocode:

                                                                      START
                   Start

                                                      Flowchart     Statement A
                         Statement A
                         Statement B
                                                                    Statement B

                   End
                                                                       END


   Example 1:
     Compute the total overtime wages of an employee.
     Problem analysis:
        Input:
        o     Hours
        o     Basic_salary
        o     OT_rate
        Process:
        o     Overtime equals OT_rate times Hours
        o     Salary equals Basic_salary plus Overtime
        Output:
        o     Salary


     Algorithm:
        1.    Enter Hours, Basic_salary, OT_rate
        2.    Calculate overtime using formula:
                   Overtime = OT_rate * Hours
        3.    Calculate salary using formula:
                   Salary = Basic_salary + Overtime
        4.    Output Salary




                                                                                       32
F1001 PROGRAMMING FUNDAMENTALS



     Pseudocode:
        START
                       Input Hours, Basic_salary, OT_rate
                       Overtime = OT_rate * Hours
                       Salary = Basic_salary + Overtime
                       Output Salary
        END



     Flowchart:

                                          START


                                        Input Hours,
                                   Basic_salary, OT_rate


                              Overtime = OT_rate * Hours


                           Salary = Basic_salary + Overtime


                                         Output
                                         Salary

                                          END



   Example 2:
     Problem: Mathematical operation: Get two numbers, then do adding, subtracting, multiplying
        and dividing operations.
     Problem analysis:
        Input:
        o          number_1, number_2
        Process:
        o          Add 2 numbers:
            Sum = number_1 + number_2
        o          Minus 2 numbers:
            Subtract = number_1 – number_2
        o          Multiply 2 numbers:



                                                                                             33
F1001 PROGRAMMING FUNDAMENTALS


         Multiple = number_1 * number_2
    o         Division 2 numbers:
         Divide = number_1 / number_2
    Output:
    o         Sum, Subtract, Multiple and Divide


   Algorithm:
    1.   Enter 2 numbers
    2.   Add 2 numbers
              Sum = number_1 + number_2
    3.   Minus 2 numbers
              Subtract = number_1 – number_2
    4.   Multiply 2 numbers
              Multiple = number_1 * number_2
    5.   Division of 2 numbers
              Divide = number_1 / number_2
    6.   Display Sum, Subtract, Multiple and Divide


   Flowchart:

                                 START



                           Input number_1,
                              number_2


                   Sum = number_1 + number_2


                 Subtract = number_1 – number_2


                 Multiple = number_1 * number_2



                   Divide = number_1 / number_2



                  Output Sum, Subtract, Multiple,
                              Divide


                                 END


                                                                         34
F1001 PROGRAMMING FUNDAMENTALS



       Pseudocode:
        START
             Input number_1, number_2
             Sum = number_1 + number_2
             Subtract = number_1 - number_2
             Multiple = = number_1 * number_2
             Divide = number_1 / number_2
             Output Sum, Subtract, Multiple, Divide
        END




Selection / Decision Control Structure
   This type of control structure is usually used in structured programming
   This control structure will execute an instruction based on result of a condition or comparison.
   A condition will result either TRUE or FALSE.
   If the condition result is true, the control program will execute the instruction within the TRUE loop
    operation.
   Otherwise, it will execute the next instruction or the instruction within the FALSE loop operation.
   Example 1:
       Condition: A person can obtain a license when he/ she is above 21 years old
        Decision: If true then she / he is qualified to have driving license. If not he / she is not qualified to
        have a driving license.
       Below is a flowchart of control structure:




                                                                                                             35
F1001 PROGRAMMING FUNDAMENTALS




                                                                                         START


                                              True
                       Condition                                                    Input age


               False                             Statement that will be
                                                 executed if condition                            True
               Statement that will be                   is true                     If age > 21
               executed if condition
                    is not true                                                    False
                                                                                                     Output
                                                                                                   “Qualified”



                                                                                    Output “Not
                                                                                    Qualified”




                                                                                           END


   Type of selection / decision control structure:
    1.        If……endif
    2.        If……else
    3.        Nested if




         1.     IF…….ENDIF
                      Rules:

                                If (condition)
                                Instruction (do this instruction if condition is true)
                                Endif
                           If condition is not true, no instruction will be executed




                                                                                                                 36
F1001 PROGRAMMING FUNDAMENTALS




   Pseudocode:
    If (condition)
                                                         START
             True statement
    Endif
                                                                     True
                                                        Conditi
                                                                            Statement
                                                 Flowchart:on

                                                      False




                                                          END



   Example 1:
       Workers who work on shift 3 will receive additional Bonus RM50, where basic salary is
        entered by workers.
       Problem analysis:
        Input:       1. Shift
                     2. Basic_salary
             Process: Bonus equals RM 50
                         If Shift equals to 3:
                                  Salary equals Bonus plus Basic_salary
             Output: Salary


       Algorithm:
        1.   Enter Basic_salary, Shift
        2.   Bonus equals to RM 50
        3.   Check workers Shift
             3.1         If Shift equals to 3
                         Salary= Basic_salary + Bonus
        4.   Display Salary




                                                                                           37
F1001 PROGRAMMING FUNDAMENTALS




   Flowchart:

               START



      Input Shift, Basic_salary


            Bonus = 50



                              True
              If Shift =
                                        Salary = Basic_salary + Bonus
                  3
            False




            Output Salary



                 END


   Pseudocode:


    START
            Input Shift, Basic_salary
            Bonus = 50
            If (Shift ==3)
                       Salary = Basic_salary + Bonus
            End if
            Output Salary
    END




                                                                        38
F1001 PROGRAMMING FUNDAMENTALS




2.       IF…….ELSE type
        A selection of control structure is used to select between two options
                                                       Pseudocode:
        Rules:
                                                           If (condition)
         If (condition)
                                                                     True statement
                  True statement
                                                           Else
         Else
                                                                     False statement
                  False statement
                                                           Endif
         Endif


        Flowchart:


                                       START



                      False                                 True
                                        Conditi
                                          on



             Statement 2                                     Statement 1




                                            END

        Example 1:
         o     Prepare a problem analysis, algorithm, flowchart and pseudocode to identify whether a
               student is qualified to further her / his studies in any local university using his / her SPM
               grade equal to 1.


         o     Problem analysis:
                  Input: Grade
                  Process:         If Grade is equal to 1
                                              Output “Qualified to further study”.
                                   If not
                                              Output “Not qualified to further study”.
                  Output: Display message qualified or not




                                                                                                         39
F1001 PROGRAMMING FUNDAMENTALS



o   Algorithm:
        1.   Enter Grade
        2.   Check Grade
             1.1 If Grade = 1
                 1.1.1      Output “Qualified to further study”
             1.2 If not
                 1.2.1      Output “Not qualified to further study”


o   Flowchart:

                                 START


                              Input Grade



                 False                           True
                                   If
                                 Grade



    Output “Not qualified                      Output “Qualified
      to further study”                         to further study”




                                 END



o   Pseudocode:


        START
                 Input Grade
                 If (Grade==1)
                            Output “Qualified to further study”
                 Else
                            Output “Not qualified to further study”

                 Endif
        END



                                                                      40
F1001 PROGRAMMING FUNDAMENTALS



   Example 2:
    o    Problem:
            Prepare the problem analysis, algorithm, flowchart and pseudocode to find
            subtraction between two numbers that users enter.
    o    Problem analysis:
            Input:        num1, num2
            Process: If num1 greater than num2
                                     Result = num1 – num2
                          If not
                                     Result = num2 – num1
            Output:       Result


    o    Algorithm:
            1.   Enter num1, num2
            2.   Compare the 2 numbers
                 2.1 If num1 greater than num2
                      2.1.1    Result = num1 – num2
                 2.2 If not
                      2.2.1    Result = num2 – num1
            3.   Display Result


    o    Flowchart:

                                   START


                              Input num1, num2



             False                                     True
                                   If num1 >
                                      num2



Result = num2 – num1                                Result = num1 – num2




                               Output Result



                                     END                                          41
F1001 PROGRAMMING FUNDAMENTALS



         o    Pseudocode:


                  START
                              Input num1, num2
                              If num1 > num2
                                         Result = num1 – num2
                              Else

                                         Result = num2 – num1
                              Endif

                              Output Result

                  END


3.   NESTED IF


        There are 3 types:


         1.   Type 1:


                        If (condition1)
                                     If (condition2)
                                         If (condition3)
                                                         True statement
                                              Endif
                                     Endif
                        Endif


         2.   Type 2:
                        If (condition1)
                                     If (condition2)
                                              If (condition3)
                                                     Statement that will be executed if condition1, condition2
                                                     and condition3 are true
                                              Else
                                                     Statement that will be executed if condition1, and
                                                     condition2 are true but condition2 is false
                                              Endif



                                                                                                           42
F1001 PROGRAMMING FUNDAMENTALS


                               Else
                                      Statement that will be executed if condition1 is true but condition2
                                      and condition3 is false
                               Endif
                    Else
                             Statement that will be executed if condition1 is false
                    Endif



    3.       Type 3


             If (condition1)
                               Statement that will be executed if condition 1 is true
                    Else
                               If (condition 2)
                                          Statement that will be executed if condition2 is true but
                                          condition1 is false
                               Else
                                          If (condition3)
                                                   Statement that will be executed if condition3 is true
                                                   but condition1 and condition2 are false
                                          Else
                                                   Statement that will be executed if condition1,
                                                   condition2 and condition3 are false
                                          Endif
                               Endif
                    End if


   Example Type 1:
    o     Problem:
          To determine whether a candidate is qualified or not to get a scholarship based on his /
          her study years, guardian’s salary and student CGPA. If the study year is more than 1,
          student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500,
          student will be considered for a scholarship.


    o     Problem analysis:
         Input: CGPA, Year, Salary.
         Process:


                                                                                                       43
F1001 PROGRAMMING FUNDAMENTALS


             1.    Check if the student application’s can be considered or not for a scholarship.
                   1.1 If Year greater than 1
                       1.1.1       If CGPA greater than or equal to 3.00
                                   1.1.1.1 If Salary is less than or equal to RM500
                                                Output “Your application is under consideration”.
             Output: Student status


         o    Algorithm:
              1.    Enter CGPA, Salary and Year
              2.    Check if the student application’s can be considered for a scholarship
                    2.11 If year > 1
                        2.1.1 If CGPA >= 3.00
                                   2.1.1.1   If salary <= RM500
                                                 Output “Your application is under consideration”
              3.1 Display status


         o    Flowchart:


              START


        Input Year, CGPA, Salary



False                              True
             If Year > 1


                           False      If CGPA        True
                                      >= 3.00

                                                                                  Output “Your
                                             False    If Salary      True
                                                                               application is under
                                                       <= 500
                                                                                 consideration”




                                             END




                                                                                                      44
F1001 PROGRAMMING FUNDAMENTALS



    o    Pseudocode:
            START
                      Input Year, CGPA, Salary
                      If Year >1
                      If CGPA >= 3.00
                                            If Salary <= RM500
                                                Output “Your application is under consideration”
                                            Endif
                                 Endif
                      Endif
            END


   Example Type 2:
    o    Problem:
        To determine whether a candidate is qualified or not to get a scholarship based on his /
        her study years, guardian’s salary and student CGPA. If the study year is more than 1,
        student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500, student
        will be considered for a scholarship. If the student is not qualified the message “Not
        success” will be displayed.


    o    Problem analysis:
        Input: CGPA, Year, Salary
        Process:
              1.    Check if a student can be considered for a scholarship
                    1.1 If Year > 1
                        1.1.1      If CGPA >= 3.00
                                   1.1.1.1 If Gaji <= 500
                                                    Output “You application is under consideration”
                                   1.1.1.2 If not
                                                    Output “Not success”
                        1.1.2      If not
                                         Output “Not success”
                    1.2 If not
                              Output “Not success”
          Output: Student status




                                                                                                      45
F1001 PROGRAMMING FUNDAMENTALS



     o      Algorithm:
                1.    Enter CGPA, Year, Salary
                2.    Check if a student can be considered for a scholarship
                      2.1 If Year > 1
                            2.1.1       If CGPA >= 3.00
                                        2.1.1.1 If Salary <= RM500
                                                         Output “Your application is under consideration”.
                                        2.1.1.2 If not
                                                         Output “Not success”
                            2.1.2       If not
                                             Output “Not success”
                      2.2 If not
                                    Output “Not success”
                3.          Display status


     o      Flowchart:


               START


         Input Year, CGPA, Salary



 False                               True
              If Year > 1


Output “Not                 False       If CGPA           True
 success”                               >= 3.00

                                                                                      Output “Your
                     Output “Not                 False     If Salary     True
                                                                                   application is under
                      success”                              <= 500
                                                                                     consideration”

                                            Output “Not
                                             success”




                                                 END


                                                                                                             46
F1001 PROGRAMMING FUNDAMENTALS



    o   Pseudocode:
        Input CGPA, Salary, Year
        If (year > 1)
               If (CGPA >= 3.00)
                      If (salary <= RM500)
                             Output “Your application is under consideration”
                      Else
                             Output ”Not success”
                      Endif
               Else
                      Output ”Not success”
               Endif
        Else
               Output ”Not success”
        Endif


   Example Type 3:
    o    Problem: Education status is determined based on the GPA achievement under the
         following scheme:
                        GPA                          Status
                      3.50-4.00            Dean List
                      2.00-3.49            Pass
                      1.80-1.99            Conditional Pass
                      0.00-1.79            Fail

    o    Problem analysis:
         Input: GPA
         Process:
                 1. If (GPA < 0.00 AND GPA > 4.00)
                         Output “Invalid data”
                 2. If not
                     2.1 If (GPA >=3.5 AND GPA <= 4.00)
                              Output “Dean List”
                     2.2 If not
                         2.2.1 If (GPA >= 2.00 AND GPA < 3.50)
                                  Output “Pass”
                         2.2.2 If not
                                2.2.2.1 If (GPA >= 1.80 AND GPA< 2.00)
                                           Output “Conditional Pass”
                                2.2.2.2 If not
                                           Output “Fail”
         Output: Student education status




                                                                                          47
F1001 PROGRAMMING FUNDAMENTALS


o   Algorithm:
       1.   Enter student GPA
       2.   Compare student’s GPA to determine his/ her education status.
            2.1 If (GPA < 0.00 AND GPA > 4.00)
                     Output “Invalid data”
            2.2 If not
                 2.2.1       If (GPA >=3.50 AND GPA <= 4.00)
                                 Output “Dean List”
                 2.2.2       If not
                           2.2.2.1 If (GPA >= 2.00 AND GPA < 3.50)
                                            Output “Pass”
                           2.2.2.2 If not
                                      2.2.2.2.1If (GPA >= 1.80 AND GPA < 2.00)
                                                        Output “Conditional Pass”
                                      2.2.2.2.2If not
                                                        Output “Fail”
       3.   Print status




o   Flowchart:



                                                                                    48
F1001 PROGRAMMING FUNDAMENTALS




                  START


                Input GPA



   True                                False
               If GPA < 0.00
              && GPA > 4.00


                      True                                   False
                                 If GPA >= 3.50 &&
Output “Invalid                      GPA <= 4.00

    data”
                  Output “Dean                 True                          False
                                                        If GPA >= 2.00 &&
                     List”                                  GPA < 3.50


                                                                True                          False
                                                                        If GPA >= 1.80 &&
                                     Output “Pass”                          GPA < 2.00



                                                            Output
                                                                                     Output “Fail”
                                                       “Conditional Pass”




                                             END


          o     Pseudocode:
                   START
                   Input GPA
                       If ((GPA < 0.00) AND (GPA > 4.00))
                              Output "Invalid Data"
                       Else
                              If ((GPA >= 3.50) AND (GPA <= 4.00))
                                     Output "Dean List"
                              Else
                                     If ((GPA >=2.00) AND (GPA < 3.50))
                                            Output "Pass"
                                     Else


                                                                                                      49
F1001 PROGRAMMING FUNDAMENTALS


                                         If ((GPA >= 1.80) AND (GPA < 2.00))
                                                Output "Conditional Pass”
                                         Else
                                                Output "Fail"
                                         Endif
                                     Endif
                              Endif
                        Endif
                   END


          Usage of Logical Operator:
  Symbol           Symbol                    Example             Result                Description
(Pseudocode)    (C Language)
    AND               &&               (1 > 3) && (10 < 20)      FALSE      Both sides of the condition must
                                                                            be true
    OR                 ||               (1 > 3) || (10 < 20)      TRUE      Either one of the condition must
                                                                            be true
    NOT                 !                    ! (10 < 20)         FALSE      Change the operation either
                                                                            from true to false or vise versa

          o    From the above table, the logical operator are AND, OR and NOT.
          o    Example using of AND operator:


                       If ((a < b) && (c > d))
                              printf(“Print a and c”);




          o    If condition (a < b) is true AND condition (c > d) is true then “Print a and c” will be
               displayed. If one of the condition is not true then “Print a and c” will not be displayed.


          o    Example usage of OR operator:



                            If ((sales > 5000) || (hoursworked> 81))
                                    bonus = 500;
                            else
                                   bonus = 0;




                                                                                                            50
F1001 PROGRAMMING FUNDAMENTALS


            o       If the sales are more than 5000 OR working hours are more than 81, bonus RM500 will
                    be given. If either condition is not fulfilled, still the amount of RM500 will still be given
                    as bonus.
            o       If both conditions are false, then bonus will not be given.


Looping Control Structure
   A programming control structure that allows a series of instructions to be executed more than once.
    The similar statement is repeated several times until the conditions are fulfilled.
   Three are three types of loop:
       o     For
       o     While
       o     Do…while
                                       For                            While                  Do…while

                For (initialize; condition; counter)         While (condition)        Do
                    True statement if condition is              True statement          True statement
                    fulfilled                                Endwhile                 While (condition)
                Endfor

        o       Initialize value: a value to start a loop.
        o       Counter: to increase or decrease the initialize value.
        o       Rules for the condition:
                -         If the condition is true / fulfilled, the process will be performed.
                -         Then, the program loops back and recheck the condition, if the condition is true /
                          fulfilled, repeat the process.
                -         When the condition is false, then exit the loop.

                                                                       Format for Do …… while loop:
            Format for the For and While loops:

                      START                                                                 START


                     Initialize                                                            Initialize

                                                                                        Statement A

                        Test                 False
                                                       END                    True
                      condition                                                              Test
                                                                                           condition
                                True
                                                                                                 False
   Example of loops usage: A
                  Statement

                                                                                            END

                                                                                                               51
F1001 PROGRAMMING FUNDAMENTALS


o      Problem: Prepare the problem analysis, algorithm, flowchart and pseudocode for the average of 5
       numbers. Data will be entered by user.
o      Problem analysis:
      Input:      5 numbers
      Process: The process of adding numbers will repeat until the condition to exit the loop is met.
      Output: Average of 5 numbers
o      Algorithm:
      1.    Initialize Counter=0; Average = 0; Total = 0
      2.    Input number
      3.    Add Total using formula:
            Total = Total + number
      4.    Add Counter using formula:
            Counter = Counter + 1
      5.    Compare whether Counter is greater than 5
            If yes , go to step 6
            If not, go to step 2
      6.    Calculate Average of numbers using formula;
            Average = Total/5
      7.    Display Average




o          Pseudocode:
                 For loop                          While loop                       Do…while loop
    START                              START                                START
      no = 0                              no = 0                                no = 0
      Total = 0                           Total = 0                             Total = 0
      Avg = 0                             Avg = 0                               Avg = 0
      For (no=0; no<=5; no++)             While (no <= 5)                       Do {
            Input Num                           Input Num                           Input Num
            Total = Total + Num                 Total = Total + Num                 Total = Total + Num
      Endfor                                    no = no + 1                         no = no + 1
      Avg = Total/5                       Endwhile                              } While (no <= 5)
      Output Avg                          Avg = Total/5                         Avg = Total/5
    END                                   Output Avg                            Output Avg
                                       END                                  END



             o      Flowchart for For and While loops:




                                                                                                          52
F1001 PROGRAMMING FUNDAMENTALS




              START


              no = 0


             Total = 0


              Avg = 0



              For /           False
             While no                 Avg = Total/5
           True


            Input Num                 Output Avg



        Total = Total + Num               END


            no = no + 1




o   Flowchart for Do……while loop:



                                                          53
F1001 PROGRAMMING FUNDAMENTALS


                START
                                            Note:
                                                     no: a variable to control loop
                 no = 0                              structure whether to continue or
                                                     exit from the loop
               Total= 0
                                                     no + 1: a counter and it is very

                Avg = 0                              important. Without a counter, the
                                                     loop will be infinite

              Input Num
                                                     Total = Total + Num: a variable to
                                                     compute the value
         Total = Total + Num


               no = no + 1


True                             False
                no <= 5                       Avg = Total/5


                                             Output Avg


                                                   END



o      Example: To compute salary of 20 employees
       Algorithm for For and While loops:

        1.   Initialize Counter = 0, Salary = 0;
        2.   Compare whether Counter is greater than 20 or not
             If yes , out from the loop
             If not , go to step 3
        3.   Enter Basic_salary, Claim, O_time
        4.   Calculate EPF:
             EPF = Basic_salary * 0.09
        5.   Calculate Salary using this formula:
             Salary = Basic_salary + Claim + O_time – EPF
        6.   Display Salary
         7. Add Counter using the formula:
       Flowchart for For and While loops:
            Counter = Counter + 1
        8.   Back to step 2

                                                                                          54
F1001 PROGRAMMING FUNDAMENTALS



               START


               no = 0


              Salary = 0



               For /             False
                                           END
              While no
            True


                Input
            Basic_salary,
            Claim, O_time


     EPF = Basic_salary * 0.09



Salary = Basic_salary + Claim + O_time – EPF



           Output Salary



              no = no + 1


  Pseudocode for For loop:

     START
        no = 0
         Salary = 0
         For (no = 0, no <=20, no ++)
             Input Basic_salary, Claim, O_time
             EPF = Basic_salary * 0.09
             Salary = Basic_salary + Claim + O_time – EPF
             Output Salary
         Endfor
     END


  Pseudocode for While loop:



                                                            55
F1001 PROGRAMMING FUNDAMENTALS



     START
        no = 0
         Salary = 0
         While no <=20
              Input Basic_salary, Claim, O_time
              EPF = Basic_salary * 0.09
              Gaji = Basic_salary + Claim + O_time – EPF
              Output Salary
              no = no + 1
         Endwhile
     END




Algorithm for Do……while loop:

1.   Initialize Counter = 0, Salary = 0;
2.   Input Basic_salary, Claim, O_time
3.   Calculate EPF
     EPF = Basic_salary * 0.09
4.   Calculate Salary using this formula:
     Salary = Basic_salary + Claim + O_time – EPF
5.   Add Counter using this formula:
     Counter = Counter + 1
6.   Compare whether the Counter is greater than 20 or not
     If yes , out of loop
     If not, go to step 2
7.   Display Salary




Flowchart for Do…..while loop:



                                                             56
F1001 PROGRAMMING FUNDAMENTALS



                         START



                         no = 0


                       Salary = 0


                         Input
                     Basic_salary,
                     Claim, O_time

                EPF = Basic_salary * 0.09



    Salary = Basic_salary + Claim + O_time – EPF



                          Output
                          Salary


                        no = no + 1


         True                          False
                         no <= 5                    END



     Pseudocode for Do…..while loop:

     START
        no = 0
         Salary = 0
         Do
         {
                Input Basic_salary, Claim, O_time
                EPF = Basic_salary * 0.09
                Salary = Basic_salary + Claim + O_time – EPF
                Output Salary
                no = no + 1
        } While (no <= 20)
o    Dummy Data or Sentinel Value
     END


                                                                 57
F1001 PROGRAMMING FUNDAMENTALS


-    Value used to end the program. Dummy data must be different from the data
     to be entered.
-    Example:


    START
            Input name
            While name != “XXX”
                                               Dummy data
                      Output name
                      Input mark
                      Output mark
                      Input name
            Endwhile
    END




                                                                           58

Mais conteúdo relacionado

Mais procurados

ISP di malaysia(internet service provider)
ISP di malaysia(internet service provider)ISP di malaysia(internet service provider)
ISP di malaysia(internet service provider)
Mohamad Arif
 
Peta minda
Peta mindaPeta minda
Peta minda
Ep Loh
 
Ukur terabas teodolit
Ukur terabas teodolitUkur terabas teodolit
Ukur terabas teodolit
Nik M Farid
 
2.2 perkakasan peranti inputoutput
2.2 perkakasan  peranti inputoutput2.2 perkakasan  peranti inputoutput
2.2 perkakasan peranti inputoutput
Suriyana Ahmad
 
Komponen utama komputer
Komponen utama komputerKomponen utama komputer
Komponen utama komputer
arah1506
 

Mais procurados (20)

Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
 
Keselamatan pemasangan komputer
Keselamatan pemasangan komputerKeselamatan pemasangan komputer
Keselamatan pemasangan komputer
 
8279 PKDI
8279 PKDI8279 PKDI
8279 PKDI
 
Report latihan industri
Report latihan industriReport latihan industri
Report latihan industri
 
ISP di malaysia(internet service provider)
ISP di malaysia(internet service provider)ISP di malaysia(internet service provider)
ISP di malaysia(internet service provider)
 
Peta minda
Peta mindaPeta minda
Peta minda
 
Memory interfacing of microprocessor 8085
Memory interfacing of microprocessor 8085Memory interfacing of microprocessor 8085
Memory interfacing of microprocessor 8085
 
23491369 litar-elektronik
23491369 litar-elektronik23491369 litar-elektronik
23491369 litar-elektronik
 
Displacement addressing
Displacement addressingDisplacement addressing
Displacement addressing
 
Report Final Projek , Pelancongan dan hospitaliti
Report Final Projek , Pelancongan dan hospitalitiReport Final Projek , Pelancongan dan hospitaliti
Report Final Projek , Pelancongan dan hospitaliti
 
Interrupt
InterruptInterrupt
Interrupt
 
Belajar Bahasa Arab
Belajar Bahasa ArabBelajar Bahasa Arab
Belajar Bahasa Arab
 
Pelan Mengajar Amali
Pelan Mengajar AmaliPelan Mengajar Amali
Pelan Mengajar Amali
 
Ukur terabas teodolit
Ukur terabas teodolitUkur terabas teodolit
Ukur terabas teodolit
 
TNB
TNBTNB
TNB
 
Sistem pengoperasian
Sistem pengoperasianSistem pengoperasian
Sistem pengoperasian
 
2.2 perkakasan peranti inputoutput
2.2 perkakasan  peranti inputoutput2.2 perkakasan  peranti inputoutput
2.2 perkakasan peranti inputoutput
 
State transition diagram 8085
State transition diagram 8085State transition diagram 8085
State transition diagram 8085
 
N300RB-PLUS 快速安裝手冊
N300RB-PLUS 快速安裝手冊N300RB-PLUS 快速安裝手冊
N300RB-PLUS 快速安裝手冊
 
Komponen utama komputer
Komponen utama komputerKomponen utama komputer
Komponen utama komputer
 

Destaque (7)

Pseudocode-Flowchart
Pseudocode-FlowchartPseudocode-Flowchart
Pseudocode-Flowchart
 
Flowchart pseudocode-examples
Flowchart pseudocode-examplesFlowchart pseudocode-examples
Flowchart pseudocode-examples
 
Programming in C++
Programming in C++Programming in C++
Programming in C++
 
GPA calculator and grading program in c++
GPA calculator and grading program in c++GPA calculator and grading program in c++
GPA calculator and grading program in c++
 
conditional statements
conditional statementsconditional statements
conditional statements
 
C if else
C if elseC if else
C if else
 
Scientific calculator in c
Scientific calculator in cScientific calculator in c
Scientific calculator in c
 

Semelhante a Unit 3

The IPO Model of Evaluation (Input-Process-Output)
The IPO Model of Evaluation (Input-Process-Output)The IPO Model of Evaluation (Input-Process-Output)
The IPO Model of Evaluation (Input-Process-Output)
Janilo Sarmiento
 
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docxWeek 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
co4spmeley
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
melbruce90096
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 
optimization process on compiler
optimization process on compileroptimization process on compiler
optimization process on compiler
Santosh Sahu
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 

Semelhante a Unit 3 (20)

Unit 3
Unit 3Unit 3
Unit 3
 
The IPO Model of Evaluation (Input-Process-Output)
The IPO Model of Evaluation (Input-Process-Output)The IPO Model of Evaluation (Input-Process-Output)
The IPO Model of Evaluation (Input-Process-Output)
 
Unit 2
Unit 2Unit 2
Unit 2
 
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docxWeek 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
 
Penyelesaian masalah
Penyelesaian masalahPenyelesaian masalah
Penyelesaian masalah
 
Lecture1
Lecture1Lecture1
Lecture1
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 
Exceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant BhabadExceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant Bhabad
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
optimization process on compiler
optimization process on compileroptimization process on compiler
optimization process on compiler
 
Pseudo code.pptx
Pseudo code.pptxPseudo code.pptx
Pseudo code.pptx
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
Lec1_EENG112-Introduction.pdf
Lec1_EENG112-Introduction.pdfLec1_EENG112-Introduction.pdf
Lec1_EENG112-Introduction.pdf
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 

Mais de rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
rohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 

Mais de rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Unit 3

  • 1. F1001 PROGRAMMING FUNDAMENTALS UNIT 3 CONTROL STRUCTURE  Sequential Control Structure  Selection Control Structure  Decision Control Structure 30
  • 2. F1001 PROGRAMMING FUNDAMENTALS BLOCKS OF CODE • Every programming language consists of thousand of statements. • Thus, to simplify the execution of the programming language, all the statements in the programming language is divided into blocks of code that have specific functions. • Statement in a block of code is arranged sequentially in a program. • A block of code is a group of statements that performs one particular task. • The block is separated in several ways, like curly bracket “{“ and “}”, Begin and End statement and others. • Function is a block of code that has been assigned a name. • Function is used as reference and execution for a block of code. • Combination of blocks of code will produce a perfect program to be executed in a computer. • Since the program comes from the different blocks of code, so, the flow needs to be controlled. • Example of blocks of code: void welcome ( ) { BLOCK OF BLOCK printf (“****************************”); printf (“********WELCOME **********”); CODE SEPARATO printf (“****************************”); R } PROGRAM FLOW • Program flow in computer is controlled by the control structure. • Control structure is a logical structure that controls the flow of instruction to be executed by computer. • Control structure uses single entry and single exit; meaning it has only one beginning and one ending. • There are three types of control structures: 1. Sequential control structure 2. Selection / decision control structure a) If……endif b) If……else c) Nested if 3. Looping control structure a) For b) While c) Do……while 31
  • 3. F1001 PROGRAMMING FUNDAMENTALS Sequential Control Structure  In this control, every step will be executed one by one from top to bottom.  Every box in control structure is a process. Every process is done sequentially.  Format:  Pseudocode: START Start Flowchart Statement A Statement A Statement B Statement B End END  Example 1:  Compute the total overtime wages of an employee.  Problem analysis: Input: o Hours o Basic_salary o OT_rate Process: o Overtime equals OT_rate times Hours o Salary equals Basic_salary plus Overtime Output: o Salary  Algorithm: 1. Enter Hours, Basic_salary, OT_rate 2. Calculate overtime using formula: Overtime = OT_rate * Hours 3. Calculate salary using formula: Salary = Basic_salary + Overtime 4. Output Salary 32
  • 4. F1001 PROGRAMMING FUNDAMENTALS  Pseudocode: START Input Hours, Basic_salary, OT_rate Overtime = OT_rate * Hours Salary = Basic_salary + Overtime Output Salary END  Flowchart: START Input Hours, Basic_salary, OT_rate Overtime = OT_rate * Hours Salary = Basic_salary + Overtime Output Salary END  Example 2:  Problem: Mathematical operation: Get two numbers, then do adding, subtracting, multiplying and dividing operations.  Problem analysis: Input: o number_1, number_2 Process: o Add 2 numbers: Sum = number_1 + number_2 o Minus 2 numbers: Subtract = number_1 – number_2 o Multiply 2 numbers: 33
  • 5. F1001 PROGRAMMING FUNDAMENTALS Multiple = number_1 * number_2 o Division 2 numbers: Divide = number_1 / number_2 Output: o Sum, Subtract, Multiple and Divide  Algorithm: 1. Enter 2 numbers 2. Add 2 numbers Sum = number_1 + number_2 3. Minus 2 numbers Subtract = number_1 – number_2 4. Multiply 2 numbers Multiple = number_1 * number_2 5. Division of 2 numbers Divide = number_1 / number_2 6. Display Sum, Subtract, Multiple and Divide  Flowchart: START Input number_1, number_2 Sum = number_1 + number_2 Subtract = number_1 – number_2 Multiple = number_1 * number_2 Divide = number_1 / number_2 Output Sum, Subtract, Multiple, Divide END 34
  • 6. F1001 PROGRAMMING FUNDAMENTALS  Pseudocode: START Input number_1, number_2 Sum = number_1 + number_2 Subtract = number_1 - number_2 Multiple = = number_1 * number_2 Divide = number_1 / number_2 Output Sum, Subtract, Multiple, Divide END Selection / Decision Control Structure  This type of control structure is usually used in structured programming  This control structure will execute an instruction based on result of a condition or comparison.  A condition will result either TRUE or FALSE.  If the condition result is true, the control program will execute the instruction within the TRUE loop operation.  Otherwise, it will execute the next instruction or the instruction within the FALSE loop operation.  Example 1:  Condition: A person can obtain a license when he/ she is above 21 years old Decision: If true then she / he is qualified to have driving license. If not he / she is not qualified to have a driving license.  Below is a flowchart of control structure: 35
  • 7. F1001 PROGRAMMING FUNDAMENTALS START True Condition Input age False Statement that will be executed if condition True Statement that will be is true If age > 21 executed if condition is not true False Output “Qualified” Output “Not Qualified” END  Type of selection / decision control structure: 1. If……endif 2. If……else 3. Nested if 1. IF…….ENDIF  Rules: If (condition) Instruction (do this instruction if condition is true) Endif If condition is not true, no instruction will be executed 36
  • 8. F1001 PROGRAMMING FUNDAMENTALS  Pseudocode: If (condition) START True statement Endif True Conditi Statement Flowchart:on False END  Example 1:  Workers who work on shift 3 will receive additional Bonus RM50, where basic salary is entered by workers.  Problem analysis: Input: 1. Shift 2. Basic_salary Process: Bonus equals RM 50 If Shift equals to 3: Salary equals Bonus plus Basic_salary Output: Salary  Algorithm: 1. Enter Basic_salary, Shift 2. Bonus equals to RM 50 3. Check workers Shift 3.1 If Shift equals to 3 Salary= Basic_salary + Bonus 4. Display Salary 37
  • 9. F1001 PROGRAMMING FUNDAMENTALS  Flowchart: START Input Shift, Basic_salary Bonus = 50 True If Shift = Salary = Basic_salary + Bonus 3 False Output Salary END  Pseudocode: START Input Shift, Basic_salary Bonus = 50 If (Shift ==3) Salary = Basic_salary + Bonus End if Output Salary END 38
  • 10. F1001 PROGRAMMING FUNDAMENTALS 2. IF…….ELSE type  A selection of control structure is used to select between two options  Pseudocode:  Rules: If (condition) If (condition) True statement True statement Else Else False statement False statement Endif Endif  Flowchart: START False True Conditi on Statement 2 Statement 1 END  Example 1: o Prepare a problem analysis, algorithm, flowchart and pseudocode to identify whether a student is qualified to further her / his studies in any local university using his / her SPM grade equal to 1. o Problem analysis: Input: Grade Process: If Grade is equal to 1 Output “Qualified to further study”. If not Output “Not qualified to further study”. Output: Display message qualified or not 39
  • 11. F1001 PROGRAMMING FUNDAMENTALS o Algorithm: 1. Enter Grade 2. Check Grade 1.1 If Grade = 1 1.1.1 Output “Qualified to further study” 1.2 If not 1.2.1 Output “Not qualified to further study” o Flowchart: START Input Grade False True If Grade Output “Not qualified Output “Qualified to further study” to further study” END o Pseudocode: START Input Grade If (Grade==1) Output “Qualified to further study” Else Output “Not qualified to further study” Endif END 40
  • 12. F1001 PROGRAMMING FUNDAMENTALS  Example 2: o Problem: Prepare the problem analysis, algorithm, flowchart and pseudocode to find subtraction between two numbers that users enter. o Problem analysis: Input: num1, num2 Process: If num1 greater than num2 Result = num1 – num2 If not Result = num2 – num1 Output: Result o Algorithm: 1. Enter num1, num2 2. Compare the 2 numbers 2.1 If num1 greater than num2 2.1.1 Result = num1 – num2 2.2 If not 2.2.1 Result = num2 – num1 3. Display Result o Flowchart: START Input num1, num2 False True If num1 > num2 Result = num2 – num1 Result = num1 – num2 Output Result END 41
  • 13. F1001 PROGRAMMING FUNDAMENTALS o Pseudocode: START Input num1, num2 If num1 > num2 Result = num1 – num2 Else Result = num2 – num1 Endif Output Result END 3. NESTED IF  There are 3 types: 1. Type 1: If (condition1) If (condition2) If (condition3) True statement Endif Endif Endif 2. Type 2: If (condition1) If (condition2) If (condition3) Statement that will be executed if condition1, condition2 and condition3 are true Else Statement that will be executed if condition1, and condition2 are true but condition2 is false Endif 42
  • 14. F1001 PROGRAMMING FUNDAMENTALS Else Statement that will be executed if condition1 is true but condition2 and condition3 is false Endif Else Statement that will be executed if condition1 is false Endif 3. Type 3 If (condition1) Statement that will be executed if condition 1 is true Else If (condition 2) Statement that will be executed if condition2 is true but condition1 is false Else If (condition3) Statement that will be executed if condition3 is true but condition1 and condition2 are false Else Statement that will be executed if condition1, condition2 and condition3 are false Endif Endif End if  Example Type 1: o Problem: To determine whether a candidate is qualified or not to get a scholarship based on his / her study years, guardian’s salary and student CGPA. If the study year is more than 1, student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500, student will be considered for a scholarship. o Problem analysis: Input: CGPA, Year, Salary. Process: 43
  • 15. F1001 PROGRAMMING FUNDAMENTALS 1. Check if the student application’s can be considered or not for a scholarship. 1.1 If Year greater than 1 1.1.1 If CGPA greater than or equal to 3.00 1.1.1.1 If Salary is less than or equal to RM500 Output “Your application is under consideration”. Output: Student status o Algorithm: 1. Enter CGPA, Salary and Year 2. Check if the student application’s can be considered for a scholarship 2.11 If year > 1 2.1.1 If CGPA >= 3.00 2.1.1.1 If salary <= RM500 Output “Your application is under consideration” 3.1 Display status o Flowchart: START Input Year, CGPA, Salary False True If Year > 1 False If CGPA True >= 3.00 Output “Your False If Salary True application is under <= 500 consideration” END 44
  • 16. F1001 PROGRAMMING FUNDAMENTALS o Pseudocode: START Input Year, CGPA, Salary If Year >1 If CGPA >= 3.00 If Salary <= RM500 Output “Your application is under consideration” Endif Endif Endif END  Example Type 2: o Problem: To determine whether a candidate is qualified or not to get a scholarship based on his / her study years, guardian’s salary and student CGPA. If the study year is more than 1, student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500, student will be considered for a scholarship. If the student is not qualified the message “Not success” will be displayed. o Problem analysis: Input: CGPA, Year, Salary Process: 1. Check if a student can be considered for a scholarship 1.1 If Year > 1 1.1.1 If CGPA >= 3.00 1.1.1.1 If Gaji <= 500 Output “You application is under consideration” 1.1.1.2 If not Output “Not success” 1.1.2 If not Output “Not success” 1.2 If not Output “Not success” Output: Student status 45
  • 17. F1001 PROGRAMMING FUNDAMENTALS o Algorithm: 1. Enter CGPA, Year, Salary 2. Check if a student can be considered for a scholarship 2.1 If Year > 1 2.1.1 If CGPA >= 3.00 2.1.1.1 If Salary <= RM500 Output “Your application is under consideration”. 2.1.1.2 If not Output “Not success” 2.1.2 If not Output “Not success” 2.2 If not Output “Not success” 3. Display status o Flowchart: START Input Year, CGPA, Salary False True If Year > 1 Output “Not False If CGPA True success” >= 3.00 Output “Your Output “Not False If Salary True application is under success” <= 500 consideration” Output “Not success” END 46
  • 18. F1001 PROGRAMMING FUNDAMENTALS o Pseudocode: Input CGPA, Salary, Year If (year > 1) If (CGPA >= 3.00) If (salary <= RM500) Output “Your application is under consideration” Else Output ”Not success” Endif Else Output ”Not success” Endif Else Output ”Not success” Endif  Example Type 3: o Problem: Education status is determined based on the GPA achievement under the following scheme: GPA Status 3.50-4.00 Dean List 2.00-3.49 Pass 1.80-1.99 Conditional Pass 0.00-1.79 Fail o Problem analysis: Input: GPA Process: 1. If (GPA < 0.00 AND GPA > 4.00) Output “Invalid data” 2. If not 2.1 If (GPA >=3.5 AND GPA <= 4.00) Output “Dean List” 2.2 If not 2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass” 2.2.2 If not 2.2.2.1 If (GPA >= 1.80 AND GPA< 2.00) Output “Conditional Pass” 2.2.2.2 If not Output “Fail” Output: Student education status 47
  • 19. F1001 PROGRAMMING FUNDAMENTALS o Algorithm: 1. Enter student GPA 2. Compare student’s GPA to determine his/ her education status. 2.1 If (GPA < 0.00 AND GPA > 4.00) Output “Invalid data” 2.2 If not 2.2.1 If (GPA >=3.50 AND GPA <= 4.00) Output “Dean List” 2.2.2 If not 2.2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass” 2.2.2.2 If not 2.2.2.2.1If (GPA >= 1.80 AND GPA < 2.00) Output “Conditional Pass” 2.2.2.2.2If not Output “Fail” 3. Print status o Flowchart: 48
  • 20. F1001 PROGRAMMING FUNDAMENTALS START Input GPA True False If GPA < 0.00 && GPA > 4.00 True False If GPA >= 3.50 && Output “Invalid GPA <= 4.00 data” Output “Dean True False If GPA >= 2.00 && List” GPA < 3.50 True False If GPA >= 1.80 && Output “Pass” GPA < 2.00 Output Output “Fail” “Conditional Pass” END o Pseudocode: START Input GPA If ((GPA < 0.00) AND (GPA > 4.00)) Output "Invalid Data" Else If ((GPA >= 3.50) AND (GPA <= 4.00)) Output "Dean List" Else If ((GPA >=2.00) AND (GPA < 3.50)) Output "Pass" Else 49
  • 21. F1001 PROGRAMMING FUNDAMENTALS If ((GPA >= 1.80) AND (GPA < 2.00)) Output "Conditional Pass” Else Output "Fail" Endif Endif Endif Endif END  Usage of Logical Operator: Symbol Symbol Example Result Description (Pseudocode) (C Language) AND && (1 > 3) && (10 < 20) FALSE Both sides of the condition must be true OR || (1 > 3) || (10 < 20) TRUE Either one of the condition must be true NOT ! ! (10 < 20) FALSE Change the operation either from true to false or vise versa o From the above table, the logical operator are AND, OR and NOT. o Example using of AND operator: If ((a < b) && (c > d)) printf(“Print a and c”); o If condition (a < b) is true AND condition (c > d) is true then “Print a and c” will be displayed. If one of the condition is not true then “Print a and c” will not be displayed. o Example usage of OR operator: If ((sales > 5000) || (hoursworked> 81)) bonus = 500; else bonus = 0; 50
  • 22. F1001 PROGRAMMING FUNDAMENTALS o If the sales are more than 5000 OR working hours are more than 81, bonus RM500 will be given. If either condition is not fulfilled, still the amount of RM500 will still be given as bonus. o If both conditions are false, then bonus will not be given. Looping Control Structure  A programming control structure that allows a series of instructions to be executed more than once. The similar statement is repeated several times until the conditions are fulfilled.  Three are three types of loop: o For o While o Do…while For While Do…while For (initialize; condition; counter) While (condition) Do True statement if condition is True statement True statement fulfilled Endwhile While (condition) Endfor o Initialize value: a value to start a loop. o Counter: to increase or decrease the initialize value. o Rules for the condition: - If the condition is true / fulfilled, the process will be performed. - Then, the program loops back and recheck the condition, if the condition is true / fulfilled, repeat the process. - When the condition is false, then exit the loop.  Format for Do …… while loop:  Format for the For and While loops: START START Initialize Initialize Statement A Test False END True condition Test condition True False  Example of loops usage: A Statement END 51
  • 23. F1001 PROGRAMMING FUNDAMENTALS o Problem: Prepare the problem analysis, algorithm, flowchart and pseudocode for the average of 5 numbers. Data will be entered by user. o Problem analysis: Input: 5 numbers Process: The process of adding numbers will repeat until the condition to exit the loop is met. Output: Average of 5 numbers o Algorithm: 1. Initialize Counter=0; Average = 0; Total = 0 2. Input number 3. Add Total using formula: Total = Total + number 4. Add Counter using formula: Counter = Counter + 1 5. Compare whether Counter is greater than 5 If yes , go to step 6 If not, go to step 2 6. Calculate Average of numbers using formula; Average = Total/5 7. Display Average o Pseudocode: For loop While loop Do…while loop START START START no = 0 no = 0 no = 0 Total = 0 Total = 0 Total = 0 Avg = 0 Avg = 0 Avg = 0 For (no=0; no<=5; no++) While (no <= 5) Do { Input Num Input Num Input Num Total = Total + Num Total = Total + Num Total = Total + Num Endfor no = no + 1 no = no + 1 Avg = Total/5 Endwhile } While (no <= 5) Output Avg Avg = Total/5 Avg = Total/5 END Output Avg Output Avg END END o Flowchart for For and While loops: 52
  • 24. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Total = 0 Avg = 0 For / False While no Avg = Total/5 True Input Num Output Avg Total = Total + Num END no = no + 1 o Flowchart for Do……while loop: 53
  • 25. F1001 PROGRAMMING FUNDAMENTALS START Note: no: a variable to control loop no = 0 structure whether to continue or exit from the loop Total= 0 no + 1: a counter and it is very Avg = 0 important. Without a counter, the loop will be infinite Input Num Total = Total + Num: a variable to compute the value Total = Total + Num no = no + 1 True False no <= 5 Avg = Total/5 Output Avg END o Example: To compute salary of 20 employees Algorithm for For and While loops: 1. Initialize Counter = 0, Salary = 0; 2. Compare whether Counter is greater than 20 or not If yes , out from the loop If not , go to step 3 3. Enter Basic_salary, Claim, O_time 4. Calculate EPF: EPF = Basic_salary * 0.09 5. Calculate Salary using this formula: Salary = Basic_salary + Claim + O_time – EPF 6. Display Salary 7. Add Counter using the formula: Flowchart for For and While loops: Counter = Counter + 1 8. Back to step 2 54
  • 26. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Salary = 0 For / False END While no True Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 Pseudocode for For loop: START no = 0 Salary = 0 For (no = 0, no <=20, no ++) Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary Endfor END Pseudocode for While loop: 55
  • 27. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Salary = 0 While no <=20 Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Gaji = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 Endwhile END Algorithm for Do……while loop: 1. Initialize Counter = 0, Salary = 0; 2. Input Basic_salary, Claim, O_time 3. Calculate EPF EPF = Basic_salary * 0.09 4. Calculate Salary using this formula: Salary = Basic_salary + Claim + O_time – EPF 5. Add Counter using this formula: Counter = Counter + 1 6. Compare whether the Counter is greater than 20 or not If yes , out of loop If not, go to step 2 7. Display Salary Flowchart for Do…..while loop: 56
  • 28. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Salary = 0 Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 True False no <= 5 END Pseudocode for Do…..while loop: START no = 0 Salary = 0 Do { Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 } While (no <= 20) o Dummy Data or Sentinel Value END 57
  • 29. F1001 PROGRAMMING FUNDAMENTALS - Value used to end the program. Dummy data must be different from the data to be entered. - Example: START Input name While name != “XXX” Dummy data Output name Input mark Output mark Input name Endwhile END 58