Skip to content

Commit 154cfb5

Browse files
authored
Merge pull request #7 from uvhareesh/develop
looping statements - while loop
2 parents 1834626 + 065e963 commit 154cfb5

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package loopingORIterativeStatements;
2+
3+
public class WhileLoop {
4+
5+
public static void main(String[] args) {
6+
7+
int i=1;
8+
9+
while(i<=10) // condition
10+
{
11+
System.out.println(i); //statement
12+
i++; // increment
13+
}
14+
15+
16+
}
17+
18+
}
19+
20+
/* condition is true that while loop is executed , condition is false loop will be exited
21+
* if we are not increment the condition i value always 1 is printed in infinite loop
22+
*
23+
*
24+
*
25+
* */
26+
27+
28+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package loopingORIterativeStatements;
2+
3+
public class WhileLoop1 {
4+
public static void main(String[] args) {
5+
// Hello message 10 times
6+
int i=1;
7+
8+
while(i<=10)
9+
{
10+
System.out.println("Hello");
11+
i++;
12+
}
13+
}
14+
15+
}
16+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package loopingORIterativeStatements;
2+
3+
public class WhileLoop2 {
4+
5+
public static void main(String[] args) {
6+
7+
// print even numbers between 1...10
8+
9+
//Approach1:
10+
11+
/* int i=2;
12+
while(i<=10)
13+
{
14+
System.out.println(i);
15+
i+=2; // i=i+2;
16+
} */
17+
18+
//Approach2:
19+
20+
int i=2;
21+
while(i<=10) // how many times we need to run the loop
22+
{
23+
if(i%2==0) // it is filter the while condition value
24+
{
25+
System.out.println(i);
26+
}
27+
i++;
28+
}
29+
30+
31+
}
32+
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package loopingORIterativeStatements;
2+
3+
public class Whileloop4 {
4+
5+
public static void main(String args[]) {
6+
7+
// print 10.......1 in descending order
8+
9+
int i=10;
10+
while(i>0)// (i>=1)
11+
{
12+
System.out.println(i);
13+
i--;
14+
}
15+
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package loopingORIterativeStatements;
2+
3+
public class Whilelopp3 {
4+
5+
public static void main(String[] args) {
6+
/* odd ..even..odd..even..odd */
7+
8+
int i=1;
9+
while(i<=10)
10+
{
11+
if(i%2==0)
12+
{
13+
System.out.println(i + "Even");
14+
}
15+
else
16+
{
17+
System.out.println(i + "Odd");
18+
}
19+
i++;
20+
}
21+
22+
23+
}
24+
25+
}

0 commit comments

Comments
 (0)