Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the...

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

If you can't read please download the document

description

Subtopic: Loop Control Statements

Transcript of Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the...

Topic: Control Statements 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 Subtopic: Loop Control Statements 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 employees 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. Types of Loops For Loop While Loop Repeat Until Loop 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. 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 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 Flowchart Initialize variable Test variable Increment variable Instructions Yes No 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 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 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 Flowchart Get value for variable Test variable Get value for variable Instructions Yes No 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 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. 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 c) Draw the flowchart count = 1 START STOP gross = basic + allowance count = count + 1 count