Top-Rated Free Essay
Preview

unit 7 assignment 1

Satisfactory Essays
431 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
unit 7 assignment 1
Travis Brister
Intro to Programming
Unit 7 Assignment 1: Homework
1. Why should you indent the statements in the body of a loop? You visually set them apart from the surrounding code.
2. Describe the difference between pretest loops and posttest loops? A pretest loop is a loop tests the condition before performing the iteration. A posttest loop performs the iteration, then tests the condition.
3. What is a condition-controlled loop? A condition-controlled loop uses a true/false condition to control the number of times it repeats.
4. What is a count-controlled loop? A count-controlled loop repeats a specific number of times.
5. What three actions do count-controlled loops typically perform using the counter variable? Initialization: Before the loop begins, the counter variable is initialized to a starting value. Test : The loop tests the counter variable by comparing it to a maximum value. If the counter variable is less than or equal to the maximum, the loop iterates. If the counter is greater than the maximum value, the program exits the loop. Increment : To increment a variable means to increase its value. During each iteration, the loop increments the counter variable by adding a predetermined amount to it

Algorithm Workbench
1.declare integer product declare integer number product = 0 do while product < 100 display "Enter a number" input number product = number * 10 loop display product
2. Design a Do-While loop that lets the user enter two numbers. do{ sum = 0; print("Enter A: ") read A print("Enter B: ") read B sum = A + B print("Sum = " + sum) print("Do you wish to continue?") read RESPONSE } while(RESPONSE) 7. Convert the While loop in the following code to a Do-While loop: int x; do { cout << "enter a number: "; cin >> x; } while (x > 0) 8. Convert the Do-While loop in the following code to a While loop:
While sure ! = “y”
Display “Are you sure you want to quit?”
Input sure
End While
3. def check_budget ( expenses , budget ) : if expenses < budget : difference = format ( budget−expenses, ’ ,.2 f ’ ) print (”You were $” , difference , ” under budget .” , sep=””) if expenses > budget : difference = format ( expenses − budget , ’ ,.2 f ’ ) print (”You were $” , difference , ” over budget .” , sep=””) else : print (”You were right on budget . Congrats !”)

budget = float ( input (”What’ s your budget this month? ”) ) print (” Please enter each of your expenses from this month :”)

total expenses = 0 keep going = ’y ’

while keep going == ’y ’ : expense = float ( input (”How much was the expense? $”) ) total expenses = total expenses + expense keepgoing = input (”Do you have any additional expenses ? (
Enter y for yes . ) ”)
Check budget ( total expenses , budget )

You May Also Find These Documents Helpful