Professional Documents
Culture Documents
Chapter 4
Chapter 4
int number=1;
while (number <10)
{
System.out.println(”number=”+number);
number=number+1;
}
Output
4
do-while statement
5
int number=1;
do
{
System.out.println("number="+number);
number=number+1;
}
while (number <10);
Output
6
Difference between while & do while
7
When Boolean expression is Even with same while condition
False while loop statements used in do-while, do-while loop
are not executed even once. statements are at least
executed once.
int n= 3; int n= 3;
while(n < 3) { do {
System.out.println(“n= “+ n); System.out.println(“n= " + n);
n=n+1; n=n+1;
} }
while(n < 3);
for loops
8
int x=3;
for (int i = 0; x == 3; i++) {
System.out.println(i);
}
//the condition x==3 is always true.
Jump Statements
18
class Myclass1
{ Output:
public static void main(String args[])
{ 1
int n = 1; 2
while (n<=15)
{
3
System.out.println(n); 4
n++; 5
if (n==7)
{ 6
break;
}
}
}
}
The continue statement
21
public static void main(String[] args) {
//for loop Output:
for(int i=1;i<=10;i++){ 1
if(i==5){ 2
//using continue statement 3
4
continue;//it will skip the rest statement
6
} 7
System.out.println(i); 8
} 9
} 10
}
The return statement
23
for(int i=1;i<=10;i++){
if(i==5){
return;
// it will terminate the loop
}
Output:
System.out.println(i); 1
} 2
3
} 4
}
The exit Statement
25
1. Loop
2. Infinite loop
3. Body
Knowledge Check
28
}
31
int a=3;
int b=0;
while (b<=10)
{
System.out.println(a*b);
b++;
}
}
33
Questions?!