[Java] Summary of control syntax

Structured programming elements

Summary of control syntax to implement these

if instruction

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
    var i = 10;
    if (i == 10) {
      System.out.println("The variable i is 10.");
      } else {
      System.out.println("The variable i is not 10.");
      //Same as below
      //System.out.println((i==10)?"Variable is 10":"Not 10");
     }
    }
}

Multi-branch

public static void main(String[] args) {
  var i = 100;
  if (i > 50) {
    System.out.println("The variable i is greater than 50.");
  } else if (i > 30) {
    System.out.println("The variable i is greater than 30 and less than or equal to 50.");
  } else {
    System.out.println("The variable i is 30 or less.");
  }
}

switch instruction

public static void main(String[] args) {
  var rank = "Instep";
  switch (rank) {
    case "Instep":
      System.out.println("It's very good.");
      break;
    case "B":
      System.out.println("Is good.");
      break;
    case "Hinoe":
      System.out.println("Let's do our best.");
      break;
    default:
      System.out.println("???");
      break;
  }
}
//Arrange multiple cases in fallthrough or implement conditions
public static void main(String[] args) {
  var drink = "beer";
  switch (drink) {
    case "Sake":
    case "beer":
    case "wine":
      System.out.println("It is brewed sake.");
      break;
    case "Brandy":
    case "whisky":
      System.out.println("It is distilled liquor.");
      break;
  }
}

while / do instruction

public class Main {
    public static void main(String[] args) throws Exception {
        var i = 1;
        do {
          System.out.println(i + "This is the second loop.");
          i++;
        } while (i < 6);
    }
}

for instruction

//NG example: Floating point type for counter variable
public class Main {
    public static void main(String[] args) throws Exception {
    for (var i = 0.1f; i <= 1.0; i += 0.1f) {
      System.out.println(i);
      //0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.70000005, 0.8000001, 0.9000001
    }
  }
}
//Sequential operator
public static void main(String[] args) {
    for (var i = 1; i < 6; System.out.println(i + "This is the second loop."), i++);
 }

Extended for instruction

public class Main {
  public static void main(String[] args) {
    var data = new String[] { "neko", "tama", "mike" };
    //Extended for instruction
    for (var value : data) {
      System.out.println(value);
    }
  }
}
//Example of receiving command line arguments with args and outputting them in order with extended for
public static void main(String[] args) {
  for (var value : args) {
    System.out.println("Hello," + value + "Mr.!");
  }
}

Loop control

//Exit from the specified loop
public class Main {
  public static void main(String[] args) {
    limit:
    for (var i = 1; i < 10; i++) {
      for (var j = 1; j < 10; j++) {
        var result = i * j;
        if (result > 50) {
          break limit;
        }
        System.out.printf(" %2d", result);
      }
      System.out.println();
    }
  }
}


Recommended Posts

[Java] Summary of control syntax
Java control syntax
Java control syntax
[Java] Control syntax notes
Summary of Java support 2018
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Summary of for statements
Summary of Java Math class
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[For beginners] Summary of java constructor
Summary of [Java silver study] package
Summary of object-oriented programming using Java
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Java knowledge summary
Java Generics Summary
[java] Summary of how to handle char
Java related summary
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
Java 8 documentation summary
Control syntax operator
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
Java 11 document summary
[Java] Overview of Java
[java] Summary of how to handle character strings
Summary of Java environment settings for myself [mac]
Organized memo in the head (Java --Control syntax)
[Java] Personal summary of classes and methods (basic)
Expired collection of java
Predicted Features of Java
[Summary] Java environment preparation
effective java 3rd summary
NIO.2 review of java
Java 13 new feature summary
Summary of strong parameters
Summary of jar files
Java static [Personal summary]
History of Java annotation
Summary of information security
Summary of using FragmentArgs
java (merits of polymorphism)
Thread safe summary ~ Java ~
Java Primitive Specialization Summary
Java development link summary
NIO review of java
java regular expression summary
Summary of using DBFlow
Java 14 new feature summary
Java design pattern summary
Java reserved word summary
Java8 Stream Rough Summary
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
This is convenient! Summary of popular Java library + α
Summary of ToString behavior with Java and Groovy annotations
[Java Silver] Summary of points related to lambda expressions