I, who has no experience as an engineer (34 years old), changed jobs as an engineer. Let's qualify for java silver at the new job. I was told, so this is a learning memo. (Originally java was the language I wanted to learn, so it's just right) So, the content of the article is really the basic key. At the same time, if there are any mistakes, please feel free to contact me ... m (_ _) m
I "What should I do when I want to branch the process depending on the conditions in programming ...?"
Another servant, "I've decided, my partner. This is ```if statement`` or ``
switch statement
!"
Another me "Reverse card activated!" Conditional branch if statement "! This card is judged as true or false as a result of processing with the conditional expression! If true, the processing in {} immediately after the conditional expression. False In the case of, the inside of {} immediately after else is executed! "
if statement.java
if(Conditional expression){
true processing;
}else{
Handling of false;
}
Another servant "However, if statement can omit {} after the conditional expression! In that case, when the result of the conditional expression is true, the one line following it is judged as true processing. Ru !! "
if statement part 2.java
if(Conditional expression)
true processing;
Processing that runs whether it is true or false;
Another me "Reverse card activated!" Conditional branch switch statement "! This card h (omitted)
switch statement.java
switch(formula){
case constant:
Processing corresponding to the case;
break;
case constant:
Processing corresponding to the case;
break;
}
I was sick of it like that, but it seems that there is also a conditional operator. Since the conditional operator consists of three items, it seems to be classified as a ternary operator.
** Conditional expression? Expression 1: Expression 2; **
I'm not used to it ... (upset) If the result of the conditional expression is true, execute expression 1. If the result of the conditional expression is false, it seems to execute expression 2.
I tried to use immediately. The content of the program looks like this.
str (the input value and" test ")
"matches" the variable . If they don't match, combine the string
" does not match "with the variable
str (the input value and" test ")`Conditional operator sample.java
public class Sample{
public static void main(String[] args){
String inputString = args[0];
System.out.println("inputString is" + inputString);
String str = "With the input value\"test\"Is";
str += inputString.equals("test") ? "It's a match" : "It doesn't match";
System.out.println(str);
}
}
/*
→ If you pass test on the command line and execute it, "with the input value"test"Is in agreement "is displayed
*/
Is the usage limited to simple conditional expressions? If you write something at the level of "Is it enough to write over many lines using an if statement? This condition.", The source code will be shorter and refreshing.
I think it's really a rudimentary beginning, but it's surprisingly unfamiliar to beginners, and there are no drawers in the first place, so I summarized it. If you have any mistakes, I would appreciate it if you could point them out for those who saw this article and for my growth and confidence.
Recommended Posts