This lab accompanies Chapter 5 (pp. 163-183 and pp. 196-201) of Starting Out with Programming Logic & Design.
Name: ___________________________
Lab 7.1 –Condition Controlled with While and Do-While Loops: Pseudocode
Critical Review
A repetition structure causes a statement or set of statements to execute repeatedly.
Repetition structures are used to perform the same task over and over.
Repetition structures are commonly called loops
A condition-controlled loop uses a true/false condition to control the number of times that it repeats.
The general structure of a While loop with a condition-controlled statement is:
//Declare loop control variable
While condition Statement Statement Etc. //Ask Question that changes the loop control variable
End While
The general structure of a Do While loop with a condition-controlled statement is:
//Declare loop control variable
Do
Statement Statement Etc. //Ask Question that changes the loop control variable
While Condition
Help Video: Double click the file to view video
Step 1: Examine the following pseudocode main module from Lab 4.1. Loops are commonly used to call modules multiple times. The best design is to use a loop around the module calls in Main.
Module main () //Declare local variables
Declare String clientName = “ “
Declare Real feetUTP = 0
Declare Real subTotal = 0
Declare Real taxCost = 0
Declare Real totalCost = 0
//Module calls
Call inputData(feetUTP, clientName)
Call calcCosts(feetUTP, subTotal, taxCost, totalCost)
Call displayBill(clientName, totalCost)
End Module
Step 2: In the space provided, re-write the pseudocode for the main module with a condition controlled While or a Do-While loop so that the program runs multiple times. Create a loop control variable named keepGoing of the data type string and initialize this variable to “y”. Don’t forget to indent the contents of your loop. (Reference: Modularizing the Code in the Body of a Loop, page 172).
Module main () Declare String keepGoing = y While keepGoing == y
Declare String clientName =
Declare Real feetUTP = 0
Declare Real subTotal = 0
Declare Real taxCost = 0
Declare Real totalCost = 0
Call inputData (feetUTP, clientName)
Call calcCosts (feetUTP, subTotal, taxCost, totalCost)
Call displayBill (clientName, totalCost)
End While
End Module
Lab 7.2 –Condition Controlled with While and Do-While Loops: Flowcharts
Critical Review
In a while loop, the question is asked first. After the statements process, the control goes back above the condition.
In a do-while loop, the question is asked last. The statements always process at least one time.
Help Video: Double click the file to view video
Step 1: Examine the following main module from Lab 4.2. Loops are commonly used to call modules multiple times. The best design is to use a loop around the module calls in Main (Reference: Modularizing the Code in the Body of a Loop, p. 173).
Step 2: Redesign the main module with a condition controlled While or a Do-While loop so that the program runs multiple times, based on the pseudocode from Lab 7-1.
PASTE FLOWCHART HERE
Lab 7.3 –Count Controlled with While and Do-While Loops: Pseudocode
Critical Review
A count-controlled loop repeats a specific number of times.
The loop keeps a count of the number of times that it iterates, and when the count reaches a specified amount the loop stops.
A variable, known as a counter variable, is used to store the number of iterations that it has performed.
The three actions that take place are initialization, test, and increment.
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.
Increment: To increment a variable means to increase its value. This is done by adding one to the loop control variable.
Any loop can be used with a count-controlled loop.
The general structure of a While loop (although it is the same process with a Do-While loop) with a count-controlled statement is:
//Declare loop control variable
Declare Integer counter = 1
While condition Statement Statement Etc. //increment counter Set counter = counter + 1
End While
Help Video: Double click the file to view video
Step 1: Understanding how to raise a number to the power of 2 is very important to the field of networking as it relates to binary conversion. Convert the following algorithm to pseudocode using a count controlled do while or while loop:
1. Create three integer variables called toPower, number, and counter. Set counter to 0, and toPower and number to 2.
2. Write a do while loop or while loop that will run 7 iterations.
3. Inside the loop, set toPower equal to 2 to the power of the number variable.
4. Display “2 to the power of”, number, “ is”, toPower to the screen
5. Increment counter and number by 1
Module Main()
Declare integer toPower = 2
Declare integer number = 2
Declare integer counter = 0
While Counter < <7
Set toPower = 2^Number
Display 2 to the power of, number, is,toPower, Counter += 1
Number += 1
End While
End Module
Lab 7.4 –Count Controlled with While and Do-While Loops: Flowcharts
Critical Review
In a count controlled while or do-while loop, the flowchart is essentially the same as a condition controlled, except you must create a counter and manually increment the counter variable. The general flow using a while loop looks as follows:
Help Video: Double click the file to view video
Step 1: Using your pseudocode from lab 7-3, design either a while or a do while loop of raising 2 to the power of 2-8 to the screen.
PASTE FLOWCHART HERE
Lab 7.5 –While and Do While Loops: Visual Basic Challenge I
Critical Review
Visual Basic supports both While and Do While loops. The general syntax for a condition controlled loop is as follows:
While loop:
While keepGoing = "yes"
End While
Do While loop:
Do While keepGoing = "yes" Loop
Visual Basic will automatically end your While with an End While, and end your Do While with the Loop.
Count controlled loops are structured the same way, but you must declare a counter variable as an Integer as loops only count in whole increments. Additionally, do not forget to increment or decrement your counter variable.
Help Video: Double click the file to view video
Write either the Pseudocode OR Flowchart AND the Visual Basic Code
Design a program that will use your pingMe() module from Lab 4-4. There should be a condition controlled loop in Main to run the program multiple times if the user wants. Additionally, in the pingMe() module, there should be a count controlled loop that will ping your system after a count down of 5 (Reference p. 199, Decrementing). You may use while or do while loops. Your output may look as follows:
PASTE FLOWCHART OR PSEUDOCODE HERE
PASTE VISUAL BASIC CODE HERE
Lab 7.6 –While and Do While Loops: Visual Basic Challenge II
Based on Lab 7-3 and 7-4, code the flowchart/pseudocode by converting 2 to the power of a number from 2 to 8. You should have a main module that calls a module called displayPower() as many times as the user types yes (using a condition controlled loop). The displayPower() module should use a count controlled loop that raises the toPower variable to 2 to the power of the number, such as toPower = 2 ^ number. The output should look as:
Help Video: Double click the file to view video
PASTE VISUAL BASIC CODE HERE
You May Also Find These Documents Helpful
-
1. Why should you indent the statements in the body of a loop? You visually set them apart from the surrounding code.…
- 431 Words
- 2 Pages
Satisfactory Essays -
4. What is a recommended best practice when implementing a Remote Access Policy server user authentication service?…
- 1094 Words
- 5 Pages
Powerful Essays -
1. What are some common risks, threats, and vulnerabilities commonly found in the LAN-to-WAN Domain that must be mitigated through a layered security strategy?…
- 928 Words
- 3 Pages
Good Essays -
4. Why would you use a tool like DevManView while performing a computer forensic investigation?…
- 414 Words
- 2 Pages
Satisfactory Essays -
return -1 // if the while loop exit without retuning a value it means the value…
- 306 Words
- 2 Pages
Satisfactory Essays -
All data-input and data-display operations (cin and cout) should be done in the function main() test program.…
- 477 Words
- 2 Pages
Good Essays -
Comments are available in the code that will provide a full understanding of the program itself; however abstraction of the problem will be featured in this lab report. In the abstraction of this problem I had to reference…
- 590 Words
- 3 Pages
Good Essays -
Using loops one of the things what you will be able to use is the While loop. A While loop does what it sounds like. While the condition(s) are true do the same task. Loops have two parts first a condition that gets tested for true or false value and the second is a statement or set of statements that’s is repeated as long as the condition is true. Do while loops is the expression after the keywords Do While. It’s the condition that is tested to determine if the statements provided before the “Loop Statement” are executed. The Do While loop is evaluated the same as in the If-Else statement. The difference between the two is in how the expression is used. In the Do While Loop the…
- 885 Words
- 3 Pages
Good Essays -
A. Select “Routines” from the “VIEW” top menu keys 1. Cursor to the new subroutine 2. Select “Decl” (Declaration) from the bottom function keys 3. Select “Jump” key to switch to bottom…
- 1495 Words
- 6 Pages
Good Essays -
Main program is given below whereas subroutines aregiven after this program ends. inp = randn(1,502); p = [0.26 0.93 0.26]; snr = input('enter snr \n'); x=rand(10,30); for i=1:10 for j=1:30 if x(i,j)>0.5 x(i,j)=1; else x(i,j)=0; end end…
- 819 Words
- 4 Pages
Good Essays -
This chapter covers fundamentals of MATLAB programming. With lots of examples, this chapter offers the essence for beginners to get a handle on programming in MALTAB. Starting from introducing various MATLAB windows and on-line help facilities, the fundamentals of MATLAB programming including data types, statements and matrix representation are explained first followed by matrix manipulations, such as algebraic computation, logical and relationship expressions and data conversion. Then, flow charts in MATLAB programming is illustrated, including loop structures, conditional structures, switches and trial structures. MATLAB function programming and pseudo code processing are covered together with two-dimensional and three-dimensional graphics and visualization techniques. MATLAB graphical user interface (GUI) techniques are explained so that the readers will gain new GUI programming skills to design user-friendly interfaces. Finally, programming skills for delivering high speed, high efficiency codes are introduced with special emphasis on commonly used tips, vectorized programming…
- 2168 Words
- 10 Pages
Powerful Essays -
4.1 Three Structured Constructs All programs can be constructed using only the three basic constructs. It is the concept of structured programming that a program should be developed using only these three basic constructs. It is not wrong to use the GOTO structure but it is discouraged, because programs with the GOTO structure are often classified as unstructured. The three basic constructs are Sequence Selection Iteration/Repetition 4.1.1 Sequence Construct In a sequence construct, the way the statements are placed implies the order in which the computer will execute the statements. The computer will interpret the statements in a left to right, top to bottom fashion. Consider the following Calculate-Average Module DO Get Two Numbers Calculate Sum Determine Average Print Average ENDDO The sequence for the above module is straightforward. You get the two numbers, calculate the sum, determine the average and print out the result. If the statements are not in that sequence, the result obtained will not be the answer intended Figure 4-1 Simple sequence The Calculate Average module is not a separate module away from the rest of the four modules below it. But it actually contains all the four modules. Each process (a rectangle) represents a component with their function/action stated in the function list. If the component does not contain or form other constructs (i.e. other sequences, selections or iterations), it is regarded as an elementary component. The diagram should be interpreted in a top-down, left to right manner. The Calculate-Average Module has four elementary components. Each time it is activated, it will activate the Input Number module, Calculate Sum module, Determine Average module and the Print Result module in that order. Consider another example Figure 4-2 Complex sequence The sequence of activation for the above will be A, B, E, F, C, D, G, I, J, H 4.1.2 Selection Construct The selection construct consists of condition(s) and one or…
- 2133 Words
- 5 Pages
Better Essays -
The loop function follows next and includes the code to be executed continuously – reading inputs, triggering outputs, etc. This function is the core of all Arduino programs and does the bulk of the work.…
- 1883 Words
- 8 Pages
Better Essays -
Limitation comes in the form of whether you and others can read and follow your code…
- 606 Words
- 3 Pages
Satisfactory Essays -
Programs to be implemented in the lab: Sr. No 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. Topics on which the programs have to be done in lab If – else Reverse an integer & find sum of digits of an integer using do-while Factorial using do-while Drawing a pyramid using for Calculator using switch Function Recursive function Single dimensional array Matrix Multiplication using…
- 2751 Words
- 12 Pages
Powerful Essays