Skip to content

Commit c53211d

Browse files
rodolforfqmaibin
authored andcommitted
BAEL-2432 (eugenp#5936)
Article's companion code.
1 parent ad03c69 commit c53211d

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.controlstructures;
2+
3+
public class ConditionalBranches {
4+
5+
/**
6+
* Multiple if/else/else if statements examples. Shows different syntax usage.
7+
*/
8+
public static void ifElseStatementsExamples() {
9+
int count = 2; // Initial count value.
10+
11+
// Basic syntax. Only one statement follows. No brace usage.
12+
if (count > 1)
13+
System.out.println("Count is higher than 1");
14+
15+
// Basic syntax. More than one statement can be included. Braces are used (recommended syntax).
16+
if (count > 1) {
17+
System.out.println("Count is higher than 1");
18+
System.out.println("Count is equal to: " + count);
19+
}
20+
21+
// If/Else syntax. Two different courses of action can be included.
22+
if (count > 2) {
23+
System.out.println("Count is higher than 2");
24+
} else {
25+
System.out.println("Count is lower or equal than 2");
26+
}
27+
28+
// If/Else/Else If syntax. Three or more courses of action can be included.
29+
if (count > 2) {
30+
System.out.println("Count is higher than 2");
31+
} else if (count <= 0) {
32+
System.out.println("Count is less or equal than zero");
33+
} else {
34+
System.out.println("Count is either equal to one, or two");
35+
}
36+
}
37+
38+
/**
39+
* Ternary Operator example.
40+
* @see ConditionalBranches#ifElseStatementsExamples()
41+
*/
42+
public static void ternaryExample() {
43+
int count = 2;
44+
System.out.println(count > 2 ? "Count is higher than 2" : "Count is lower or equal than 2");
45+
}
46+
47+
/**
48+
* Switch structure example. Shows how to replace multiple if/else statements with one structure.
49+
*/
50+
public static void switchExample() {
51+
int count = 3;
52+
switch (count) {
53+
case 0:
54+
System.out.println("Count is equal to 0");
55+
break;
56+
case 1:
57+
System.out.println("Count is equal to 1");
58+
break;
59+
case 2:
60+
System.out.println("Count is equal to 2");
61+
break;
62+
default:
63+
System.out.println("Count is either negative, or higher than 2");
64+
break;
65+
}
66+
}
67+
68+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.controlstructures;
2+
3+
public class Loops {
4+
5+
/**
6+
* Dummy method. Only prints a generic message.
7+
*/
8+
private static void methodToRepeat() {
9+
System.out.println("Dummy method.");
10+
}
11+
12+
/**
13+
* Shows how to iterate 50 times with 3 different method/control structures.
14+
*/
15+
public static void repetitionTo50Examples() {
16+
for (int i = 1; i <= 50; i++) {
17+
methodToRepeat();
18+
}
19+
20+
int whileCounter = 1;
21+
while (whileCounter <= 50) {
22+
methodToRepeat();
23+
whileCounter++;
24+
}
25+
26+
int count = 1;
27+
do {
28+
methodToRepeat();
29+
count++;
30+
} while (count < 50);
31+
}
32+
33+
/**
34+
* Prints text an N amount of times. Shows usage of the {@code break} branching statement.
35+
* @param textToPrint Text to repeatedly print.
36+
* @param times Amount to times to print received text.
37+
*/
38+
public static void printTextNTimes(String textToPrint, int times) {
39+
int counter = 1;
40+
while (true) {
41+
System.out.println(textToPrint);
42+
if (counter == times) {
43+
break;
44+
}
45+
}
46+
}
47+
48+
/**
49+
* Prints an specified amount of even numbers. Shows usage of both {@code break} and {@code continue} branching statements.
50+
* @param amountToPrint Amount of even numbers to print.
51+
*/
52+
public static void printEvenNumbers(int amountToPrint) {
53+
if (amountToPrint <= 0) { // Invalid input
54+
return;
55+
}
56+
int iterator = 0;
57+
int amountPrinted = 0;
58+
while (true) {
59+
if (iterator % 2 == 0) { // Is an even number
60+
System.out.println(iterator);
61+
amountPrinted++;
62+
iterator++;
63+
} else {
64+
iterator++;
65+
continue; // Won't print
66+
}
67+
if (amountPrinted == amountToPrint) {
68+
break;
69+
}
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)