Explain Control Statements In Java, If Statement In Java Explain With Programm, Explain IF Statement With Program And Example In Java
In This Topic & Blog Having Any Query Then Post your Comment and Suggestion Below

Control Statements
Although Chapter 5 will look closely at control statements, two are briefly introduced here so
that they can be used in example programs in Chapters 3 and 4. They will also help illustrate
an important aspect of Java: blocks of code.
The if Statement
The Java
if
statement works much like the IF statement in any other language. Further, it is
syntactically identical to the
if
statements in C, C++, and C#. Its simplest form is shown here:
if( condition ) statement ;
Here, condition is a Boolean expression. If condition is true, then the statement is executed.
If condition is false, then the statement is bypassed. Here is an example:
if(num < 100) System.out.println("num is less than 100");
In this case, if
num
contains a value that is less than 100, the conditional expression is
true, and
println( )
will execute. If
num
contains a value greater than or equal to 100, then
the
println( )
method is bypassed.
Here is a program that illustrates the
if
statement:
/*
Demonstrate the if.
Call this file "IfSample.java".
*/
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");
x = x * 2;
if(x > y) System.out.println("x now greater than y");
// this won't display anything
if(x == y) System.out.println("you won't see this");
}
}
The output generated by this program is shown here:
x is less than y
x now equal to y
x now greater than y
Notice one other thing in this program. The line
int x, y;
declares two variables,
x
and
y
, by use of a comma-separated list.
Courtesy: Java Complete reference Book
Herbart Schieldt
Labels: Java - J2SE