Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 64

Control Structure

Instructor: Ellen M. Guiñares


CONTROL STRUCTURE

These are programming block that


can change the path we take through those
instructions or not. It is a syntactic form in a
programming language to express flow of
control.
CONTROL STRUCTURE

• Program is written using simple control


structure to determine the flow of program
execution.
• There are three types of control structure:
1. Sequential
2. Selection (Conditional Control Statements)
3. Repetition (Loops)
01
SEQUENTIAL
Sequential Structure

• The sequence structure is built into Java. Unless


directed otherwise, the computer executes Java
statements one after the other in the order in
which they’re written—that is, in sequence.
Sequential Structure
Sequential Structure
package typeControlStructures;
import java.util.Scanner;
public class sequential {

public static void main(String[] args) {

int age;
Scanner input = new Scanner(System.in);

System.out.print("Enter person's age: ");


age = input.nextInt();
System.out.print("Inputted person's age is: " +age);
}
}
02
SELECTION
Selection Structure

• Also known as a conditional structure, decision


making statement
• They are used to select a part of the program to
be executed based on a condition.
• They allow choosing between two or more paths
for execution of any particular program.
Selection Structure

• if
• nested if
• if-else
• if-else-if statement/ ladder if statements
• switch statement
Selection Structure
• Within a method, we can alter the flow of
control (the order in which statements are
executed) using either conditionals or loops.
• The conditional statements if, if-else, and switch
allow us to choose which statement will be
executed next.
• Each choice or decision is based on the value of
boolean expression (also called the condition).
Selection Structure -
if

• If statement consists a condition, followed by


statement or a set of statements as shown below:

if(condition){
Statement(s);
}
Selection Structure -
if
• The statements 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;

public class ifStatement {

public static void main(String[] args) {


int a = 5;
int b = 4;
// Evaluating the expression that will return true or false
if (a > b) {
System.out.println("a is greater than b");
}
}
}

Output: a is greater than b


Selection Structure -
if

Output: number is less than 100


Selection Structure – nested if

• When there is an if if(condition_1) {


statement inside another Statement1(s);
if statement then it is
called the nested if if(condition_2) {
statement. Statement2(s);
}
}
Selection Structure – nested if
Selection Structure – nested if
package typeControlStructures;

public class nestedifStatement {

public static void main(String[] args) {

int age = 20;


boolean hasVoterCard = true;
// Evaluating the expression that will return true or false
if (age > 18)
{
// If outer condition is true then this condition will be
check
if (hasVoterCard)
{
System.out.println("You are Eligible");
}
}
}
}
Output: You are Eligible
Selection Structure – nested if

Output: number is less than 100


number is greater than 50
Selection Structure – if -
else
• If we want to choose between two alternative we
use the if/else statement:

if(condition) {
Statement(s);
}
else {
Statement(s);
}
Selection Structure – if -
else
Selection Structure – if -
else
Selection Structure – if-else
package typeControlStructures;

public class ifelseStatement {

public static void main(String[] args) {


int a = 10;
int b = 50;
// Evaluating the expression that will return true or false
if (a > b)
{
System.out.println("a is greater than b");
}
else
{
System.out.println("b is greater than a");
}
}
}
Output: b is greater than a
Selection Structure – if-else

Output: num is greater than or equal 50


Selection Structure – if – else -if

• In Java, the if-else-if ladder statement is used for


testing conditions. It is used for testing one
condition from multiple statements.
• In this statement we have only one “if” and one
“else”, however we can have multiple “else if”. It is
also known as if else if ladder.
Selection Structure – if – else -if
if(condition_1) {
/*if condition_1 is true execute this*/
statement(s);
}
else if(condition_2) {
/* execute this if condition_1 is not met and
* condition_2 is met
*/
statement(s);
}
else if(condition_3) {
/* execute this if condition_1 & condition_2 are
* not met and condition_3 is met
*/
statement(s);
}
else {
/* if none of the condition is true
* then these statements gets executed
*/
statement(s);
}
Selection Structure – if-else-if

Output: The age of person is : 15 He/she is a child.


This statement is outside of the if-else-if
Selection Structure – if-else-if

Output: Its a four digit number


Selection Structure – switch

• The switch statement is like the if-else-if ladder


