Java if statement syntax
The if statement is a fundamental programming contruct. If allows a program to execute different blocks of code depending on the result of a test. This recipe describes the variations of the if statement in Java.
The general form of a Java if statement is:
if (statement) {
code_block_true;
} else {
code_block_false;
}
where statement is anything that evaluates as a boolean value. If the statement is true then the Java code in the “code_block_true” block is executed. Otherwise, the code in “code_block_false” is executed. The else statement is optional, so the simplest example of the if statement is:
if ( x == 1 ) {
System.out.println("yep, x is one.");
}
A block of code is either a single line of code or several lines of code contained in curly braces. Note that in this example, enclosing the code in curly braces is not required, although some programmers prefer to use them anyway since it makes it clearer what code is exectued.
Multiple else if conditions can be chained together:
if ( x == 1 )
System.out.println("one");
else if ( x > 1 ) {
y = x*2;
System.out.println("many");
} else {
y = -x;
System.out.println("negative");
}










richie said on February 21, 2009
if ( x == 1 )
System.out.println(“one”);
else if ( x > 1 ) {
y = x*2;
System.out.println(“many”);
} else {
y = -x;
System.out.println(“negative”);
}
richie said on February 21, 2009
thanks and what are the output?
Kevin Taylor said on February 19, 2010
heres an example.
if (input1.endsWith(“.gov”))
System.out.println(“This is a Government address”);
else if (input1.endsWith(“.edu”))
System.out.println(“This is an educational address”);
else if (input1.endsWith(“.com”))
System.out.println(“This is an organizational address”);
else
System.out.println(“This is an out of country address”);
Svenbayaca said on September 3, 2010
if (x
Svenbayaca said on September 3, 2010
im it expert my grade is
perlim = 77;
midterm = 76;
weak nio see u next year!!!!….. im 1st year from up cabalan
Svenbayaca said on September 3, 2010
type nio ko sa fb para magkaalaman mga weak svenbayaca@ymail.com
Ahmad said on October 9, 2010
an or statement could be added >>> “||”
if (x==1 || y==1) {
System.out.println(“x = one or y = 1″);
}
bbb said on October 13, 2010
Why is none of the indenting showing up (tried Chrome and Safari)?!
Bob said on December 5, 2010
How about evaluating a string expression? I can’t find that anywhere! it does not seem to work as :
if (myStringVariable == “Hello” ) …
what is the right operator? eq? =? ==?
help
Thanks
Kim Honoridez said on December 8, 2010
I would like to ask the effect of having multiple conditions in the if() statement with an && operator.
Example:
if(x == 5
&& y == 7
&& z == 3
&& a != 90
.
.
.)
would this highly affect the program especially when I call it every time I call a new page?
ctviky said on January 29, 2011
how to add and inside if for example.,
if(stringvariable && stringvariable && stringvariable !=null)
{
statement1;
}
else
{
statement2;
}
Mr.BLiek said on February 2, 2011
paki check yung grammar mo bago ka magyabang… napaghahalata ka tuloy na walang alam…