Martin
PT 1420
Unit7 Assignment1: Homework
Short Answer
1. Why should you indent the statement in the body of a loop? By indenting the statements, you make them stand out from the surrounding code. This helps you to identify at a glance the statements that are conditionally executed by a loop.
2. Described the difference between pretest loop and posttest loops. A pretest loop tests its condition before each iteration. A posttest loop tests its condition after each iteration. A posttest loop will always execute at least once.
3. What is a condition-controlled loop? Because they are only executed when a condition is true
4. What is a count-controlled loop? It is a condition-controlled loop with predictable number of iterations. The loop starts at some number (usually the topmost number or zero), then counts down until it reaches the limit (usually zero or the topmost number), at which point it exist the loop. The other common type starts at zero or one, and increments until it reaches a destination number.
5. What three do count-controlled loops typically perform using the counter variables? Test, Initialize and Increment
Algorithm
1. Design a while loop that lets the user enter a number. The number should be multiplied by 10, and the result stored in a variable named products. The loop should iterate as long as product contains a value less than 100. Dim product as integer While product < 100 Display “What is your number” Input number Product = number * 10 Display “Your number is”, product End While
2. Design a Do-While loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user if he or she wishes to perform the operation again. If so, the loop should repeat, otherwise it should terminate. Declare 1num as Real Declare 2num as Real Declare SunNum as Real Do Display “What is your first number” Input 1num = “” Display “What is your second number” Input 2num SumNum =