From c53211dbef8eb12485f9b617d4babd4d7339d461 Mon Sep 17 00:00:00 2001 From: rodolforfq <31481067+rodolforfq@users.noreply.github.com> Date: Sun, 16 Dec 2018 16:07:31 -0400 Subject: [PATCH] BAEL-2432 (#5936) Article's companion code. --- .../ConditionalBranches.java | 68 +++++++++++++++++ .../com/baeldung/controlstructures/Loops.java | 73 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 core-java-lang/src/main/java/com/baeldung/controlstructures/ConditionalBranches.java create mode 100644 core-java-lang/src/main/java/com/baeldung/controlstructures/Loops.java diff --git a/core-java-lang/src/main/java/com/baeldung/controlstructures/ConditionalBranches.java b/core-java-lang/src/main/java/com/baeldung/controlstructures/ConditionalBranches.java new file mode 100644 index 0000000000..bace609699 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/controlstructures/ConditionalBranches.java @@ -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; + } + } + +} diff --git a/core-java-lang/src/main/java/com/baeldung/controlstructures/Loops.java b/core-java-lang/src/main/java/com/baeldung/controlstructures/Loops.java new file mode 100644 index 0000000000..83f2a27ea7 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/controlstructures/Loops.java @@ -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; + } + } + } + +}