statement. To reduce the code complexity of the if-
else-if ladder switch statement comes.
• In a switch, the statement executes one statement
from multiple statements based on condition. In the
switch statements, we have a number of choices and
we can perform a different task for each choice.
Selection Structure – switch

• Switch case statement is used when we have number of


options (or choices) and we may need to perform a
different task for each choice.
Selection Structure – switch
switch (variable or an integer expression)
{
case constant:
//Java code
; Note: Switch Case statement is mostly
case constant: used with break statement even though it
//Java code is optional. We will first see an example
; without break statement and then we will
default: discuss switch case with break.
//Java code
;
}
Selection Structure – switch

Output: Default: Value is: 2


Selection Structure – switch

Output: Case4: Value is: 2


Selection Structure – switch

• Switch Case Flow


Diagram
Selection Structure – break

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
Valid expression for switch
given inside switch
should result in a switch(1+2+23)
switch(1*2+3%4)
constant value
otherwise it would not Invalid Switch Expression
be valid.
switch(ab+cd)
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.

6. The case value must be literal or constant. The case value


type should be a type of expression.

7. Each case should be unique. If you create duplicate case


value, it will throw compile-time error.
03
REPITITION
Repetition Structure

Repetition structures, or loops, are used when a program


needs to repeatedly process one or more instructions until
some condition is met, at which time the loop ends.
There are three looping statements:
 For loop
 While loop
 Do while loop
Repetition Structure For loops
• For loop is used to execute a set of statements repeatedly until a
particular condition returns false.
• Java for loop is used to run a block of code for a certain number
of times. The syntax of for loop is:

for (initial Expression; test Expression; update Expression) {


// body of the loop
}
Repetition Structure For loops

The syntax of for loop is:

for(initialization; condition ; increment/decrement)


{
statement(s);
}
Repetition Structure For loops
for(initialization; condition ; increment/decrement)
{
statement(s);
}
• Initialization: In the initialization part, variables like loop
counter (you will generally see i and j in loops, these are the
loop counters) are initialized. This is an optional part of the loop
as the variables can be initialized before the loop. This executes
only once when the loop starts.
Repetition Structure For loops
for(initialization; condition ; increment/decrement)
{
statement(s);
}

• Condition: This is one of the important part of the loop. This


condition determines till when the loop should keep repeating.
The loop keeps repeating until the condition becomes false.
Repetition Structure For loops
for(initialization; condition ; increment/decrement)
{
statement(s);
}
• Increment/Decrement: In this part of the loop declaration, you
can specify the increment or decrement of loop counter. This is
to modify the loop counter value so that at one point condition
becomes false and the loop ends.
• Statement: The statements inside the loop body keeps
executing for each iteration of the loop until the loop stops.
Flow of execution For Loops
First step: In for loop,
initialization happens
first and only one
time, which means
that the initialization
part of for loop only
executes once.
Flow of execution For Loops
Second step:
Condition in for loop
is evaluated on each
iteration, if the
condition is true then
the statements inside
for loop body gets
executed.
Flow of execution For Loops
Third step: After
every execution of for
loop’s body, the
increment/decrement
part of for loop
executes that updates
the loop counter.
Flow of execution For Loops

Fourth step: After


third step, the control
jumps to second step
and condition is re-
evaluated.
Repetition Structure – For Loop

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 – While Loop
1. Test Expression: In this expression,
we have to test the 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
EVEN NUMBERS: Activity
1. Write a for loop that displays the even numbers between 23
and 42.

GRADES:
2. Get three exam grades from the user and compute the average
of the grades. Output the average of the three exams. Together
with the average, also include a smiley face in the output if the
average is greater than or equal to 60,
otherwise output ☹
• Use Scanner to get input from the user, and System.out to
output the result.
Activity
NUMBER IN WORDS

1. Get a number as input from the user, then output the


equivalent of the number in words. The number inputted
should
range from 1-10. If the user inputs a number that is not in
the range, output, “invalid number”.
• Use an if-else statement to solve this problem.
• Use a switch statement to solve this problem.
Activity
HUNDRED TIMES
1. Create a program that prints your name a hundred
times. Do three versions of this program using a while
loop, a do-while loop and a for-loop.

POWERS
1. Compute the power of a number given the base and
exponent. Do three version of this program using a while
loop, a
do-while loop and a for loop.

You might also like