Skip to content

Commit 890849e

Browse files
committed
Control flow statements in Java
1 parent cca2e3c commit 890849e

17 files changed

Lines changed: 454 additions & 0 deletions

section8/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class BreakStatementDemo {
2+
3+
public static void main(String[] args) {
4+
5+
for(int i = 0; i < 10; i++) {
6+
if (i == 5) {
7+
break;
8+
}
9+
System.out.println(i);
10+
}
11+
12+
int [] arrayOfInt = { 43, 455, 4376, 343, 6 };
13+
int lookingFor = 455;
14+
boolean isFound = false;
15+
for(int i =0;i<arrayOfInt.length;i++) {
16+
if(arrayOfInt[i] == lookingFor){
17+
isFound = true;
18+
break;
19+
}
20+
}
21+
22+
if(isFound) {
23+
System.out.println("Hurray number is found");
24+
} else {
25+
System.out.println("Oops ! Number is not found");
26+
}
27+
28+
outerForLoop:
29+
for (int i =1; i<5; i++ ) {
30+
31+
innerForLoop:
32+
for (int j =1; j < 3; j++) {
33+
System.out.println(" i = " + i + " and j = " + j);
34+
if(i == 3){
35+
break outerForLoop;
36+
}
37+
}
38+
39+
}
40+
41+
42+
}
43+
44+
}

section8/src/CanVote.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class CanVote {
2+
3+
public static void main(String[] args) {
4+
boolean isCitizen = false;
5+
int age = 19;
6+
7+
if (isCitizen && age > 18) {
8+
System.out.println("The person can vote");
9+
} else {
10+
System.out.println("The person can't vote");
11+
}
12+
}
13+
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class ContinueStatementDemo {
2+
3+
public static void main(String[] args) {
4+
5+
for(int i = 0; i < 10; i++) {
6+
if ( i % 2 == 0) {
7+
continue;
8+
}
9+
System.out.println(i);
10+
}
11+
12+
outer:
13+
for (int i =1; i<5; i++ ) {
14+
15+
inner:
16+
for (int j =1; j < 5; j++) {
17+
if(j == 2){
18+
continue outer;
19+
}
20+
System.out.println(" i = " + i + " and j = " + j);
21+
}
22+
23+
}
24+
25+
}
26+
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class DoWhileStatementDemo {
2+
3+
public static void main(String[] args) {
4+
5+
int n = 6;
6+
7+
do {
8+
System.out.println(n);
9+
n++;
10+
} while ( n < 10);
11+
12+
}
13+
14+
}

section8/src/EvenOddNumber.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class EvenOddNumber {
2+
3+
public static void main(String[] args) {
4+
int inputNumber = 9;
5+
6+
if (inputNumber%2 == 0) {
7+
System.out.println("The given number is even");
8+
} else {
9+
System.out.println("The given number is odd");
10+
}
11+
12+
}
13+
14+
}

section8/src/FindTheDay.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class FindTheDay {
2+
3+
public static void main(String[] args) {
4+
String whatIsToday = "Monday";
5+
6+
if(whatIsToday.equals("Monday")) {
7+
System.out.println("It is weekday");
8+
} else if (whatIsToday.equals("Tuesday")) {
9+
System.out.println("It is weekday");
10+
} else if (whatIsToday.equals("Wednesday")) {
11+
System.out.println("It is weekday");
12+
} else if (whatIsToday.equals("Thursday")) {
13+
System.out.println("It is weekday");
14+
} else if (whatIsToday.equals("Friday")) {
15+
System.out.println("It is weekday");
16+
} else if (whatIsToday.equals("Saturday")) {
17+
System.out.println("It is weekend");
18+
} else if (whatIsToday.equals("Sunday")) {
19+
System.out.println("It is weekend");
20+
} else {
21+
System.out.println("Invalid input. Please check the same");
22+
}
23+
24+
if(whatIsToday.equals("Monday") || whatIsToday.equals("Tuesday")
25+
|| whatIsToday.equals("Wednesday") || whatIsToday.equals("Thursday")
26+
|| whatIsToday.equals("Friday")) {
27+
System.out.println("It is weekday");
28+
} else if (whatIsToday.equals("Saturday") || whatIsToday.equals("Sunday")) {
29+
System.out.println("It is weekend");
30+
} else {
31+
System.out.println("Invalid input. Please check the same");
32+
}
33+
34+
}
35+
36+
}

section8/src/ForLoopDemo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class ForLoopDemo {
2+
3+
public static void main(String[] args) {
4+
int result ;
5+
for (int i = 1; i <= 10; i++) {
6+
result = i * 9;
7+
System.out.println("9 * " + i + " = "+ result);
8+
}
9+
10+
/*for(; ;) {
11+
System.out.println("Hi");
12+
}*/
13+
14+
for(; ;) ;
15+
16+
}
17+
18+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class LocalVariablesDemo {
2+
3+
public static void main(String[] args) {
4+
5+
for (int i = 0; i < 5; i++) { // i declared as a local variable
6+
int x = i * 2; // x is a local variable
7+
System.out.println("i = " + i + ", x = " + x);
8+
}
9+
10+
int number = 10;
11+
if (number > 0) {
12+
int square = number * number;
13+
System.out.println("The square of " + number + " is " + square);
14+
} else {
15+
int absoluteValue = -1 * number;
16+
System.out.println("The absolute value of " + number + " is " + absoluteValue);
17+
}
18+
19+
}
20+
21+
public void myMethod() {
22+
int x = 10;
23+
System.out.println(x);
24+
25+
int number = 2;
26+
27+
switch (number) {
28+
case 1:
29+
System.out.println("Odd number");
30+
// System.out.println(i); // Compilation error as i is never declared
31+
break;
32+
case 2:
33+
int i = number; // local variable i declared first time
34+
System.out.println(i);
35+
System.out.println("Even number");
36+
break;
37+
default:
38+
i = 10; // local variable i can be accessed as it is declared in the top
39+
System.out.println(i);
40+
System.out.println("Invalid number");
41+
break;
42+
}
43+
44+
// System.out.println(i);
45+
46+
}
47+
48+
}

section8/src/MessageForDay.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class MessageForDay {
2+
3+
public static void main(String[] args) {
4+
5+
String dayOfWeek = "Wednesday";
6+
7+
if (dayOfWeek.equals("Saturday") || dayOfWeek.equals("Sunday")) {
8+
System.out.println("Hooray, it's the weekend!");
9+
if (dayOfWeek.equals("Saturday")) {
10+
System.out.println("Time for a relaxing day or maybe some outdoor activities!");
11+
} else {
12+
System.out.println("Lazy Sunday vibes perfect for a cozy day indoors.");
13+
}
14+
} else {
15+
System.out.println("It's a weekday. Time to work or attend classes.");
16+
if (dayOfWeek.equals("Wednesday") || dayOfWeek.equals("Thursday")
17+
|| dayOfWeek.equals("Friday")) {
18+
System.out.println("Midweek hustle! Keep going, the weekend is approaching.");
19+
} else {
20+
System.out.println("Monday blues? Grab some coffee and power through the day!");
21+
}
22+
}
23+
24+
}
25+
26+
}

0 commit comments

Comments
 (0)