Module 2: Operators and Control Statements Programming in Java (18CS653)
Module 2
Control Statements
Java’s program control statements can be put into the following categories: selection,
iteration, and jump.
Selection statements allow your program to choose different paths of execution based
upon the outcome of an expression or the state of a variable.
Iteration statements enable program execution to repeat one or more statements (that
is, iteration statements form loops).
Jump statements allow your program to execute in a nonlinear fashion.
Java’s Selection Statements
Java supports two selection statements: if and switch.
The if statement
The if statement executes a block of code only if the specified expression is true.
If the value is false, then the if block is skipped and execution continues with the rest
of the program.
You can either have a single statement or a block of code within an if statement.
Note that the conditional expression must be a Boolean expression.
Syntax:
if (<conditional expression>) {
<statements>
}
Example:
public class Example {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)
Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1
,Module 2: Operators and Control Statements Programming in Java (18CS653)
System.out.println("b > a");
}
}
The if else statement
The if statement is Java’s conditional branch statement. It can be used to route
program execution through two different paths.
Here is the general form of the if statement:
Syntax:
if (condition)
statement1;
else statement2;
Here, each statement may be a single statement or a compound statement enclosed in
curly braces (that is, a block).
The condition is any expression that returns a boolean value. The else clause is
optional.
The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed.
Example:
public class Example {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
else
System.out.println("b > a");
}
}
Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2
, Module 2: Operators and Control Statements Programming in Java (18CS653)
Nested ifs
A nested if is an if statement that is the target of another if or else.
When you nest ifs, the main thing to remember is that an else statement always refers
to the nearest if statement that is within the same block as the else and that is not
already associated with an else.
Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder.
It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be executed.
Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3
Module 2
Control Statements
Java’s program control statements can be put into the following categories: selection,
iteration, and jump.
Selection statements allow your program to choose different paths of execution based
upon the outcome of an expression or the state of a variable.
Iteration statements enable program execution to repeat one or more statements (that
is, iteration statements form loops).
Jump statements allow your program to execute in a nonlinear fashion.
Java’s Selection Statements
Java supports two selection statements: if and switch.
The if statement
The if statement executes a block of code only if the specified expression is true.
If the value is false, then the if block is skipped and execution continues with the rest
of the program.
You can either have a single statement or a block of code within an if statement.
Note that the conditional expression must be a Boolean expression.
Syntax:
if (<conditional expression>) {
<statements>
}
Example:
public class Example {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)
Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1
,Module 2: Operators and Control Statements Programming in Java (18CS653)
System.out.println("b > a");
}
}
The if else statement
The if statement is Java’s conditional branch statement. It can be used to route
program execution through two different paths.
Here is the general form of the if statement:
Syntax:
if (condition)
statement1;
else statement2;
Here, each statement may be a single statement or a compound statement enclosed in
curly braces (that is, a block).
The condition is any expression that returns a boolean value. The else clause is
optional.
The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed.
Example:
public class Example {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
else
System.out.println("b > a");
}
}
Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2
, Module 2: Operators and Control Statements Programming in Java (18CS653)
Nested ifs
A nested if is an if statement that is the target of another if or else.
When you nest ifs, the main thing to remember is that an else statement always refers
to the nearest if statement that is within the same block as the else and that is not
already associated with an else.
Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder.
It looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be executed.
Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3