SlideShare une entreprise Scribd logo
1  sur  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 ( )
                         {
                                  printf (“****************************”);                   BLOCK OF
    BLOCK                                                                                     BLOCK OF
     BLOCK                        printf (“********WELCOME **********”);                       CODE
    SEPARATO                      printf (“****************************”);                      CODE
     SEPARATO
    R                    }
     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:                        Flowchart

                                                                      START
                   Start

                                                                    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:


                                                                                               33
F1001 PROGRAMMING FUNDAMENTALS


         Subtract = number_1 – number_2
    o    Multiply 2 numbers:
         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
                                                                              If age > 21
         Statement that will be                 is true
         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




                                                                                                           36
F1001 PROGRAMMING FUNDAMENTALS


        If condition is not true, no instruction will be executed




   Pseudocode:
                                                Flowchart:
    If (condition)
                                                         START
        True statement
    Endif
                                                                        True
                                                         Conditi               Statement
                                                           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
        Rules:                                                     Pseudocode:

         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”.


                                                                                                           39
F1001 PROGRAMMING FUNDAMENTALS


                         If not
                                   Output “Not qualified to further study”.
        Output: Display message qualified or not




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
                                    == 1

    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”



                                                                                 40
F1001 PROGRAMMING FUNDAMENTALS


                 Else
                            Output “Not qualified to further study”

                 Endif
            END


   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
                                     START
    o    Flowchart:
                                Input num1, num2



             False                                         True
                                     If num1 >
                                        num2



Result = num2 – num1                                    Result = num1 – num2




                                 Output Result
                                                                                  41

                                       END
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)




                                                                          42
F1001 PROGRAMMING FUNDAMENTALS


                                               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
                             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,


                                                                                                     43
F1001 PROGRAMMING FUNDAMENTALS


              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:
             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.11If 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”



                                                                                                      44

                                             END
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



                                                                                                    45
F1001 PROGRAMMING FUNDAMENTALS


                                                           Output “Not success”
                               1.1.2        If not
                                                 Output “Not success”
                        1.2 If not
                                      Output “Not success”
            Output: Student status




     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
               START

     o       Flowchart:
         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
                      success”                                                     application is under
                                                            <= 500
                                                                                     consideration”

                                            Output “Not
                                             success”



                                                                                                             46

                                                 END
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


                                                                                          47
F1001 PROGRAMMING FUNDAMENTALS


                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

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




                                                                                 48
F1001 PROGRAMMING FUNDAMENTALS




          o   Flowchart:


                  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 &&
                                                                         GPA < 2.00
                                 Output “Pass”


                                                         Output
                                                                                  Output “Fail”
                                                    “Conditional Pass”




                                        END


          o     Pseudocode:
                   START
                   Input GPA
                       If ((GPA < 0.00) AND (GPA > 4.00))


                                                                                                   49
F1001 PROGRAMMING FUNDAMENTALS


                               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
                                             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.




                                                                                                              50
F1001 PROGRAMMING FUNDAMENTALS


            o       Example usage of OR operator:



                                  If ((sales > 5000) || (hoursworked> 81))
                                             bonus = 500;
                                  else
                                     bonus = 0;
            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.
                                                                    Format for Do …… while loop:
                -    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.
                                                                                           START
                 START
            Format for the For and While loops:                                          Initialize

                     Initialize
                                                                                        Statement A


                                                                              True          Test
                        Test                  False
                                                            END                           condition
                      condition                                                              A
                         A                                                                      False          51
                                True

                     Statement A                                                           END
F1001 PROGRAMMING FUNDAMENTALS




   Example of loops usage:

    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)



                                                                                                              52
F1001 PROGRAMMING FUNDAMENTALS


 Output Avg                      Avg = Total/5                   Avg = Total/5
END                              Output Avg                      Output Avg
                             END                              END



      o   Flowchart for For and While loops:




                    START


                    no = 0


                   Total = 0


                    Avg = 0



                     For /             False
                    While no                     Avg = Total/5
                     <= 5
                 True


                   Input Num                     Output Avg



               Total = Total + Num                   END


                   no = no + 1




                                                                                 53
F1001 PROGRAMMING FUNDAMENTALS




o      Flowchart for Do……while loop:

                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
        1.   Initialize Counter = 0, Salary = 0;
        2. Compare whether Counter is greater than 20 or not
o      Example: To compute salary of 20 employees
            If yes , out from the loop
       Algorithm for For and While loops:
            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:
             Counter = Counter + 1
                                                                                          54
        8.   Back to step 2
F1001 PROGRAMMING FUNDAMENTALS




  Flowchart for For and While loops:


               START


               no = 0


              Salary = 0



                                 False
                For /
                                           END
               While no
            True<= 20


                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                                             55
     END
