- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor_loop.java
More file actions
Latest commit
43 lines (35 loc) · 1.17 KB
/
Copy pathfor_loop.java
File metadata and controls
43 lines (35 loc) · 1.17 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
publicclassfor_loop {
// java For Loop
Whenyouknowexactlyhowmanytimesyouwanttoloopthroughablockofcode, usetheforloopinsteadofawhileloop:
SyntaxGetyourownJavaServer
for (statement1; statement2; statement3) {
// code block to be executed
}
Statement1isexecuted (onetime) beforetheexecutionofthecodeblock.
Statement2definestheconditionforexecutingthecodeblock.
Statement3isexecuted (everytime) afterthecodeblockhasbeenexecuted.
Theexamplebelowwillprintthenumbers0to4:
Example
for (inti = 0; i < 5; i++) {
System.out.println(i);
}
*/
//nested for loop
publicstaticvoidmain(String[] args) {
/* for (int i=1;i<=2;i++){
System.out.println("Outer"+i);
for(int j=1;j<=3;j++){
System.out.println("ineer"+j);
}
}*/
// for each loop
/*
for (typevariableName : arrayName) {
// code block to be executed
}
String[] cars = {"VOlvo","bmw","audi","benz"};
for(Stringi : cars){
System.out.println(i);
}
}
}