-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathJavaLoops.java
47 lines (40 loc) · 1.65 KB
/
JavaLoops.java
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
import java.util.Scanner;
class JavaLoops {
public static void main (String args[]) {
int a = 1; // Assign a starting value to variable 'a'
// Demonstrating Usage of While Loop in Java
// While loop is an entry controlled loop
System.out.print("***** Using While Loop ****** ");
System.out.println();
// While the value of Variable a < = 10 do the condition in the loop body { do this }
while( a <= 10) {
System.out.println("Value of a : " + a); // While the loop condition is satisfied print value of 'a'
a++; // Increment the value of 'a' for every iteration
}
// Demonstrating Usage of For Loop in Java
// for loop is an entry controlled loop
// Print the values that are in the range of the condition given in the loop.
System.out.print("***** Using For Loop ****** ");
System.out.println();
for( a = 0 ; a<=10 ; a++ ) {
System.out.println("Value of a : " + a); // While the loop condition is satisfied print value of 'a'
}
// adding do while loop
// exit controlled loop
System.out.print("***** Using Do While Loop ****** ");
a=0;
do{
System.out.println("Value of a : " + a);
a++;
}while (a<=10);
// for each loop example in java
// majorly used with Maps and Sets
System.out.print("***** Output Using forEach Loop ****** ");
System.out.println();
int inp[] = {1,2,3,4,5};
// prints each element in the inp array
for(int i : inp){
System.out.println(i+" ");
}
}
}