Monthly Archives: December 2013

5 Minutes of Java (9. Write a For loop for a given output)

One common question we often encounter is to write a for loop for a given output.

Output example:

number 1
number 2
number 3
number 4
number 5
number 6

We will follow a 5-step process to come out with a For loop.

Step 1: write a program without using while/for loop

System.out.println(“number 1”);
System.out.println(“number 2”);
System.out.println(“number 3”);
System.out.println(“number 4”);
System.out.println(“number 5”);
System.out.println(“number 6”);

Step 2: Identify the constant part and the variable part

constant and variable

System.out.println(“number 1”);
System.out.println(“number 2”);
System.out.println(“number 3”);
System.out.println(“number 4”);
System.out.println(“number 5”);
System.out.println(“number 6”);

 

Step 3:  Use a variable to replace the variable part

System.out.println("number "+n);

Step 4: Identify the starting number, the ending number and the step/update

  • start: 1
  • end: 6
  • step/update: +1 (add 1 every time)

Step 5:  Construct a for loop

for(int n=1; n <= 6; n++)
{
   System.out.println(“number “+n);
}

Let’s recap the 5-step process again:

  • Write a program without using for loop
  • Identify the constant part and the variable part
  • Use a variable to replace the variable part
  • Identify the starting number, the ending number and the step/update
  • Construct a for loop

5 Minutes of Java (8. For loop)

For Loop Structure:

for(Initialization; Conditional Test; Update)
{
   Statements //inside the loop body
}
//statements after for loop

meaning:

  • The initialization statement is executed before the loop starts, and it is executed only once. It is commonly used to initialize the loop variable.
  • Conditional test is evaluated, if it is true, loop body will be executed. If it is false, the for loop is terminated, and the statement after the for loop will be run.
  • Update statement is executed after the loop body is executed. It is usually used to increment or decrement the loop variable.

Example:

for (int i = 0; i < 8; i++) {
   System.out.println(i);
}
System.out.println("done");

Output:

0
1
2
3
4
5
6
7
done

Common use of for loop:

  • When the number of iterations is known from the outset.

 

5 Minutes of Java (7. While loop)

While Loop Structure:

While (Conditional Test)
{
   Statements; //inside the loop block/body
}

Meaning:  While conditional test result is true, keep looping (statements inside the loop block/body will be executed/run)

Example:

int cupsOfRice = 0;
while(cupsOfRice<4)
{
 System.out.println("I am eating");
 cupsOfRice++; //cupOfRice = cupOfRice + 1;
}
System.out.println("I am full");

Output:

I am eating
I am eating
I am eating
I am eating
I am full

Common uses of While Loop:

  • When the number of iterations is not known from the outset.
  • When an infinite loop is enforced, until the condition becomes false.

Example:

int num = Helper.readInt("Enter a number between 1 to 10> ");
while (num < 1 || num > 10) {
   num = Helper.readInt("Invalid Numer, 1 to 10 please > ");
}
System.out.println("You entered " + num);

In this example, we are not sure how many times a user would enter a invalid number, so we use while loop. As long as the user enters a invalid number, the program will keep asking the user to enter a new number again.