parent
ad03c69e01
commit
c53211dbef
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.controlstructures;
|
||||
|
||||
public class ConditionalBranches {
|
||||
|
||||
/**
|
||||
* Multiple if/else/else if statements examples. Shows different syntax usage.
|
||||
*/
|
||||
public static void ifElseStatementsExamples() {
|
||||
int count = 2; // Initial count value.
|
||||
|
||||
// Basic syntax. Only one statement follows. No brace usage.
|
||||
if (count > 1)
|
||||
System.out.println("Count is higher than 1");
|
||||
|
||||
// Basic syntax. More than one statement can be included. Braces are used (recommended syntax).
|
||||
if (count > 1) {
|
||||
System.out.println("Count is higher than 1");
|
||||
System.out.println("Count is equal to: " + count);
|
||||
}
|
||||
|
||||
// If/Else syntax. Two different courses of action can be included.
|
||||
if (count > 2) {
|
||||
System.out.println("Count is higher than 2");
|
||||
} else {
|
||||
System.out.println("Count is lower or equal than 2");
|
||||
}
|
||||
|
||||
// If/Else/Else If syntax. Three or more courses of action can be included.
|
||||
if (count > 2) {
|
||||
System.out.println("Count is higher than 2");
|
||||
} else if (count <= 0) {
|
||||
System.out.println("Count is less or equal than zero");
|
||||
} else {
|
||||
System.out.println("Count is either equal to one, or two");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ternary Operator example.
|
||||
* @see ConditionalBranches#ifElseStatementsExamples()
|
||||
*/
|
||||
public static void ternaryExample() {
|
||||
int count = 2;
|
||||
System.out.println(count > 2 ? "Count is higher than 2" : "Count is lower or equal than 2");
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch structure example. Shows how to replace multiple if/else statements with one structure.
|
||||
*/
|
||||
public static void switchExample() {
|
||||
int count = 3;
|
||||
switch (count) {
|
||||
case 0:
|
||||
System.out.println("Count is equal to 0");
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("Count is equal to 1");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Count is equal to 2");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Count is either negative, or higher than 2");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.controlstructures;
|
||||
|
||||
public class Loops {
|
||||
|
||||
/**
|
||||
* Dummy method. Only prints a generic message.
|
||||
*/
|
||||
private static void methodToRepeat() {
|
||||
System.out.println("Dummy method.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows how to iterate 50 times with 3 different method/control structures.
|
||||
*/
|
||||
public static void repetitionTo50Examples() {
|
||||
for (int i = 1; i <= 50; i++) {
|
||||
methodToRepeat();
|
||||
}
|
||||
|
||||
int whileCounter = 1;
|
||||
while (whileCounter <= 50) {
|
||||
methodToRepeat();
|
||||
whileCounter++;
|
||||
}
|
||||
|
||||
int count = 1;
|
||||
do {
|
||||
methodToRepeat();
|
||||
count++;
|
||||
} while (count < 50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints text an N amount of times. Shows usage of the {@code break} branching statement.
|
||||
* @param textToPrint Text to repeatedly print.
|
||||
* @param times Amount to times to print received text.
|
||||
*/
|
||||
public static void printTextNTimes(String textToPrint, int times) {
|
||||
int counter = 1;
|
||||
while (true) {
|
||||
System.out.println(textToPrint);
|
||||
if (counter == times) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints an specified amount of even numbers. Shows usage of both {@code break} and {@code continue} branching statements.
|
||||
* @param amountToPrint Amount of even numbers to print.
|
||||
*/
|
||||
public static void printEvenNumbers(int amountToPrint) {
|
||||
if (amountToPrint <= 0) { // Invalid input
|
||||
return;
|
||||
}
|
||||
int iterator = 0;
|
||||
int amountPrinted = 0;
|
||||
while (true) {
|
||||
if (iterator % 2 == 0) { // Is an even number
|
||||
System.out.println(iterator);
|
||||
amountPrinted++;
|
||||
iterator++;
|
||||
} else {
|
||||
iterator++;
|
||||
continue; // Won't print
|
||||
}
|
||||
if (amountPrinted == amountToPrint) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue