To gain experience with
Consider the following code
// Print a table of squares public class PrintSquares { public static void main(String [] args) { int i = 1; while(i <= 10) { System.out.print(i); System.out.print(" squared is "); System.out.println(i * i); i = i + 1; } } } |
Change the code so that it
prints out the squares all the way up to 100.
prints the squares from 90 to 100.
prints out the square of every even number between 1 and 10, i.e. the numbers 2, 4, 6 etc.
What happens if you put a semicolon after the condition in the while loop? e.g. instead of while(i <= 10) you had while(i <= 10);
Adapt the PrintSquares program so that it prints out the squares of the numbers from 10 down to 1. Call the program PrintBackwards. Here is a sample run of the program.
10 squared is 100 9 squared is 81 8 squared is 64 7 squared is 49 6 squared is 36 5 squared is 25 4 squared is 16 3 squared is 9 2 squared is 4 1 squared is 1 |
Adapt the PrintSquares program so that it first asks the user to enter a number n and then prints the squares of the numbers from one to n. A sample run would look like
$ java PrintSquares Please enter a number: 12 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 6 squared is 36 7 squared is 49 8 squared is 64 9 squared is 81 10 squared is 100 11 squared is 121 12 squared is 144 $ |
In this case, the user entered 12, and so the program printed all the squares up to (and including) 12.
Hint: You will have to use Console.readInt() to read in an integer.
Here is a program that uses a while loop to create a String.
// Program to create a String of at least 4 'x's public class PrintString { public static void main(String [] args) { String data; // Put one "x" in data data = "x"; // Keep going as long as the length is less than 4 while(data.length() < 4) { // Plonk an "x" onto the end of the string data = data + "x"; } System.out.println(data); } } |
What does this program print? How does this program work? Change the program so it creates a String of 10 dashes ("----------").
A variable that counts the iterations of a loop is called a loop index. In the program PrintSquares, i serves as an index. This type of loop is frequently written using the for statement.
for (loopIndex = startValue; condition; indexIncrement)
Write a program with two for loops which will print the following working hours, 9-5
Hour 9 am 10 am 11 am 12 am 1 pm 2 pm 3 pm 4 pm 5 pm
Here is another example of a for loop. Note it is written in the C programming language so it looks a little different.
One loop type might be better suited to a purpose than another. The following usages are typical.
for | Known number of iterations |
while | Unknown number of iterations |
do while | At least one iteration |
Change the program PrintSquares (see above) so that it uses a for loop.
Is this an improvement? Explain your answer.
While loops are often used to read in a sequence of numbers. You are to write a program that will read in numbers until a minus nine is entered. The program should then print out how many numbers were entered and also how many numbers were precisely divisible by three. Here is a sample run of the program.
$ java CountThree Enter a sequence of numbers (-9 to end): 3 8 7 1 21 -9 There were 5 numbers. 2 numbers were divisible by three. $ |
Note that the minus nine is not counted as being one of the numbers. It merely indicates to the program that there are no more numbers. Only 3 and 21 were divisible by three, so the program states that 2 numbers were divisible by three.
In the next sample run, only three numbers are entered before the minus nine and the program again correctly counts the numbers and counts how many numbers were divisible by three.
$ java CountThree Enter a sequence of numbers (-9 to end): 3 4 2 -9 There were 3 numbers. 1 numbers were divisible by three. $ |
Here is a hint.
1) What is the output of each of the following loops?
2) In each example, leave the loop as it is and change only the expression inside System.out.print so that the program will display "1 2 3 4 5 ".
for(i = 0 ; i < 5 ; i++) { System.out.print(i + " "); }
int decimals = 1; while(decimals < 100000) { System.out.print(decimals + " "); decimals *= 10; }
Here's a hint
int i = 5; do { System.out.print(i + " "); i--; } while( i > 0 );
Write a RobotWorld program that uses nested for loops to creates a rectangle of beepers.
Here's a hint.
You can access the individual characters in a string by using the charAt method of the String class. The charAt method returns a character of type char. Note that char constants are enclosed in single quotes such as 'e'.
Many word-processors can check spelling. One of the corrections applied is to swap 'e' for 'i' if 'i' occurs immediately before 'e' and immediately after 'c'. For example, concieve is corrected to conceive. Write a program that carries out this correction. That is, your program should read a string and print the corrected string (or the original string if no correction needed to be applied.)
Here's a hint.