Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to format "if" statements in Java with multiline "ands" or "ors"? [closed]

Apparently "if", "and", and "or" are such generic search parameters that I can't find the answer on google for my life. Which of these is the correct format according to the Java standards?

Option 1:

if (condition1     && condition2     && condition3) ... 

or Option 2:

if (condition1 &&     condition2 &&     condition3) ... 
like image 954
Dasmowenator Avatar asked Nov 07 '13 00:11

Dasmowenator


People also ask

How do I format an if statement in Java?

Java has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

How do you write multiple or statements in Java?

When using multiple conditions, we use the logical AND && and logical OR || operators. Note: Logical AND && returns true if both statements are true. Logical OR || returns true if any one of the statements is true.

Can we use 2 if statements in Java?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.


2 Answers

The Oracle/Sun guidelines ("Code Conventions for the Java TM Programming Language") tell us to break before an operator. And they give this example.

if ((condition1 && condition2)         || (condition3 && condition4)         ||!(condition5 && condition6)) {     doSomethingAboutIt(); }  

Many companies that I've worked for adopt the Oracle/Sun guidelines as the standard for their own code.

Refer http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248

like image 194
Dawood ibn Kareem Avatar answered Oct 06 '22 23:10

Dawood ibn Kareem


I believe the correct answer is <CTRL>+<SHIFT>+F in Eclipse :)

like image 27
Xuan Avatar answered Oct 06 '22 22:10

Xuan