Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Overig

Precise,easy to understand ,textbook based

Beoordeling
-
Verkocht
-
Pagina's
13
Geüpload op
25-02-2026
Geschreven in
2025/2026

This is JAVA notes

Instelling
Vak

Voorbeeld van de inhoud

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

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
25 februari 2026
Aantal pagina's
13
Geschreven in
2025/2026
Type
OVERIG
Persoon
Onbekend

Onderwerpen

€9,74
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
yashaswinir

Maak kennis met de verkoper

Seller avatar
yashaswinir
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
2 maanden
Aantal volgers
0
Documenten
4
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen