Java starting from beginners, logical operators / conditional operators

Postscript: 2020/3/6 We received a large editing request from @shiracamus, so we will replace it there. (I left the old article as a memorandum for myself in the limited post) Postscript 2020/3/6 Corrected the content that the semicolon was required at the end of the conditional operator. I am always grateful for your help.

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

Logical operator

** Logical Operator ** is an operator used to describe more complicated conditions while using the conditions and relational operators mentioned last time. This time, I will write "how many operands (terms) are operators?" An operator that takes two operands is called a ** binary operator **, and an operator that takes only one operand is called a ** unary operator **. Write an expression in the operand.

You can see what the truth values are by using the Venn diagrams that you often see in mathematical "sets". I'm sorry I'm not good at it, but I applied it. (Most important issue: drawing ability) ベン図(仮).png

Green && Red: true is blue Green||Red:true is everything except ashes ! (Gray): Red Blue Green

Will be. (I'm sorry to point out that blue is actually distorted.)

Practicing mistakes in logical operators

digitForture.java


import java.io.*;
class digitForture
{
    public static void main(String[] args) throws IOException
    {
      System.out.println("Please enter your favorite single digit number.");

      BufferedReader br =
        new BufferedReader(new InputStreamReader(System.in));

      String str = br.readLine();
      int digit = Integer.parseInt(str);

      if(digit > 10 && digit <= -1){
          System.out.println("Please enter a number up to one digit.");
          //Hmm?
        }
      else{
          System.out.println("Your numbers" + digit + "is not it?");
          System.out.println("I will check it from now on, so please wait for a while....。");
    }
}
...//Omitted thereafter. Suppose you have prepared a fortune-telling table linked to a single-digit number used for fortune-telling.

At this rate, the logical operations prepared will not be effective, so properly**&&To||To**Must. Are you dealing with AND or OR? I felt that it was very necessary to practice making Venn diagrams (easy to see) in order to organize my thoughts.

Conditional operator

** Conditional operator ** takes three ** operands **, and for ** conditional expression evaluation result **, ** expression when true ** and expression when ** false * * Can be written and returns the evaluation result of either expression. The other formula is not evaluated. After the "conditional expression", write "? " And "expression when true", and then write ": " and "expression when false".

Uninflected form of conditional operator


Conditional expression?Expression when true:Expression when false

One point to note. There is no trailing; (semicolon) in the quote. ~~ ** Must be included **. ~~

2020/3/6 postscript: It is not necessary to include it because it can be used as a condition in the text. The end of the sentence really needs a semicolon. This is not limited to this operator. (You don't have to be afraid because it will be played with a compile error. At first, I was frustrated that it was often played. Now I feel that it helps me to play it.)

in conclusion

I came up with "pancakeSearch.java" to distinguish between bread and cake, but I have the following problems.

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 starting from beginners, logical operators / conditional operators
Java, abstract classes starting from beginners
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
Java life starting from scratch
Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
Java overload constructor starting from beginner
Java starting from beginner, nested / break / continue
Getting Started with Java Starting from 0 Part 1
Java, for statement / while statement starting from beginner
IntelliJ starting from 1
Java conditional branch
java conditional branch
Programming beginners learn PHP from a Java perspective-variables-
Java starting from beginner, class declaration / object generation
For Java engineers starting Kotlin from now on
[Java] Conditional branch
Java development for beginners to start from 1-Vol.1-eclipse setup
[Java] Platforms to choose from for Java development starting now (2020)
Call Java from JRuby
Changes from Java 8 to Java 11
Sum from Java_1 to 100
java learning (conditional expression)
Eval Java source from Java
Picture-in-picture starting from iOS14
Evaluation of logical operators
Access API.AI from Java
From Java to Ruby !!