Java, if statement / switch statement starting from beginner

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

Conditional judgment statement

When performing processing according to the situation, in Java, the premise of processing is grasped by the idea of ** "Condition" **. It can be considered the same as the conditions used in daily life, but the "condition" in Java refers to a condition that has either a value of ** True or False **. The conditional judgment statement is a statement that performs processing according to the value of ** "condition" **, and is a function that can prepare the contents of processing separately based on the value of the condition.

Relational operator

Of the relational operators, I talked about equal signs and inequality signs last time. The rest of the relational operators are almost the same as those dealt with in mathematics.

if statement

Here is the basic structure.

if(conditions){
Contents;
Contents;
  ...
}
else{
Contents;
Contents;
  ...
}

Be careful about the position of **; (semicolon). ** Do not add immediately after if or else, and always add after the content. Also, unless there are special circumstances, enclose the contents of if and else in {}.

The following mistakes.

WrongEnclosing.java


...
int eggsInPoundCake = 2;
int theseEggsWeUse = 8;

if(theseEggsWeUse != eggsInPoundCake)
  System.out.println("The number of eggs required for a pound cake is two.");
  System.out.println("Eggs for pound cake" + eggsInPoundCake +"You will get one.");
  theseEggsWeUse= theseEggsWeUse - eggsInPoundCake;
else
  System.out.println("I've used up all the eggs.");

 //Two places are wrong. But there is one reason why it can't be compiled.

The if statement is not enclosed in {}

System.out.println ("The number of eggs required for a pound cake is two."); It only hangs up to the stage. ** A compile error occurs as the else statement below is treated as not connected. ** ** If there is no else

System.out.println ("You'll get" + eggsInPoundCake + "Eggs for pound cake."); theseEggsWeUse= theseEggsWeUse - eggsInPoundCake;

These two lines are treated as outside the if statement, and are processed arbitrarily even though they are conditional (I think).

I will omit the else if this time.

switch statement

Here is the basic structure.

switch(formula){
case value:
Contents;
   ...
   break;
case value:
Contents;
   ...
   break;
  default:
Contents;
   ...
   break;
}

A break is a statement that forcibly ends the processing inside the block and exits the block. ** In the switch statement, all the contents of the corresponding process are displayed up to the contents of default (if there is default), so break is used when you want to operate the process flow. I made a mistake below.

noBreak.java


...
int creditInSchool = 0;
char judge = 'A';
... //Examination of each condition required for school credits
    //judge is the unit

//Suppose he remained in the unit of A
switch(judge){
  case 'A':
    System.out.println("His unit is" + judge + "It is.");
  case 'B':
    System.out.println("His unit is" + judge + "It is.");
  case 'C':
    System.out.println("His unit is" + judge + "It is.");
  case 'D':
    System.out.println("His unit is" + judge + "It is.");
  default :
    System.out.println("His unit is" + judge + ", This unit cannot be raised");
}

//result
//His unit is A.
//His unit is A.
//His unit is A.
//His unit is A.
//His unit is A, this unit cannot be raised

If you don't put a break, all cases will be executed.

At the end

The conditional statement may be an example or the way to improve by solving a few questions. The problem of piles of else if is difficult, and above all, if you mess up with practice, there is no doubt that it is a spaghetti code.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition

Recommended Posts

Java, if statement / switch statement starting from beginner
Java, for statement / while statement starting from beginner
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java overload constructor starting from beginner
Java switch statement
Java starting from beginner, nested / break / continue
Basics of java basics ② ~ if statement and switch statement ~
Java starting from beginner, class declaration / object generation
Java if and switch statements
Java life starting from scratch
Java, abstract classes starting from beginners
[Java] Branch enum with switch statement
[Java] Really scary switch statement story
Java, interface to start from beginner
Java study # 4 (conditional branching / if statement)
switch statement
java beginner 4
java beginner 3
java beginner
if statement
Java starting from beginners, logical operators / conditional operators
Getting Started with Java Starting from 0 Part 1
If a person from Java learns PHP
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
IntelliJ starting from 1
Java switch statement and break, Kotlin when expression ...
Java Beginner Exercises
[Java] How to switch from open jdk to oracle jdk
For Java engineers starting Kotlin from now on
Java for statement
Java Exercise "Beginner"
Omission of curly braces in if statement (Java silver)
[Beginner] Java variable / logical operator / extended for statement [Note 22]
[Java] Platforms to choose from for Java development starting now (2020)
JAWJAW is convenient if you use WordNet from Java
[Java] for statement, while statement
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Java instruction execution statement
[Java] for statement / extended for statement
Eval Java source from Java
About Ruby if statement
Picture-in-picture starting from iOS14
Access API.AI from Java
Switch java with direnv
Switch statement range specification
10 Corresponds to if statement
Studying Java-Part 10-if statement
From Java to Ruby !!
(Memo) Java for statement
Studying Java-Part 11-switch statement
Java review ③ (control syntax [if / switch / while / for], conditional expression)