Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

Precise,easy to understand ,textbook based

Rating
-
Sold
-
Pages
13
Uploaded on
25-02-2026
Written in
2025/2026

This is JAVA notes

Institution
Course

Content preview

Object Oriented Programming with JAVA BCS306A

MODULE-I
CHAPTER 4 Control Statements
➢ A programming language uses control statements to cause the flow of execution to advance and
branch based on changes to the state of a program. 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. Jump statements allow your program to execute in
a nonlinear fashion.

4.1 Java’s Selection Statements
➢ Java supports two selection statements: if and switch.
➢ These statements allow you to control the flow of your program’s execution based upon
conditions known only during runtime.

1. if
➢ The if statement is Java’s conditional branch statement.
➢ The if statement can be used to route program execution through two different paths.
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. If the condition is true, then statement1 is executed. Otherwise, statement2 (if
it exists) is executed. In no case will both statements be executed.
Example:
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero. In no case are
they both set to zero.

➢ Most often, the expression used to control the if will involve the relational operators. However,
this is not technically necessary. It is possible to control the if using a single boolean variable, as
shown in this code fragment:
boolean dataAvailable;
// ...
if (dataAvailable)
ProcessData();
else
waitForMoreData();
➢ Remember, only one statement can appear directly after the if or the else. If you want to include
more statements, you’ll need to create a block, as in this fragment:
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else {

1

, Object Oriented Programming with JAVA BCS306A
waitForMoreData();
bytesAvailable = n;
}

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");
}
}

2. 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)
➢ As the comments indicate, the final else is not associated with if (j<20) because it is not in the
same block (even though it is the nearest if without an else). Rather, the final else is associated
with if(i==10). The inner else refers to if(k>100) because it is the closest if within the same
block.

3. 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.



2

Written for

Institution
Course

Document information

Uploaded on
February 25, 2026
Number of pages
13
Written in
2025/2026
Type
OTHER
Person
Unknown

Subjects

$10.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
yashaswinir

Get to know the seller

Seller avatar
yashaswinir
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 months
Number of followers
0
Documents
4
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions