Beruflich Dokumente
Kultur Dokumente
while (boolean-expression)
{
statement1;
[...]
}
Loop
false false
Continuation (count < 100)?
Condition?
true true
Statement(s) System.out.println("Welcome to Java!");
(loop body) count++;
(A) (B)
5
animation
6
animation
7
animation
8
animation
9
animation
10
animation
11
animation
12
animation
13
animation
14
Trace while Loop
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0)
{
System.out.println("tick " + n);
n--;
}
}
}
15
Trace while Loop
16
// The target of a loop can be empty.
class NoBody {
public static void main(String args[]) {
int i, j;
i = 100; Midpoint is 150
j = 200;
// find midpoint between i and j
while(++i < --j); // no body in this loop
17
Program to Reveres of a number
1.Program to Check Palindrome using while loop
public class Main {
public static void main(String[] args) {
int num = 1431, reversedInteger = 0, remainder; 2. Check Armstrong Number for 3 digit number
// reversed integer is stored in variable
while( num != 0 )
{ 3. Count Number of Digits in an Integer using while loop
remainder = num % 10;
reversedInteger = reversedInteger * 10 + remainder;
num /= 10; 4. Program to convert binary number to decimal
}
System.out.println("Reverse no is:"+reversedInteger);
} 5. Program to convert decimal number to binary
}
Output:
Reverse no is:1341 7.Find LCM of two numbers
18
do while loops
The do-while loop is identical to the while loop except that the
test is evaluated at the end of the loop.
do
{
statement1;
[...]
} while (boolean-expression);
2.After the execution of the statements, and update of the variable value, the condition is checked
for true or false value. If it is evaluated to true, next iteration of loop starts.
3.When the condition becomes false, the loop terminates which marks the end of its life cycle.
4.It is important to note that the do-while loop will execute its statements atleast once before any
condition is checked, and therefore is an example of exit control loop.
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String args[]) {
int n = 10;
do { do {
System.out.println("tick " + n); System.out.println("tick " + n);
n--; } while(--n > 0);
} while(n > 0);
}
}
class dowhileloopDemo
{
public static void main(String args[])
{
int x = 21;
do
{
// The line will be printed even
// if the condition is false
System.out.println("Value of x:" + x);
x++;
}
while (x < 20);
}
}
import java.util.Scanner;
class Sum {
public static void main(String[] args) {
Double number, sum = 0.0; 1.Write a program using do-while loop to print
Scanner input = new Scanner(System.in); Numbers and their cubes up to 10.
2.
do {
System.out.print("Enter a number: ");
number = input.nextDouble();
sum += number;
} while (number != 0.0);
System.out.print(2);
}
while(false);
}
while(false);
}
}
public class Main
{
++i;
}
while(i<3);
}
}
// Using a do-while to process a menu selection
class Menu {
public static void main(String args[])
char choice;
do {
System.out.println("Help on: ");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\n");
System.out.println("Choose one:");
choice = (char) System.in.read();
} while( choice < '1' || choice > '5');
System.out.println("\n");
switch(choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" //...");
System.out.println("}"); Help on:
break; 1. if
case '3': 2. switch
System.out.println("The while:\n"); 3. while
System.out.println("while(condition) statement;"); 4. do-while
break; 5. for
case '4': Choose one:
System.out.println("The do-while:\n"); 4
System.out.println("do {"); The do-while:
System.out.println(" statement;"); do {
System.out.println("} while (condition);"); statement;
break; } while (condition);
case '5':
System.out.println("The for:\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
Loops - Examples
int x = 0; int x = 0;
while(x<10) while(x<10)
{ {
System.out.println(x++); System.out.println(++x);
} }
int x = 0;
do
{
System.out.println(x);
x = x+1;
} while(x<10);
for loop
1.Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop. An
already declared variable can be used or a variable can be declared, local to loop only.
2.Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean value. It is
also an Entry Control Loop as the condition is checked prior to the execution of the loop statements.
3.Statement execution: Once the condition is evaluated to true, the statements in the loop body are
executed.
4.Increment/ Decrement: It is used for updating the variable for next iteration.
5.Loop termination:When the condition becomes false, the loop terminates marking the end of its life
cycle.
for loop
The syntax of the for loop makes the 4 parts of the loop
explicit.
Initial-Action i=0
Loop
false false
Continuation (i < 100)?
Condition?
true true
Statement(s) System.out.println(
(loop body) "Welcome to Java");
Action-After-Each-Iteration i++
(A) (B)
32
animation
33
animation
34
animation
35
animation
36
animation
37
animation
38
animation
39
animation
40
animation
41
animation
42
class forLoopDemo
{
public static void main(String args[])
{
// for loop begins when x=2
// and runs till x <=4
for (int x = 2; x <= 4; x++)
System.out.println("Value of x:" + x);
}
}
Value of x:2
Value of x:3
Value of x:4
// Using the comma.
class Comma {
public static void main(String args[]) {
int a, b;
for(a=1, b=4; a<b; a++, b--)
{
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}
Output
a=1
b=4
a=2
b=3
Here is another interesting for loop variation. Either the initialization or
the iteration expression or both may be absent, as in this next program:
// Parts of the for loop can be empty.
class ForVar {
public static void main(String args[]) {
int i;
boolean done = false;
i = 0;
for( ; !done; )
{
System.out.println("i is " + i);
if(i == 10)
done = true;
i++;
}
}
}
class ForLoopExample {
public static void main(String args[]){
for(int i=10; i>1; i--)
{
System.out.println("The value of i is: "+i);
}
}
}
Output:
Sum of first 10 natural numbers is: 55
public class JavaExample {
Output:
Factorial of 5 is: 120
public class JavaExample {
If the condition is True then statements inside the For loop will be executed. It means, compiler will enter into
second For loop. Goto Step 2
If the condition is False then, compiler will exit from For Loop
Step 2: Compiler will check for the condition inside the second for loop.
If the condition is True then statements inside the second For loop will be executed. It means, it will execute
from Statement 1 to N.
If the condition is False then, compiler will exit from second For Loop
Step 3: Once it exit from second for loop, compiler will check for the condition inside the for loop (repeating
Step 1 )
Nested Loops
Java allows loops to be nested. That is, one loop may be inside another. For example,
here is a program that nests for loops:
// Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++)
{
for(j=i; j<10; j++)
System.out.print("."); The output produced by this program is shown here:
System.out.println(); ..........
} .........
} ........
} .......
......
.....
....
...
..
.
class NestedLoop {
public static void main(String[] args) {
Outer loop iteration 1
i = 1; j = 1
int i = 1;
i = 1; j = 2
Outer loop iteration 2
while (i <= 5) {
i = 2; j = 1
i = 2; j = 2
System.out.println("Outer loop iteration " + i);
Outer loop iteration 3
i = 3; j = 1
for (int j = 1; j <= 2; ++j) {
i = 3; j = 2
System.out.println("i = " + i + "; j = " + j);
Outer loop iteration 4
}
i = 4; j = 1
i = 4; j = 2
++i;
Outer loop iteration 5
}
i = 5; j = 1
}
i = 5; j = 2
}
class Pattern {
public static void main(String[] args) {
int rows = 5;
int rows = 5;
System.out.println(); 54321
} 4321
sc.close(); 321
} 21
} 1
import java.util.Scanner;
public class Pattern4
{
public static void main(String[] args) System.out.println();
{ }
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: "); }
}
int rows = sc.nextInt();
}
}
}
class Test {
public static void main(String[] args) {
1
for (int i = 1; i <= 10; ++i) { 2
if (i == 5) { 3
break; 4
}
System.out.println(i);
}
}
}
import java.util.Scanner;
class UserInputSum {
public static void main(String[] args) {
sum += number;
}
System.out.println("Sum = " + sum);
}
}
class Test {
public static void main(String[] args) {
1
2
3
4
9
10
import java.util.Scanner;
class AssignmentOperator {
public static void main(String[] args) {
sum += number;
}
System.out.println("Sum = " + sum);
}
}
For nested loop
https://www.programiz.com/java-programming/continue-statement
https://www.programiz.com/java-programming/break-statement
http://www.beginwithjava.com/java/loops/break-continue-statement.html