Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.

Similar presentations


Presentation on theme: "Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and."— Presentation transcript:

1 Topic: Control Statements

2 Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and calculated the gross salary by adding the basic salary and the allowance amount. The program should output the gross salary for the employee. Requirements: List the operations List variable Write Pseudocode Draw flowchart

3 Subtopic: Loop Control Statements

4 Loops (or Iteration or Repetition) The solution to many problems requires performing some of the instructions more than once. Example: Calculate the gross pay for a number of employees. In this example, each employee’s gross pay is calculated using the same method. Hence the same instructions that are used for the first employee can be repeated for all the employee. A loop is used to repeatedly perform an operation or a block of code through the use of a conditional expression.

5 Types of Loops For Loop While Loop Repeat Until Loop

6 Cases in which types of Loops are used: A loop that involves repeating the instructions a predetermined (known) number of times. The loop constructs that can be use in this case are the for and the while construct. In this case a counter must be used to control the number of times the loop is repeated. A loop that involves repeating the instructions for an unknown number of times. The loop constructs that can be use in this case are the while and the repeat construct.

7 For Loop Construct Number of times loop is to be repeated is predetermined. Pseudocode For variable = beginning to ending do Instructions to be repeated Endfor

8 For variable = beginning to ending do “beginning to ending” - represents the range Example: For number = 1 to 10 do 1 to 10 is the range This one line is used to: Initialize the variable to the first value in the range Test the variable with the values in the range Increment the variable until the test is false

9 Flowchart Initialize variable Test variable Increment variable Instructions Yes No

10 While Loop Construct Number of times that the loop is to be repeated is predetermined. Pseudocode Initialize variable to be used in condition While condition do Instructions to be repeated Increment the variable used in the condition Endwhile Note: The condition is where the variable is tested

11 Flowchart Initialize variable Test variable Increment variable Instructions Yes Note: When the number of times that the loop will be repeated is predetermined, the flowcharts are the same with for loop and while loop. No

12 While Loop Construct Number of times that the loop is to be repeated is unknown. Pseudocode Get value for the variable to be used in condition While condition do Instructions to be repeated Get value for the variable to be used in condition Endwhile

13 Flowchart Get value for variable Test variable Get value for variable Instructions Yes No

14 Problem 1 Write a program that accepts the basic salary and allowance amount for ten (10) employees and calculate the gross salary by adding the basic salary and the allowance amount. The program should output the gross salary for the employees. Requirements: a) State the control structure to be used. b) Write the pseudocode c) Draw the flowchart

15 a) State the control structure to be used Answer Loop control structure - for loop or while loop Reason: The number of employees is known, as a result the number of times the loop is to be repeated is predetermined.

16 b) Write the Pseudocode Answer if the for loop is used Start Declare basic, allowance, gross as Real count as Integer Print “A program that calculates the gross salary amount for ten employees” for count = 1 to 10 do Print “Enter basic salary” Read basic Print “Enter allowance” Read allowance gross = basic + allowance Print “Gross salary is ”, gross endfor Stop Answer if the while loop is used Start Declare basic, allowance, gross as Real count as Integer Print “A program that calculates the gross salary amount for ten employees” count = 1 while count < = 10 do Print “Enter basic salary” Read basic Print “Enter allowance” Read allowance gross = basic + allowance Print “Gross salary is ”, gross count = count + 1 endwhile Stop

17 c) Draw the flowchart count = 1 START STOP gross = basic + allowance count = count + 1 count <= 10 Print “Enter basic salary and allowance” Read basic, allowance Print gross No Yes

18 Problem 2 Write a program that accepts the basic salary and allowance amount for employees and calculate the gross salary by adding the basic salary and the allowance amount. Output the gross salary for the employees. The program should be terminated by zero (0). Requirements: State the control structure to be used. Write the pseudocode Draw the flowchart

19 a) State the control structure to be used Answer Loop control structure - while loop or repeat until loop Reason: The number of employees is unknown, as a result the number of times the loop is to be repeated is not known.

20 b) Write the Pseudocode Start Declare basic, allowance, gross as Real Print “A program that calculates the gross salary amount for employees” Print “Enter basic salary” Read basic while basic 0 do Print “Enter allowance” Read allowance gross = basic + allowance Print “Gross salary is ”, gross Print “Enter basic salary” Read basic endwhile Stop

21 c) Draw the flowchart START STOP gross = basic + allowance basic 0 Print “Enter allowance” Read allowance Print gross No Yes Read basic Print “Enter basic salary” Read basic Print “Enter basic salary”

22 Homework


Download ppt "Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and."

Similar presentations


Ads by Google