0% found this document useful (0 votes)
59 views3 pages

Lesson 7: Loops Practice Guide

PLTW worksheet

Uploaded by

italyrox21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Lesson 7: Loops Practice Guide

PLTW worksheet

Uploaded by

italyrox21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lesson 7: Loops Practice

Please complete each level to the best of your ability and attach your completed code below

Level 1 - While Loop Practice


This program prints the numbers 0-9 to the console.
● Run the program to see how it works. The speed slider has been made to run more
slowly so you can watch how it works.
● Edit the program to print the numbers 0-99. You can turn the speed slider all the way
up.
● while(counter < 100);
● [Link](counter);

Level 2 - While Loop Practice


This program prints the word "Hello" to the console 10 times.
● Run the program to see how it works. The speed slider has been made to run more
slowly so you can watch how it works.
● Edit the program to print the word "Hello" 100 times. You can turn the speed slider
all the way up.
● while(counter < 100){
● [Link]("Hello");

Level 3 - While Loop Practice


This program simulates flipping 10 coins. On each loop it appends another random 0 or 1 to a
list.
You'll need to add code to simulate rolling 10 dice. That means appending 10 random numbers
between 1 and 6 to a list.
● Run the program to see how it works.
● Add code in the spaces indicated to generate 10 random numbers between 1 and 6.
This will simulate rolling 10 dice.
● Use the variables and lists provided for you. Feel free to copy the patterns from the
code to roll the dice.
● var counter2 = 0;
● var diceRolls = [];
● while((counter2 < 10)){
● appendItem(diceRolls, randomNumber(1,6));
● counter2++;

Level 4 - Putting It Together


● var counter = promptNum("Enter a number");
● while((counter > 0)){
● [Link](counter);
● counter = counter-1;}
● [Link]("Blastoff!");

Level 5 - For Loop Practice


● Read the program and run it to see how it works
● Change the program so that it prints the numbers 0-99. You can turn the speed slider all
the way up.
● var num = 0;
● while ((num < 100)) {
● [Link](num);
● num++;

Level 6 - For Loop Practice


● Run the program and read the code to understand how it works.
● Change the program so that it displays the even numbers between 0 and 100.
● for(var i = 0; i < 101; i = i + 2){

Level 7 - For Loop Practice


● Run the program and read the code to understand how it works.
● Update the program to fill the third list of random coin flips. You don't need to add
another for-loop
● appendItem(flips3, randomNumber(0,1));

Level 8 - Loops and Screen Elements


● Read the code carefully to understand how the i variable is being used inside the
loop.
● Update this program so that when you click the button it randomly resizes each
word
● setProperty("label" + i, "font-size", randomNumber(1, 50));

Level 9 - Loops and Screen Elements


This program is supposed to roll 20 dice but it has several things that need to be improved.

● Make it roll all 20 dice


● Add code so that it calculates the number of even rolls
● Update the code so only even rolls get a white border
● onEvent("rollButton", "click", function( ) {
● var evens = 0;
● var odds = 0;
● for(var i = 0; i < 20; i++){
● var roll = randomNumber(1,6);
● setProperty("dice"+i,"text",roll);
● setProperty("dice"+i,"border-width",1);
● setProperty("dice"+i,"border-color","black");

● if((roll == 1) || (roll == 3) || (roll == 5)){
● odds++;
● } else {
● evens++;
● setProperty("dice"+i,"border-color","white");
● setProperty("informationLabel","text","Total Odds: " + odds + "\nTotal Evens: " +
evens);

Common questions

Powered by AI

Using loops when dealing with graphical user interface elements, as demonstrated in Levels 8 and 9, provides several benefits. Firstly, loops allow for systematic iteration over multiple user interface elements, enabling comprehensive updates, such as random resizing of words or altering dice display properties, swiftly. Secondly, loops enhance readability and reduce redundancy in code by eliminating the need for repeated blocks of similar instructions, thus streamlining the handling of dynamic elements on GUIs. Lastly, loops facilitate rapid adjustments based on criteria, such as changing visual properties for even vs. odd dice rolls, showcasing their utility in enhancing interactive and responsive interface behavior .

The speed slider plays a crucial role in understanding loop execution by allowing variable pacing of program operation, which aids in visualizing each step and comprehending loop dynamics. By slowing down execution, learners can observe precisely how iterations progress and identify logical or structural issues. This concept can be expanded into debugging techniques by incorporating step-through and break-point functionalities, enabling developers to pause execution at specific lines, examine variable states, and trace the exact flow of control, thus enhancing the debugging process and contributing to a more thorough understanding of program behavior .

The use of while loops in levels 1 and 2 illustrates a fundamental pattern of program control in iterative statements – initialization, condition check, and update. In level 1, the loop starts with an initialized counter at 0 and checks if it is less than 100 before executing the loop body to print numbers 0-99. Similarly, in level 2, the counter initializes at 0 and iterates until it reaches 100, printing 'Hello' each time. Both loops showcase how control flows based on dynamic condition evaluation and successive updates of the counter variable .

The problem-solving approach differs between simulating coin flips and dice rolls primarily in the range of the random numbers generated and the complexity of the loop logic used. In simulating coin flips, the task involves generating simple random binary outputs (0 or 1) for each iteration. Conversely, simulating dice rolls requires generating random numbers within a broader range (1 to 6), necessitating additional logic to handle this range's increased complexity. While both simulations use loops to repeatedly add random values to a list, the dice roll simulation demands more intricate random number generation .

Exercises involving random number generation and iteration could introduce statistical analysis by incorporating elements like calculating means, variances, or distributions. For instance, beyond collecting random numbers, the program could tally frequencies to form a probability distribution or compute running averages to track convergence in simulations. Extending these exercises with functions that compute statistical metrics would offer insights into the randomness's structure and variability. Further modifications could involve visualizing results via histograms or plots to provide graphical interpretations of statistical properties, thus enriching the pedagogical context with applied statistical analysis .

Conditional statements integrate into iteration to enhance decision-making capabilities by allowing specific actions based on values evaluated during each loop cycle. For instance, in Level 9, a loop iterates over dice rolls and uses conditional logic to determine if a roll is even. If true, the program assigns a white border to the output for that particular dice. This integration provides dynamic control within iterative sequences, enabling programs to respond to diverse data-driven conditions and produce differing outcomes efficiently, thus improving programmatic decision-making .

In Level 6, a for loop is leveraged to modify an existing program to print even numbers between 0 and 100. This is done by initializing the loop variable at 0, setting the condition to run while less than or equal to 100, and incrementing by 2 each iteration. This ensures that only even numbers are considered on each iteration, demonstrating how tailored modification of the loop's initialization, condition, and update expressions allows for meeting specific programming requirements efficiently .

Lists are utilized in these programming exercises as flexible data structures for storing sequences of elements and facilitating data processing tasks. In simulating coin flips or dice rolls, lists are employed to collect multiple outputs from a loop iteration, allowing aggregation and easy access to rolled values. The capacity to dynamically add elements during iteration showcases lists' utility in handling expandable data sets. Furthermore, lists support operations such as appending, accessing, and traversing, which are essential for executing batch processing tasks commonly required in programmatic data management .

Yes, the random number generation logic from these exercises can be applied to complex simulations in various fields such as statistical analysis, gaming, and machine learning. In statistical simulations, random numbers serve as fundamental components for Monte Carlo methods, helping model probabilities and uncertainties. In gaming, similar mechanics allow for unpredictability and varied outcomes, enhancing player engagement. In machine learning, randomization underlies algorithms like stochastic gradient descent, aiding in exploring and optimizing solutions through randomized sampling. Thus, the foundational logic of generating random numbers forms a cornerstone of more intricate and diverse simulation frameworks .

The strategies for updating program output based on user interactions primarily involve event handling mechanisms and dynamic property manipulations. GUI-based tasks leverage event-driven programming models, as seen in Level 9 where button clicks trigger the rolling of dice and subsequent updates to display properties. The use of loops and conditionals within event handlers facilitates responsive interface updating based on user actions, ensuring real-time feedback and interaction. This approach allows programs to adapt outputs dynamically to user inputs, enhancing user experience through swift and relevant visual updates .

You might also like