-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b18ef5f
commit 66d19d8
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class JavaConditionalStatements { | ||
public static void main(String args[]) { | ||
int a = 3, b = 20, c = 30; | ||
// Simple if-else statement: | ||
if (a < b) { | ||
System.out.printf("%d is less than %d", a, b); | ||
} else { | ||
System.out.printf("%d is greater than %d", a, b); | ||
} | ||
|
||
System.out.println(" "); | ||
|
||
// Ladder if-else or if-else if | ||
if (a > b && a > c) { | ||
System.out.printf("%d is the greatest number.", a); | ||
} else if (b > a && b > c) { | ||
System.out.printf("%d is the greatest number", b); | ||
} else { | ||
System.out.printf("%d is the greatest number", c); | ||
} | ||
|
||
System.out.println(" "); | ||
|
||
// Switch case | ||
|
||
int day = 8; | ||
|
||
switch (day) { | ||
|
||
case 1: | ||
System.out.println("Monday"); | ||
break; | ||
|
||
case 2: | ||
System.out.println("Tuesday"); | ||
break; | ||
|
||
case 3: | ||
System.out.println("Wednesday"); | ||
break; | ||
|
||
case 4: | ||
System.out.println("Thrusday"); | ||
break; | ||
|
||
case 5: | ||
System.out.println("Friday"); | ||
break; | ||
|
||
case 6: | ||
System.out.println("Saturday"); | ||
break; | ||
|
||
case 7: | ||
System.out.println("Sunday"); | ||
break; | ||
|
||
default: | ||
System.out.println("Please make a valid choice.."); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
public class JavaLooping { | ||
public static void main(String[] args) { | ||
// for loops : program to print all even numbers between 1-50 | ||
|
||
for (int i = 0; i <= 50; i++) { | ||
if (i % 2 == 0) { | ||
System.out.println(i); | ||
} | ||
} | ||
|
||
// while loop : | ||
|
||
int num = 0; | ||
|
||
while (num <= 5) { | ||
System.out.println("Hello World !"); | ||
num++; | ||
} | ||
|
||
// do-while loop: | ||
|
||
do { | ||
System.out.println("Hello from do-while loop"); | ||
num++; | ||
} while (num > 10); | ||
|
||
// Nested loop: | ||
|
||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 2; j++) { | ||
System.out.println(i); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
int weeks = 3; | ||
int days = 7; | ||
int i = 1; | ||
// outer loop | ||
while (i <= weeks) { | ||
System.out.println("Week: " + i); | ||
|
||
// inner loop | ||
for (int j = 1; j <= days; ++j) { | ||
System.out.println(" Days: " + j); | ||
} | ||
++i; | ||
} | ||
|
||
} | ||
} |