0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

論理演算子のメモ

Posted at

論理演算子&,&&,|,||について、
&&、||はショートサーキット演算子と呼ばれ、左オペランドの結果によって後続処理が不要な場合は処理を省略する。

後続処理というのはショートサーキット演算子以降のすべてに対してなので、以下のような条件も省略される。

public class Main {
    public static void main(String[] args) throws Exception {
     int a = 1;
     int b = 1;
     if(a == 2 & b++ == 1&& a++ == 3 & a++ == 4){    	  
     }
     System.out.println(a+":"+b);
    }
}

1:2

もちろん手前で結果未定になる場合は後続も実行される。

public class Main {
    public static void main(String[] args) throws Exception {
     int a = 1;
     int b = 1;
     if(a == 2 & b++ == 2|| a++ == 1 & a++ == 4){
     }
     System.out.println(a+":"+b);
    }
}

3:2

|と||が混在したりするようなプログラムがあったらこういう目的なんだろうとは思うが…そんな可読性低そうな方法使うか?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?