F1001 PROGRAMMING FUNDAMENTALS




Pseudocode for While loop:


     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




                                                             56
F1001 PROGRAMMING FUNDAMENTALS




 Flowchart for Do…..while loop:

                     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

 START
    no = 0
 Pseudocode for Do…..while loop:
    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)                                      57
 END
F1001 PROGRAMMING FUNDAMENTALS




o   Dummy Data or Sentinel Value
    -   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

Contenu connexe

Tendances

Rpl 05 - persyaratan perangkat lunak
Rpl   05 - persyaratan perangkat lunakRpl   05 - persyaratan perangkat lunak
Rpl 05 - persyaratan perangkat lunak
Febriyani Syafri
 
LAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUS
LAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUSLAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUS
LAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUS
Bima Febrian Mandala Putra
 
Arsitektur Prosesor AMD
Arsitektur Prosesor AMDArsitektur Prosesor AMD
Arsitektur Prosesor AMD
PTIK BB
 

Tendances (20)

Instalasi dan Konfigurasi Mikrotik CHR pada Proxmox VE 5.1
Instalasi dan Konfigurasi Mikrotik CHR pada Proxmox VE 5.1Instalasi dan Konfigurasi Mikrotik CHR pada Proxmox VE 5.1
Instalasi dan Konfigurasi Mikrotik CHR pada Proxmox VE 5.1
 
Législatives 2022 | Sociologie des électorats et profil des abstentionnistes ...
Législatives 2022 | Sociologie des électorats et profil des abstentionnistes ...Législatives 2022 | Sociologie des électorats et profil des abstentionnistes ...
Législatives 2022 | Sociologie des électorats et profil des abstentionnistes ...
 
Tugas mandiri struktur data
Tugas mandiri struktur dataTugas mandiri struktur data
Tugas mandiri struktur data
 
Bisnis It 1
Bisnis It 1Bisnis It 1
Bisnis It 1
 
Pengertian sistem berkas
Pengertian sistem berkasPengertian sistem berkas
Pengertian sistem berkas
 
Rpl 05 - persyaratan perangkat lunak
Rpl   05 - persyaratan perangkat lunakRpl   05 - persyaratan perangkat lunak
Rpl 05 - persyaratan perangkat lunak
 
4. magnetic storage
4. magnetic storage4. magnetic storage
4. magnetic storage
 
LAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUS
LAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUSLAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUS
LAPORAN RESMI BASIS DATA PENGATURAN OUTPUT PADA SQL*PLUS
 
Spesifikasi server
Spesifikasi serverSpesifikasi server
Spesifikasi server
 
