{% highlight java %} /** * --------------------------------------------------------------------------- * File name: ForLoopExercises.java * Project name: Winter Study Guide * --------------------------------------------------------------------------- * Author’s name and email: Michael Schneider, schneidm@etsu.edu * Course-Section: All * Creation Date: 1/5/15 * Last modified: Schneider 1/5/15 schneidm@etsu.edu * --------------------------------------------------------------------------- */ import java.util.Scanner; /** * Class Name: ForLoopExercises
* Class Purpose: To give example exercises on using for loops to create repetitive code *
* *
* Date created: 1/5/15
* Last modified: Schneider 1/5/15 schneidm@etsu.edu * @author Michael Schneider */ public class ForLoopExercises{ public static void main(String[] args){ /******************************************************* * * Examples * ********************************************************/ //Create a for loop that displays 0 to 15 to the screen //The counter (i) is initialized to 0 // //The conditional statement (i < 16) states that the for loop will end when // the counter is no longer less than 16 (i.e. when the counter reaches 16) // //The incrementer (i++) states that the counter will only increase by values of 1 // for(int i = 0; i < 16; i++) { System.out.print(" " + i); } //Create a for loop that only displays even numbers between 0 and 30 //The counter is initialized at 0, the first number in our range // //The conditional statement (i <=30) states that the loop will end when // the counter reaches a value greater than 30 // //The incrementer (i+=2) states that the counter will increase by values of 2, // since the counter is initialized as an even number, only the even numbers // in the range will be displayed. // for(int i = 0; i <= 30; i+=2) { System.out.println(" " + i); } //Rewrite the previous for loop to use a conditional statement to check // if the counter is even. //The only change made from the previous for loop is in the incrementer. // The incrementer now only increases the counter by 1. This moves the // logic for checking if the counter is even into the for loop itself. // for(int i = 0; i <=30; i++) { //Remember mod (%), returns the remainder from a division. If a number // is divided by 2 and the remainder is 0, we have an even number. if(i %2 == 0) { System.out.println(" " + i); } } /********************************** * *Counting Exercises * ***********************************/ //Problem 1 //Create a for loop that counts from 1 to 10 //Problem 2 //Create a for loop that counts from 3 to 15 //Problem 3 //Create a for loop that counts from 0 to 50 by counting by 5 //Problem 4 //Create a for loop that counts from 10 to 1 //Problem 5 //Create a for loop that counts from 50 to 0 by counting down by 5 //Problem 6 //Create a for loop that count by multiples of 3, up to 90 /********************************** * *Repetitive Exercises * ***********************************/ //Problem 1 //Sum all the numbers from 1 to 50 //Problem 2 //Display all odd numbers from 0 to 16 //Problem 3 //Take 10 numbers, find their sum //Problem 4 //Take 10 numbers from the user, display a message if // x < 100 : "That's a tiny number" // 100 < x < 200 : "That number is a bit high.." //x > 200 : "That number is crazy!" //Problem 5 //Take 10 number pairs and provide a menu for the user // 1. Find the sum // 2. Find the difference // 3. Find the product // 4. Find the quotient //Process the user's choice with if/else statements //Problem 6: Rewrite Problem 5 using a switch statement to process the user's // response. } } {% endhighlight %}