[Java] [Java] If you say conditional branching, it’s an if statement, but there are also conditional operators.

3 minute read

Introduction

I am an engineer inexperienced (34 years old) and changed my job as an engineer. Get a java silver qualification at the job change destination. That is the learning memo. (Because java was originally a language I wanted to learn, it was just right.) So the content of the article is really basic. And at the same time, I hope you can feel free to make a mistake if you make an error…m(_ _)m

If you say conditional branching, you mean that?

Until now (of me)

I “What should I do when I want to branch processing depending on conditions in programming?” Another me, “It’s a fixed partner. This is if sentence or switch sentence!”

In case of #### if statement

Another person, “Activate reverse card! “Conditional branch if statement”! This card judges whether the result processed by the conditional expression is true or false! If true, the processing inside {} immediately after the conditional expression. false In case of, the inside of {} immediately after else is executed!”

if statement.java


if (conditional expression) {
   true processing;
}else{
   false processing;
}

Another me, “However, if you can omit the {} after the conditional expression in the if statement! In that case, when the result of the conditional expression is true, the one line after that is judged as the processing of true. Ru!

if statement part 2.java


if (conditional expression)
true processing;
Whether it's true or false, it runs;

switch statement

Another me, “Activate reverse card! “Conditional branch switch statement”! This card h (Omitted)

switch statement.java


switch (expression){
 case constants:
   Processing that applies to the case;
   break;
 case constants:
   Case-specific processing;
   break;
}

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

It was such a shame, 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 that Expression 2 is executed.

Try it out

I tried to use immediately. This is what the program looks like.

  1. Pass a character string as an argument when executing java on the command line.
  2. Check if it matches the string “test” (this time, conditional operator: source code 6th line) If they match, combine the character string of “” which matches the variable “str (input value and “test”). If they do not match, combine the string ""not match" to the variable str (input value and "test")
  3. Output the combined str on the 7th line 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 = "The input value and \"test\" is ";
        str += inputString.equals("test")? "matches": "does not match";
        System.out.println(str);
    }
}
/*
 → When you pass test on the command line and execute it, "The input value and "test" match" is displayed
*/

Is it limited to simple conditional expressions? If the level of “If you use an if statement to write over multiple lines? This condition.” is written, it will be shorter and the source code will be refreshing.

at the end

I think it’s really a beginning, but surprisingly for beginners it’s unfamiliar and there are no drawers in the first place, so I summarized it. If you find any mistakes, I would appreciate it if you could read this article, and for my ~ growth and confidence ~.