Kamus data (data dictionary) - (Bambang Sugianto - Politeknik Sawunggalih Aji...
Kamus data (data dictionary) - (Bambang Sugianto - Politeknik Sawunggalih Aji...Kamus data (data dictionary) - (Bambang Sugianto - Politeknik Sawunggalih Aji...
Kamus data (data dictionary) - (Bambang Sugianto - Politeknik Sawunggalih Aji...
 
CA_Module_1.pptx
CA_Module_1.pptxCA_Module_1.pptx
CA_Module_1.pptx
 
07 prinsip kerja softswitch
07 prinsip kerja softswitch07 prinsip kerja softswitch
07 prinsip kerja softswitch
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Power Point Processor x86 and arm
Power Point Processor x86 and armPower Point Processor x86 and arm
Power Point Processor x86 and arm
 
Permasalahan system telekomunikasi
Permasalahan system telekomunikasiPermasalahan system telekomunikasi
Permasalahan system telekomunikasi
 
4. pengamanan sistem operasi
4. pengamanan sistem operasi4. pengamanan sistem operasi
4. pengamanan sistem operasi
 
Pengantar database
Pengantar databasePengantar database
Pengantar database
 
Tugas sistem basis data kelompok
Tugas sistem basis data kelompokTugas sistem basis data kelompok
Tugas sistem basis data kelompok
 
Arsitektur Prosesor AMD
Arsitektur Prosesor AMDArsitektur Prosesor AMD
Arsitektur Prosesor AMD
 
Array dan Contoh
Array dan ContohArray dan Contoh
Array dan Contoh
 

En vedette (15)

Unit 1
Unit 1Unit 1
Unit 1
 
Unit 2
Unit 2Unit 2
Unit 2
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 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 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit1 1
Fp201 unit1 1Fp201 unit1 1
Fp201 unit1 1
 
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
 
Kandungan nota
Kandungan notaKandungan nota
Kandungan nota
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Nota (first)
Nota (first)Nota (first)
Nota (first)
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 

Similaire à 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
 
optimization process on compiler
optimization process on compileroptimization process on compiler
optimization process on compiler
Santosh Sahu
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 

Similaire à 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)
 
Penyelesaian masalah
Penyelesaian masalahPenyelesaian masalah
Penyelesaian masalah
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
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
 
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
 
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
 
Lecture1
Lecture1Lecture1
Lecture1
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
Switch Control and Time Delay - Keypad
Switch Control and Time Delay - KeypadSwitch Control and Time Delay - Keypad
Switch Control and Time Delay - Keypad
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
Pl sql guide
Pl sql guidePl sql guide
Pl sql guide
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 

Plus de rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
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
 
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
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
rohassanie
 

Plus de rohassanie (18)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
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
 
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
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

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 ( ) { printf (“****************************”); BLOCK OF BLOCK BLOCK OF BLOCK printf (“********WELCOME **********”); CODE SEPARATO printf (“****************************”); CODE SEPARATO R } 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: Flowchart START Start 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: 33
  • 5. F1001 PROGRAMMING FUNDAMENTALS Subtract = number_1 – number_2 o Multiply 2 numbers: 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 If age > 21 Statement that will be is true 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 36
  • 8. F1001 PROGRAMMING FUNDAMENTALS If condition is not true, no instruction will be executed  Pseudocode: Flowchart: If (condition) START True statement Endif True Conditi Statement 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  Rules:  Pseudocode: 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”. 39
  • 11. F1001 PROGRAMMING FUNDAMENTALS If not Output “Not qualified to further study”. Output: Display message qualified or not 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 == 1 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” 40
  • 12. F1001 PROGRAMMING FUNDAMENTALS Else Output “Not qualified to further study” Endif END  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 START o Flowchart: Input num1, num2 False True If num1 > num2 Result = num2 – num1 Result = num1 – num2 Output Result 41 END
  • 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) 42
  • 14. F1001 PROGRAMMING FUNDAMENTALS 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 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, 43
  • 15. F1001 PROGRAMMING FUNDAMENTALS 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: 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.11If 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” 44 END
  • 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 45
  • 17. F1001 PROGRAMMING FUNDAMENTALS Output “Not success” 1.1.2 If not Output “Not success” 1.2 If not Output “Not success” Output: Student status 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 START o Flowchart: 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 success” application is under <= 500 consideration” Output “Not success” 46 END
  • 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 47
  • 19. F1001 PROGRAMMING FUNDAMENTALS 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 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 48
  • 20. F1001 PROGRAMMING FUNDAMENTALS o Flowchart: 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 && GPA < 2.00 Output “Pass” Output Output “Fail” “Conditional Pass” END o Pseudocode: START Input GPA If ((GPA < 0.00) AND (GPA > 4.00)) 49
  • 21. F1001 PROGRAMMING FUNDAMENTALS 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 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. 50
  • 22. F1001 PROGRAMMING FUNDAMENTALS o Example usage of OR operator: If ((sales > 5000) || (hoursworked> 81)) bonus = 500; else bonus = 0; 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.  Format for Do …… while loop: - 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. START START  Format for the For and While loops: Initialize Initialize Statement A True Test Test False END condition condition A A False 51 True Statement A END
  • 23. F1001 PROGRAMMING FUNDAMENTALS  Example of loops usage: 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) 52
  • 24. F1001 PROGRAMMING FUNDAMENTALS Output Avg Avg = Total/5 Avg = Total/5 END Output Avg Output Avg END END o Flowchart for For and While loops: START no = 0 Total = 0 Avg = 0 For / False While no Avg = Total/5 <= 5 True Input Num Output Avg Total = Total + Num END no = no + 1 53
  • 25. F1001 PROGRAMMING FUNDAMENTALS o Flowchart for Do……while loop: 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 1. Initialize Counter = 0, Salary = 0; 2. Compare whether Counter is greater than 20 or not o Example: To compute salary of 20 employees If yes , out from the loop Algorithm for For and While loops: 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: Counter = Counter + 1 54 8. Back to step 2
  • 26. F1001 PROGRAMMING FUNDAMENTALS Flowchart for For and While loops: START no = 0 Salary = 0 False For / END While no True<= 20 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 55 END
  • 27. F1001 PROGRAMMING FUNDAMENTALS Pseudocode for While loop: 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 56
  • 28. F1001 PROGRAMMING FUNDAMENTALS Flowchart for Do…..while loop: 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 START no = 0 Pseudocode for Do…..while loop: 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) 57 END
  • 29. F1001 PROGRAMMING FUNDAMENTALS o Dummy Data or Sentinel Value - 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