| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Programming Exercises - Loops

Page history last edited by Steve Sweeney 5 years, 2 months ago

These exercises should work in any programming language (e.g., Turing, Java).  Exercises in blue will generally require a graphical environment such as Scratch or Alice.  Possible extensions to each exercise are added below, generally in the order of difficulty, using letters such as (a), (b), (c), etc.

 

A.  Simple Counted Loops (do these exercises in any order)

 

A.1  Ask the user their name and output the name 5 times.

(a) Instead of 5 times, allow the user to specify the number of outputs.

A.2  Output the numbers from 1 to 10 using a counted loop. (Scratch Solution)

(a) Allow the user to input the upper value (i.e., count from 1 to N). (Scratch Solution)

(b) Allow the user to input the upper and lower value (i.e., count from M to N). (Scratch Solution)

(c) Warn the user if they input numbers that don't make sense (e.g., the upper value is smaller than the lower value).

(d) If the user enters the numbers backwards (i.e., a higher number first, then a lower number), count DOWN from the higher to the lower number.

 

A.3  Have a character or object move back and forth across the screen N times.

(a) Let the user input a value to determine how many paces are performed.

 

A.4  Have two characters or objects pass something between them (e.g., two soccer players passing a ball).  They should pass it back and forth N times. (Scratch Solution)

(a) Let the user specify the number of passes.

 

A.5  Find or create something that looks like a fan.  Have the fan rotate N times.

(a) Let the user specify the number of passes.

(b) Add some buttons that turn the fan on/off.

(c) Add more buttons that control the speed of the fan.

 

A.6 Ask the user to enter 5 numbers, one at a time, and add them together.  This is called a running total.  Once the user is done, tell them the total.

(a) Modify the program to tell the user the total after each number is added.

 

B.  Complex & Nested Counted Loops (do these exercises in any order)

 

B.1  Output the times tables (from 1 to 12) for a number of your choice.

(a) Allow the user to pick the number;

(b) Allow the user to specify the start and end to the table (e.g., 4 to 15);

(c) Allow the user to specify the step size (e.g., by 3 is 4, 7, 10, 13).

 

B.2  Output the numbers from 1 to 10 both numerically (i.e., “1”, “2”, etc.) and graphically (using the character of your choice).  For example, the line for 3 might read “3    ***”, while 4 would be “4   ****”.

 

B.3  Ask the user to enter 5 integer values between 1 and 50.  Create a simple bar graph (histogram) using the character of your choice.

(a) Let the user specify the number of values.

(b) Let the user enter any values they want, and scale your graph so it fits on the screen.

 

 

B.4   Create a program that simulates the Octopus Ride, as shown in the image.  The entire structure should rotate N times, and each of the smaller structures (at the end of each arm) should rotate at a different rate, independent of the larger structure.

(a) Let the user specify how many times the main structure rotates (i.e., how long the ride lasts).

(b) Have each of the smaller structures rotate at different rates in different directions.

 

B.5  Find or create a clock with a minute hand and hour hand.  When the minute hand reaches 12, the hour hand should advance by 1 hour.

(a)  Have the hour hand move smoothly, rather than only on the hour.

(b)  Add a second hand.

 

B.6  Prime numbers are divisible only by one and themselves.  For example, 2, 3, 5, and 7 are prime, but 9 is not (because 9 is divisible by 1, 3, and 9).  Prime numbers are very important for cryptography, which allows computers to secure data.  As a new programmer, an valuable concept is identifying prime numbers.  Create a program which asks the user for a number and then tells them whether or not it is prime.  Note:  You may need to do additional research on this topic, and specifically the mathematical operator known as "modulo".

 

Simple Conditional Loops

 

C.1 (a) Write a program that keeps asking the user for their name until they enter "Tim".  Otherwise, keep saying "You are not Tim!" and ask for their name.  (turing solution)

(b) Allow your name as a valid input as well.

(c) Keep track of how many times the user fails to enter the correct name, and report it when they finally get it right.

 

C.2 (a) Create a program that checks user name and password. Start with a single user. (turing solution)

(b) Add a security feature where there are a maximum number of failed attempts before the system locks out the login process.

(c) More security - whenever there is a valid user name with an incorrect password, track the number of failed attempts.  Upon a successful login, report the number of failed attempt on their account to the user.  You might want to only try this feature for a single user.

 

Complex Conditional Loops

 

 

Comments (0)

You don't have permission to comment on this page.