Control Structures Learning Competency:: Self-Learning Package in
Control Structures Learning Competency:: Self-Learning Package in
Control Structures Learning Competency:: Self-Learning Package in
SELF-LEARNING PACKAGE IN
Control Structures
Learning Competency:
Writing codes using control structures in programming.
SSP_TLE-CT8AP-IId-m-3.2
Prepared by: Maricar R. Pornel
Ready to Launch!
In the most basic sense, a program is a list of instructions. Control structures are programming
blocks that can change the path we take through those instructions.
In module 3, we discuss the different control structures and how it is use in programming. In this
lesson, we will continue with our lesson in Control structures, and this time we will write simple programs
codes using Java.
Try This!
Direction. Fill in the blanks. Select the correct answer from the words inside the box.
1. The _______________ loop evaluations the condition first and then execute the statements.
2. ___________ statement is where there is more than one possible choices to choose when trying to solve the
problem. Only one process can be carried out.
3. The_____________ loop executes the statements first before evaluating the condition.
4. _________allows the computer to decide between
two or more different courses of action by testing conditions that occur as the program is running.
5. The ________loop allows a statement to be executed a specified a number of times.
FLOWCHART: Problem: The program will display the numbers from 1 to 100.
PROGRAM CODE:
class Main {
public static void main(String[] args) {
int i = 1, n = 99;
while(i <= y) {
System.out.println(l);
i++;
}
}
}
2
Analysis:
1. Are the flow of instructions in the flowchart the same in the program code? Explain. Hint. Review the
flowchart of While loop and Do-while loop.
2. Did you find any error with the program code? Explain.(Hint 1. Observe what is ask in the problem and
compare it with the program code. Hint 2. Observe the variable declarations)
B. SELECTION
1. BINARY SELECTION
In binary selection, we use the if ..Else statement.
If .. Else statement tests the condition. It executes the if block if condition is true otherwise else
block is executed.
Syntax:
if (condition ) {
True branch
statement
statement This is executed
... if the condition
}
else {
False branch
statement
statement This is executed
... if the condition
}
3
Problem. Determine whether to compute for the regular pay or overtime pay based on the hours worked of
the employee. If hours worked is than greater 40 then compute for overtime, otherwise it
will compute for regular pay.
Flowchart
Program Code:
Class Pay
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Input Hours Worked: "
int hours = input.nextInt();
if ( hours > 40)
{
System.out.println ("Compute Overtime Pay");
}
else if (hours < 40)
{
System.out.println("Compute Regular Pay");
}
}
2. Switch Statement
Switch statement tests the value of a variable and compares it with multiple cases. Once the case match is
found, a block of statements associated with that particular case is executed.
Each case in a block of a switch has a different name/number which is referred to as an identifier. The value
provided by the user is compared with all the cases inside the switch block until the match is found.
If a case match is NOT found, then the default statement is executed, and the control goes out of the
switch block. Problem: Write a program that will match the age inputted by the
user and will display the corresponding text message.
Syntax: Age Text to display
switch( expression )
{ 18 You can vote
case value-1:
Block-1; 40 Your middle aged
Break; 60 Consider retirement
case value-2:
Block-2; Age Not in the Error
Break;
case value-n:
Block-n; Flowchart
Break;
default:
Block-1;
Break;
4
Program code
class Age
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter your age:");
int user = in.nextInt();
switch (user) {
case 18: System.out.println("You can Vote!");
break;
case 40:System.out.println("Your Middle Aged!");
break;
case 60:System.out.println("Consider Retirement!");
break;
default: System.out.println("Error");
}
}
}
C. Repetitions
1. While loop Statement How while statement works?
5
Program code
OUTPUT:
Here, is how the program code of while loop works.
1
2 ITERATION VARIABLE CONDITION: ACTION
3
4 i < = n
5 1st i = 1 true 1 is printed
n = 5 1 is increased to 2
2nd i = 2 true 2 is printed
n = 5 1 is increased to 3
3rd i = 3 true 3 is printed
n = 5 1 is increased to 4
4th i = 4 true 4 is printed
n = 5 1 is increased to 5
5th i = 5 true 5 is printed
n = 5 1 is increased to 6
6th i = 6 false The loop is terminated
n = 5
6
2. Do-While loop
The do...while loop is similar to while loop. However, the body of do...while loop is executed once before the
test expression is checked.
How do-while statement works?
Syntax:
do {
// body of loop 1. The body of the loop is executed at first. Then
} while (textExpression) the textExpression is evaluated.
2. If the textExpression evaluates to true, the body of the loop
inside the do statement is executed again.
3. The textExpression is evaluated once again.
4. If the textExpression evaluates to true, the body of the loop
inside the do statement is executed again.
5. This process continues until the textExpression evaluates
class Main {
public static void main(String[] args) {
int i = 1, n = 5;
7
OUTPUT: Here, is how the program code of do- while loop works.
1
ITERATION VARIABLE CONDITION: ACTION
2
3 i < = n
4
i = 1 Not checked 1 is printed
5
n = 5 1 is increased to 2
1st i = 2 true 2 is printed
n = 5 1 is increased to 3
2nd i = 3 true 3 is printed
n = 5 1 is increased to 4
3rd i = 4 true 4 is printed
n = 5 1 is increased to 5
4th i = 5 true 5 is printed
n = 5 1 is increased to 6
5th i = 6 false The loop is terminated
n = 5
3. For Loop
Java for loop is used to run a block of code for a certain number of times.
Syntax:
8
Problem: Write a program that will print a text Hello
Flowchart
5 times.
Program code:
class Main {
public static void main(String[] args) {
Initialize another variable in a separate statement
int n = 5;
Program code: 6
class Main { 7
public static void main(String[] args) { 8
int i = 6, n = 10; 9
while(i <= n) { 10
System.out.println(i);
i++;
}
} HINT. See example of while loop
Table
ITERATION VARIABLE CONDITION ACTION
10
Reflect
Direction. Complete the sentences below.
After you performed the activity above,
1. I learned that __________________________________________________________________________.
2. I realized that __________________________________________________________________________.
3. I hoped that ___________________________________________________________________________.
1 B.. X is 1 C. x is 1 D. None of
A. II. Essay ( 5 points)
2 x is 2 The above
X is 2 1. How do you compare While loop from
3 X is 3 x is 3
Do-while loop?
4 X is 4 x is 4
Program code:
switch (course) {
case 1: System.out.println("Engineering"); break;
case 2: System.out.println("Nursing"); break;
case 3: System.out.println("Architecture"); break;
case 4: System.out.println("Education"); break;
default: System.out.println("Error");
}
3. What will be the output if the user’s input is 8?
A. Education B. Error C. None of the above
4. Which among the following input number will display the output Architecture?
A. 1 b. 2 c. 3 d. 4
11
GLOSSARY:
Operators use in programming
12