hts.StevenWood.com

[ Last Page ]

for Loops & Questions

Structure:
initialization
The initial value for the variable that controls the loop.
Note this can be done in the loop ( as in the examples below, or before the loop)
termination
Continue while this condition is true. When the condition is false, the loop terminates
increment
How the control variable changes with each repetition of the loop. It usually increases OR decreases in an orderly manner.
Print the numbers from -5 to 5.

  i++ means add one to the variable i each time you loop

Print the even number from 0 to 10.

  j+= 2 means to take the variable j and add 2 to it each time you loop
Print the number from 10 to 20 and their squares.

 The \t is a special printing character that indicates to "tab over"
  Note that the titles must go before the loop so that they only print once.

 
Try these ones
  1. Make a Fahrenheit to Celsius conversion table, in columns, from -40 to 100 degrees Fahrenheit in 10-degree increments. Use C = (F - 32) * 5 / 9
  2. Print the odd numbers from 100 down to 1.
  3. Create a table of values for the equation y = 2.135 x + 4.53 for values of x from -10 to 10 in steps of 0.5.
  4. Create a table of values for the equation in the the question above. Ask the user for the starting value and for the ending value to use for the table.
  5. Create a table of values for the equation y = 3(x3 - 1.5x2 + 4x - 1) for values of x from 2 to -4, changing by 1 each time.
  6. Add up the numbers from 1 to 100. Display the answer.
  7. Create a program that asks for a number and then uses a for loop to calculate the sum of the numbers from 1 to the given number.
  8. The square of a number can be found by adding successive odd integers. For example, to find 42, add the first four odd integers:

    42 = 1 + 3 + 5 + 7 = 16

    To find 62, add the first six odd integers:

    62 = 1 + 3 + 5 + 7 + 9 + 11 = 36

    Create a program that asks for a number and then uses a for loop to calculate its square using this method.
 
[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+