-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx7Loop.java
More file actions
66 lines (56 loc) · 2.07 KB
/
Ex7Loop.java
File metadata and controls
66 lines (56 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package JavaEntry;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
public class Ex7Loop {
public static void main(String[] arg){
// while loop: run through a block of code as long as a specified condition is true:
short no = 0;
while(no < 5){
System.out.println("Current no. is " + no);
no++; // update the value, otherwise the loop will never end because no is always zero if not increase!
}
// Do-while loop: This loop will execute the code block once, before checking if the condition is true,
// If true then it will repeat the loop as long as the condition is true.
short dayInWeek = 7;
short curDay = 0;
do{
curDay++;
System.out.println("Day" + curDay);
}
while(curDay < dayInWeek);
// ForLoop: exactly loop through a block of code for the mentioned condition
System.out.println("ForLoop");
for(short i=0; i < 5; i++){
System.out.println(i);
}
// Nested ForLoop
System.out.println("Nested ForLoop");
for(short i=0; i<5; i++){
System.out.println(i + "Parent");
for(short j = 0; j < 2; j++){
System.out.println(j + "Child");
}
}
// ForEach Loop: loop through elements in an array;
System.out.println("ForEach Loop");
String[] fruits = {"Mango", "Orange", "Grapes", "Watermelon"};
for(String item:fruits){
System.out.println(item);
}
// Break: statement can be used to jump out of a loop if condition match.
System.out.println("Break Statement");
for(int i=0; i<5; i++){
if(i==2){
break;
}
System.out.println(i);
}
// Continue: statement can be used to jump out of a loop.
System.out.println("Contniue Statement");
for(int i=0; i<5; i++){
if(i==2){
continue;
}
System.out.println(i);
}
}
}