Condition Statement
Decision makingstructures have one or
more conditions to be evaluated or tested
by the program, along with a statement or
statements that are to be executed if the
condition is determined to be true, and
optionally, other statements to be executed
if the condition is determined to be false.
Following is the general form of a typical
decision making structure found in most of
the programming languages −
3.
Condition Statement
Sr.No. Statement& Description
1 if statement
An if statement consists of a boolean expression followed by one or more
statements.
2 if...else statement
An if statement can be followed by an optional else statement, which executes
when the boolean expression is false.
3 nested if statement
You can use one if or else if statement inside another if or else if statement(s).
4 switch statement
A switch statement allows a variable to be tested for equality against a list of
values.
4.
The if Statement
Theif statement in Java checks a Boolean expression and executes a
specific block of code only if the condition is true.
5.
The if-else Statement
Theif-else statement allows Java programs to handle both true and
false conditions. If the condition inside the if statement evaluates to
false, the else block is executed instead.
Using if-else statements in Java improves decision-making in programs
by executing different code paths based on conditions.
Syntax of if-else Statement
Following is the syntax of an if...else statement −
Ladder if (ifelse if) Statements
The if...else if...else statement is used for executing multiple code
blocks based on the given conditions (Boolean expressions).
An if statement can be followed by an optional else if...else statement,
which is very useful to test various conditions using a single if...else if
statement.
Syntax -
9.
Ladder if (ifelse if) Statements
Points to Remember
● When using if-else if- else statements there are a few points to
keep in mind.
● An if can have zero or one else's and it must come after any else
if's.
● An if can have zero to many else if's and they must come before
the else.
● Once an else if succeeds, none of the remaining else if's or else's
will be tested.
Nested if-else Statement
Thenested if else statement is used for better decision-making when
other conditions are to be checked when a given condition is true. In
the nested if else statement, you can have an if-else statement block
the another if (or, else) block.
Syntax -
Rules for Usingif-else Conditions
The if-else statement in Java follows specific rules to ensure proper execution.
Below are the key rules:
● Boolean Expression Required: The condition inside an if statement must
be a Boolean expression that evaluates to true or false.
● Curly Braces for Multiple Statements: If there is more than one
statement inside if or else, enclose them within { } curly braces.
● Single Statement Optional Braces: If there is only one statement inside if
or else, curly braces are optional but recommended for better
readability.
● Else is Optional: The else block is not mandatory; an if statement can
exist without an else.
● Else Without If is Invalid: The else statement must always be associated
with a preceding if statement.
15.
Rules for Usingif-else Conditions
● Nested if-else Allowed: You can place an if or else if statement
inside another if statement (nested if-else).
● Multiple else-if Conditions: The else if condition can be used
multiple times before the final else statement.
● Execution Order: Java executes the if-else conditions from top to
bottom, and once a condition is met, the remaining conditions are
ignored.
16.
Java switch Statement
Javaswitch statement allows a variable to be tested for equality
against a list of values. Each value is called a case, and the variable
being switched on is checked for each case.
The switch statement can be used when multiple if-else statements
are required. It can have multiple code blocks along with the case
values and execute one of many code blocks based on the matched
case value.
Rules for Usingswitch Statement
● The variables used in a switch statement can only be integers,
convertable integers (byte, short, char), strings, and enums.
● You can have any number of case statements within a switch. Each
case is followed by the value to be compared to and a colon.
● The value for a case must be the same data type as the variable in
the switch, and it must be a constant or a literal.
● When the variable being switched on is equal to a case, the
statements following that case will execute until a break
statement is reached.
● When a break statement is reached, the switch terminates, and
the flow of control jumps to the next line following the switch
statement.
20.
Rules for Usingswitch Statement
● Not every case needs to contain a break. If no break appears, the
flow of control will fall through to subsequent cases until a break
is reached.
● A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is
needed in the default case.
21.
Java - LoopControl
When Loops are Required?
There may be a situation when you need to execute a block of code
several number of times. In general, statements are executed
sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow
for more complicated execution paths.
22.
Java - LoopControl
Loop Statement
A loop statement allows us to
execute a statement or group of
statements multiple times and
following is the general form of a
loop statement in most of the
programming languages −
23.
Java - LoopControl
Java programming language provides the following types of loops to handle the
looping requirements:
1. while loop
Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
2. do...while loop
Like a while statement, except that it tests the condition at the end of the loop
body.
3. for loop
Execute a sequence of statements multiple times and abbreviates the code
that manages the loop variable.
4. Enhanced for loop
As of Java 5, the enhanced for loop was introduced. This is mainly used to
traverse collection of elements including arrays.
24.
Java while Loop
Javawhile loop statement repeatedly executes a code block as long as
a given condition is true.
The while loop is an entry control loop, where conditions are checked
before executing the loop's body.
Syntax of while Loop
The syntax of a while loop is −
25.
Java while Loop
ExecutionProcess of a while Loop
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value.
When executing, if the boolean_expression result is true, then the
actions inside the loop will be executed. This will continue as long as
the expression result is true.
When the condition becomes false, program control passes to the line
immediately following the loop.
26.
Java while Loop
FlowDiagram
Here, key point of the while loop
is that the loop might not ever
run. When the expression is
tested and the result is false, the
loop body will be skipped and the
first statement after the while
loop will be executed.
Infinite While Loop
ExamplesOutput
In this example, we're
showing the infinite
loop using while loop. It
will keep printing the
numbers until you
press ctrl+c to
terminate the program.
29.
Infinite While Loop
ExamplesOutput
In this example, we're
showing the infinite
loop using while loop. It
will keep printing the
numbers until you
press ctrl+c to
terminate the program.
30.
Java do whileLoop
Java do while loop is similar to a while loop, except that a do while
loop is guaranteed to execute at least one time.
The do-while loop is an exit control loop where the condition is
checked after executing the loop's body.
Syntax of do while Loop
Following is the syntax of a do...while loop −
31.
Java do whileLoop
Execution Process of a do while Loop
Notice that the Boolean expression appears at the end of the loop, so
the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the control jumps back up to do
statement, and the statements in the loop execute again. This process
repeats until the Boolean expression is false.
Java - forLoop
In Java, a for loop is a repetition control structure used to execute a block of
code a specific number of times. It is particularly useful when the number of
iterations is known beforehand, making it an efficient way to automate
repetitive tasks.
For loops are used in Java for tasks such as iterating over arrays, performing
calculations, and handling repetitive operations.
The Java for loop is an entry control loop, meaning it checks the given
condition before executing the loop body. Its structured syntax makes it
more readable and concise compared to other loop structures like the while
loop.
Syntax
The syntax of a for loop is −
Structure of forLoop
1. Initialization
Defines and initializes the loop counter variable. This part executes
only once at the start.
2. Boolean Expression (Condition)
Evaluates before each iteration. If the condition evaluates to true, the
loop executes; if false, it terminates.
38.
Structure of forLoop
3. Body
Contains the statements that execute repeatedly as long as the
Boolean condition remains true. This section may also include an
update to the loop counter.
4. Update
Modifies the loop counter at the end of each iteration.
39.
Execution Process ofa for Loop
● The initialization step is executed first, and only once. This step allows you to
declare and initialize any loop control variables and this step ends with a
semi colon (;).
● Next, the Boolean expression is evaluated. If it is true, the body of the loop
is executed. If it is false, the body of the loop will not be executed and
control jumps to the next statement past the for loop.
● After the body of the for loop gets executed, the control jumps back up to
the update statement. This statement allows you to update any loop control
variables. This statement can be left blank with a semicolon at the end.
● The Boolean expression is now evaluated again. If it is true, the loop
executes and the process repeats (body of loop, then update step, then
Boolean expression). After the Boolean expression is false, the for loop
terminates.
40.
Nested for Loops
Anested for loop is a for loop containing another for loop inside it.
Example
Output
41.
Java - breakStatement
The Java break statement is used to exit a loop immediately and
transfer control to the next statement after the loop. It has two main
usages: when encountered inside a loop, it terminates the loop
instantly, and in a switch statement, it is used to exit a specific case
(covered in the next chapter).
Syntax of break Statement
The syntax of a break is a single statement inside any loop or switch
case −
Java - continueStatement
The continue statement can be used in any loop control structure to
skip the current iteration and jump to the next one. In a for loop, it
immediately transfers control to the update statement, while in a
while or do-while loop, it jumps directly to the Boolean expression for
the next iteration.
Syntax of continue Statement
The syntax of a continue is a single statement inside any loop −