[Java] Conditional branching is an if statement, but there is also a conditional operator.

Introduction

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

If you say conditional branching, you mean that, right?

Until now (me)

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 !"

For if 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;

switch statement

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;
}

There is also a conditional operator (this is the main subject)

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.

syntax

** 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.

Actually use

I tried to use immediately. The content of the program looks like this.

  1. When executing java on the command line, pass a character string as an argument.
  2. Check if it matches the string "test" (this time, conditional operator: source code line 6) If they match, combine the string str (the input value and" test ") "matches" the variable . If they don't match, combine the string " does not match "with the variablestr (the input value and" test ")`
  3. Output the str combined on line 7 to the console

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.

at the end

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

[Java] Conditional branching is an if statement, but there is also a conditional operator.
Java study # 4 (conditional branching / if statement)
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
Write a third If statement that is neither an if statement nor a ternary operator
[Java] "Setting" using Enum rather than conditional branching by if statement -Strategy Enum
Try using conditional branching other than if statement
If there is a state transition, let's create a State class
Is there an instance even if the constructor fails?
What is an operator?
Also complicated conditional branching
[Java] A class is an OS, and an instance is a virtual computer.
What is a Java collection?
A little complicated conditional branching
java (conditional branching and repetition)
[Ruby] conditional branch if statement
Creating a mixed conditional expression of Rails if statement and unless
Determining if a custom keyboard is enabled in Android Studio (Java)
Resultset's next () is not a "method to determine if there is a ResultSet next".
What to do if zip dies if there is a pom when making an executable jar with gradle