Module 5 Part Three
Module 5 Part Three
Infinite loop
To make a while statement infinite loop, you need to use the boolean literal true as the
condition-expression.
while (true)
System.out.println ("This is an infinite loop");
Initialization
while (condition-expression)
{
Statement
Expression-list
}
int i = 1;
while (i <= 10)
{
System.out.println(i);
i++;
}
The above code can also be rewritten as
int i = 0;
while (++i <= 10)
{
System.out.println(i);
}
Or
int i = 1;
while (i <= 10)
{
System.out.println(i++);
}
DTTH Page 1
Java Fundamentals
DTTH Page 2
Java Fundamentals
while(counter<=7)
{
c=a+b;
System.out.print(" " +c);
a=b;
b=c;
counter++;
}
}
}
}catch(IOException ie){
System.out.println(ie.getMessage());
}
System.out.println("You tried "+ count + " times.");
}
}
DTTH Page 3
Java Fundamentals
Example 2
import java.io.*;
class WhileUnknownTimesChar
{
public static void main(String[] args)
{
int count=0;
try
{
//int count = 0;
InputStreamReader ir= new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Will you start?[Y/N] : ");
String str = br.readLine();
char code = str.charAt(0);
code = Character.toUpperCase(code);
//code = Character.toLowerCase(code);
//while(code=='y')
while(code=='Y')
{
System.out.println("Processing in Loop");
count++;
System.out.println("Will you continue?[Y/N]
:");
str = br.readLine();
code = str.charAt(0);
code = Character.toUpperCase(code);
}
}catch(IOException ie){
System.out.println(ie.getMessage());
}
System.out.println("You tried "+ count + " times.");
}
}
do
Statement
while (condition-expression);
DTTH Page 4
Java Fundamentals
11
21 22
31 32 33
If you want to exit from the outer for-loop statement from inside the inner for-loop statement,
you have to use a labeled break statement.
A label in Java is any valid Java identifier followed by a colon.
The following are some valid labels in Java:
label1:
alabel:
Outer:
Hello:
IamALabel:
DTTH Page 5
Java Fundamentals
Note that the outer label appears just before the outer for-loop statement.
A labeled statement can be used not only inside switch, for-loop, while-loop, and do-while
statements.
Rather it can be used with any type of a block statement.
blockLabel:
{
int i = 5;
//int i = 10;
if (i == 5)
{
break blockLabel; // Exits the block
}
if (i == 10)
{
System.out.println("i is not five");
}
}
The label used with the break statement must be the label for the block in which that labeled
break statement is used.
lab1:
{
int i = 10;
if (i == 10)
break lab1; // Ok. lab1 can be used here
}
lab2:
{
int i = 10;
if (i == 10)
break lab1; //compile error
}
DTTH Page 6
Java Fundamentals
When a continue statement is executed inside a for loop, the rest of the statements in the body of the
loop are skipped and the expressions in the expression-list are executed.
Example - print all odd integers between 1 and 10 using a for-loop statement,
for (int i = 1; i < 10; i += 2)
{
System.out.println(i);
}
for(int i=1;i<10;i++)
{
if(i%2==0)
{
continue;
}
System.out.println(i);
}
Example - print all odd integers between 1 and 10, using a continue statement inside a while
loop
int i =1;
while (i<10)
{
if(i%2==0)
{
i++;
continue;
}
System.out.println(i);
i++;
}
The main difference in using a continue statement inside a for loop and a while loop is the
place where the control is transferred.
Inside a for loop, control is transferred to the expression-list, and in a while loop, the control
is transferred to the condition-expression.
This is why a for-loop statement cannot always be converted to a while-loop statement
without modifying some logic.
An unlabeled continue statement always continues the innermost for loop, while loop, and do-
while loop.
If you are using nested loop statements, you need to use a labeled continue statement to
continue in the outer loop.
DTTH Page 7
Java Fundamentals
if (j==2)
{
continue; // Contiue the inner for loop
}
System.out.println("j=" + j +" Inner Loop Last
Statement");
}
System.out.println("i=" + i + " Outer Loop Last Statement");
}
Output
i=1 Outer Loop First Statement
j=1 Inner Loop First Statement
j=1 Inner Loop Last Statement
j=2 Inner Loop First Statement
j=3 Inner Loop First Statement
j=3 Inner Loop Last Statement
i=1 Outer Loop Last Statement
i=2 Outer Loop First Statement
j=1 Inner Loop First Statement
j=1 Inner Loop Last Statement
j=2 Inner Loop First Statement
j=3 Inner Loop First Statement
j=3 Inner Loop Last Statement
i=2 Outer Loop Last Statement
i=3 Outer Loop First Statement
j=1 Inner Loop First Statement
j=1 Inner Loop Last Statement
j=2 Inner Loop First Statement
j=3 Inner Loop First Statement
j=3 Inner Loop Last Statement
i=3 Outer Loop Last Statement
if (i == j)
{
System.out.println();
continue outer; // Contiue outer for loop
}
}
}
DTTH Page 8
Java Fundamentals
PrimeFactor.java
import java.io.*;
public class PrimeFactor
{
public static void main(String[] args)
{
try
{
InputStreamReader ir = new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter a number: ");
String str = br.readLine();
int num = Integer.parseInt(str);
int i = 2;
while ( i < num)
{
if ( num % i == 0)
{
System.out.print(i + "*");
num = num / i;
i=2;
continue;
}
i++;
}
System.out.print(" " + num);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Output
Enter a number:
6
2* 3
DTTH Page 9