Control Structure in Java
Control Structure in Java
Structure
Instructor: Russel R. Guiñares
CONTROL STRUCTURE
int age;
Scanner input = new Scanner(System.in);
if(condition){
Statement(s);
}
Selection
•Structure
The statements
- if
gets executed only
when the given
condition is true. If
the condition is
false then the
statements inside
if statement body
are completely
ignored.
Selection
Structure - if
package typeControlStructures;
if(condition) {
Statement(s);
}
else {
Statement(s);
}
Selection Structure –
if - else
Selection Structure –
if - else
Selection Structure –
if-else
package typeControlStructures;
Output:
Case2
Case3
Case4
Default
Selection Structure –
break
• Break statements are used when you want
your program-flow to come out of the switch
body. Whenever a break statement is
encountered in the switch body, the
execution flow would directly come out of
the switch, ignoring rest of the cases.
Selection Structure – switch
with break
Output:
Case2
Few point about Switch
Case
1. Case doesn’t always need to have order 1,
2, 3 and so on. It can have any integer
value after case keyword. Also, case
doesn’t need to be in an ascending order
always, you can specify them in any order
based on the requirement.
Few point about Switch
Case
2. You can also
use characters in
switch case.
Few point about Switch
Case
3. The expression
given inside
Valid expression for switch
switch should
result in a switch(1+2+23)
switch(1*2+3%4)
constant value
otherwise it Invalid Switch Expression
would not be
switch(ab+cd)
valid. switch(a+b+c)
Few point about Switch
Case
4. Nesting of switch statements are allowed,
which means you can have switch statements
inside another switch. However nested switch
statements should be avoided as it makes
program more complex and less readable.
Few point about Switch
Case
5. You can create any number of cases in switch
statement.
Output:
Iteration number: 1
Iteration number: 2
Iteration number: 3
Repetition Structure –
For Loop
Output:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2
Repetition Structure While
Loop
• is a control flow statement that allows code to be
executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a
repeating if statement. While loop in Java comes
into use when we need to repeatedly execute a
block of statements. The while loop is considered
as a repeating if statement. If the number of
iterations is not fixed, it is recommended to use
the while loop.
Repetition Structure –
While Loop
Syntax:
while (test_expression)
{
// statements
update_expression;
}
Repetition Structure –
1. Test Expression: In this
expression, we have to While
test the Loop
condition. If the condition
evaluates to true then we will
execute the body of the loop
and go to update expression.
Otherwise, we will exit from the
while loop.
Example:
i <= 10
Repetition Structure –
While Loop
2. Update Expression: After
executing the loop body, this
expression
increments/decrements the loop
variable by some value.
Example:
i++;
Repetition Structure –
While Loop