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.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
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.
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.
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.
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.
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.
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Recommended Posts