0% found this document useful (0 votes)
27 views73 pages

CS 01 Chapter 4

The document describes decision control structures in programming. It defines if and if-else structures that allow selecting and executing code blocks based on boolean conditions. If executes code if the condition is true, while if-else executes one block if true and another if false. Examples show programs that output pass/fail messages for a grade based on if/if-else conditions. The learning outcomes are to evaluate and create programs using decision structures to solve problems.

Uploaded by

Alex Saljay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views73 pages

CS 01 Chapter 4

The document describes decision control structures in programming. It defines if and if-else structures that allow selecting and executing code blocks based on boolean conditions. If executes code if the condition is true, while if-else executes one block if true and another if false. Examples show programs that output pass/fail messages for a grade based on if/if-else conditions. The learning outcomes are to evaluate and create programs using decision structures to solve problems.

Uploaded by

Alex Saljay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

CHAPTER 4

CO N TRO L
S TRU CTU RES
PREPARED BY: DANILYN A. FLORES AND NOR-AINE M. CORPUZ

CS 01 1-2023-2024 1
LEARNING OUTCOMES

1. Evaluate the output of a specific program that is using different control


structures
2. Create an interactive program following the steps of the PDLC, using
Decision Control Structures to solve a specific problem
3. Create an interactive program following the steps of the PDLC, using
Repetition Control Structures to solve a specific problem
4. Create an interactive program that uses different Control Structures to
solve a given problem

CS 01 1-2023-2024 2
CONTROL STRUCTURES

In the examples given in the previous chapters, programs were sequential


wherein statements are executed one after another in a fixed order. Control
Structures allows changing the ordering of how statements in programs are
executed.

CS 01 1-2023-2024 3
DECISION
CONTROL
STRUCTURES
CS 01 1-2023-2024 4
DECISION CONTROL STRUCTURES

These control structures allows selecting and executing specific


blocks of code while skipping other sections.

CS 01 1-2023-2024 5
DECISION CONTROL STRUCTURES
1) if

The if specifies that a statement or block of code will be executed if and only if a
certain condition is true. As shown in the syntax and flowchart in the next slide,
only one of two events can happen:
1. when the condition in the boolean expression is true, the if block will be
executed then proceed to the next lines of code outside the block; or
2. skip the if block and proceed to the next lines of code outside the block.

CS 01 1-2023-2024 6
DECISION CONTROL STRUCTURES

Syntax:
true
boolean_expr
if(boolean_expression) {
statement;
statement1; }

statement

CS 01 1-2023-2024 7
DECISION CONTROL STRUCTURES

The statement in the previous slide’s flowchart is not necessarily a


process. Though it can be a process, it can also be an input or output
or another decision that depends entirely to the analysis of the
problem. And it is not limited to only one. It can be multiple
processes or inputs or outputs or decisions.

CS 01 1-2023-2024 8
DECISION CONTROL STRUCTURES
EXAMPLE 1
Problem: Create a program that will display “Congratulations! You Passed!” if grade is greater
than 60 using if control structure
Program Design grade = 68
START
Problem Analysis
Output: grade>60
true
“Congratulations!”
“You Passed!”
if value of grade > 60
Display “Congratulations!”
Input: None
Process:
Display “You Passed!”
grade = 68 STOP

CS 01 1-2023-2024 9
DECISION CONTROL STRUCTURES
Program Coding Output
public class grade {
public static void main(String []args) { Congratulations!
int grade = 68; You Passed!
if(grade>60) {
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”); }
}}

CS 01 1-2023-2024 10
DECISION CONTROL STRUCTURES
EXAMPLE 2
Problem: Create a program that will display a remarks of “PASSED” if grade >60 using if control
structure
Program Design
Program Analysis
Output: remarks
Input: None
Process:
grade = 68
remarks = “PASSED” if grade >60

CS 01 1-2023-2024 11
DECISION CONTROL STRUCTURES
Program Coding Output
public class grade {
public static void main(String []args) { Remarks: PASSED
int grade = 68;
String remarks = “”;
if(grade>60) {
remarks = “PASSED”; }
System.out.println(“Remarks: ” + remarks);
}}

CS 01 1-2023-2024 12
DECISION CONTROL STRUCTURES
EXAMPLE 3
Problem: Create a program that will display a remarks of “PASSED” if input grade is greater
than 60 using if control structure
Program Design
Program Analysis
Output: remarks
Input: grade
Process:
remarks = “”
remarks = “PASSED” if grade >60

CS 01 1-2023-2024 13
DECISION CONTROL STRUCTURES
Program Coding Output (assuming input value is 95)
import javax.swing.JOptionPane;
public class grade {
public static void main(String [ ]args) { Remarks: PASSED
String remarks = “ ”;
int
grade=Integer.parseInt(JOptioPane.showInp
utDialog(“Please enter grade: “));
if(grade>60) {
remarks = “PASSED”; }
System.out.println(“Remarks: ” + remarks);
}}

CS 01 1-2023-2024 14
DECISION CONTROL STRUCTURES
2) if-else
This is used if a statement or a block should be executed if the condition
is true and a different statement or block is executed if the condition is false. As
shown in the syntax and flowchart in the next slide, only one of two events can
happen:
1. When the condition in the boolean expression is true, the if block will be
executed, skip the else block, then proceed to the next lines of code after the
block; or
2. Skip the if block, execute the else block, then proceed to the next lines of
code after the block.

CS 01 1-2023-2024 15
DECISION CONTROL STRUCTURES
Syntax

if(boolean_expression) {
true false
statement1; boolean_expr
statement2; }
else {
statement3;
statement statement
statement4; }

CS 01 1-2023-2024 16
DECISION CONTROL STRUCTURES

The statements in the previous slide’s flowchart are not necessarily


processes. Though it can be a process, it can also be an input or
output or another decision that depends entirely to the analysis of
the problem. And it is not limited to only one. It can be multiple
processes or inputs or outputs or decisions.

CS 01 1-2023-2024 17
DECISION CONTROL STRUCTURES
EXAMPLE 1
Problem: Create a program that will display “Congratulations! You Passed!” if grade>60
otherwise display “Sorry, you Failed!” using if-else
START grade = 68
Problem Analysis Program Design
Output:
“Congratulations!” and “You Passed!” false
true
grade>60
if the value of grade > 60 or
“Sorry, you Failed”
Input: none Display “Congratulations!” Display “Sorry,
you
Process:
Failed!”
grade = 68
Display “You STOP
Passed!”
CS 01 1-2023-2024 18
DECISION CONTROL STRUCTURES
Program Coding
public class grade { Output
public static void main(String []args) {
int grade = 68; Congratulations!
if(grade>60) { You Passed!
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”); }
else {
System.out.println(“Sorry, you Failed!”); }
}}

CS 01 1-2023-2024 19
DECISION CONTROL STRUCTURES
EXAMPLE 2
Problem: Create a program that will display the remarks based on the value of grade. Remarks
will be “PASSED” if grade is greater than 60, otherwise “FAILED” using if-else

Problem Analysis
Output: remarks
Input: none Program Design
Process:
grade = 68
remarks = “PASSED”
if grade >60 or
remarks = “FAILED”

CS 01 1-2023-2024 20
DECISION CONTROL STRUCTURES
Program Coding
public class grade { Output
public static void main(String []args) {
int grade = 68;
Remarks: PASSED
String remarks = “”;
if(grade>60) {
remarks = “PASSED”; }
else {
remarks = “”FAILED”; }
System.out.println(“Remarks: ” +
remarks);
}}
CS 01 1-2023-2024 21
DECISION CONTROL STRUCTURES
EXAMPLE 3
Problem: Create a program that will display the remarks based on the input value of grade.
Remarks will be “PASSED” if grade is greater than 60, otherwise “FAILED” using if-else
Program Design
Program Analysis
Output: remarks
Input: grade
Process:
remarks = “ ”
remarks = “PASSED”
if grade >60 otherwise
remarks = “FAILED”
CS 01 1-2023-2024 22
DECISION CONTROL STRUCTURES
Program Coding
import javax.swing.JOptionPane; Output (assuming input value is
public class grade { 59.9)
public static void main(String []args) {
String remarks = “”;
Remarks: FAILED
double
grade=Double.parseDouble(JOptionPane.sh
owInputDialog(“Enter student’s grade:”));
if(grade>60) { remarks = “PASSED”; }
else { remarks = “”FAILED”; }
System.out.println(“Remarks: ” + remarks);
}}
CS 01 1-2023-2024 23
DECISION CONTROL STRUCTURES
3) if-else-if
This allows the else clause of an if-else to be another if-else. This allows
making more complex selections. As shown in the next slide, one of 3 events can
happen:
1. When the condition in the 1st boolean expression is true, the if block will be
executed, skip the else if and else blocks, then proceed to the next lines of code
after the blocks;
2. Skip the if block, and when condition in the 2nd boolean expression is true,
execute else if block, skip else block, and proceed to the next lines of code after the
blocks; or
3. Skip if and else if blocks, execute else block and proceed to next lines of code.

CS 01 1-2023-2024 24
DECISION CONTROL STRUCTURES
The number of events will depend on the number of else
ifs.
Syntax:
if(boolean_expression1) { true boolean_expr1
false
statement1;
statement2; }
else if(boolean_expression2) {
statement3; true boolean_expr2
false
statement4; } ...
statement
else {
statement statement
statement5;
statement6; }
CS 01 1-2023-2024 25
DECISION CONTROL STRUCTURES

The statements in the flowchart in the previous slide are not


necessarily processes. Though it can be a process, it can also be an
input or output or another decision that depends entirely to the
analysis of the problem. And it is not limited to only one. It can be
multiple processes or inputs or outputs or decisions.

CS 01 1-2023-2024 26
DECISION CONTROL STRUCTURES
EXAMPLE 1
Problem: Create a program that will display “Congratulations! You Passed!” if the value of grade
is greater than 90,“Good!” if greater than 75, or “Sorry, you Failed!” using if-else-if
Problem Analysis
Output:
“Congratulations! You Passed!” if grade > 90, if not,
“Good!” if grade >75 otherwise
“Sorry, you Failed!”
Input: none
Process:
grade = 68

CS 01 1-2023-2024 27
DECISION CONTROL STRUCTURES
Program Design
START grade = 68

true false
grade>90

Display “Congratulations!”
true grade>75 false

Display “You Passed!”

Display “Good!” Display “Sorry,


you Failed!”

STOP

CS 01 1-2023-2024 28
DECISION CONTROL STRUCTURES
Program Coding
public class grade {
Output
public static void main(String []args) {
int grade = 68;
if(grade>90) { Sorry, you Failed!
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”); }
else if(grade>75) {
System.out.println(“Good!”); }
else {
System.out.println(“Sorry, you Failed!”); }
}}

CS 01 1-2023-2024 29
DECISION CONTROL STRUCTURES
EXAMPLE 2
Problem: Create a program that will determine the remarks based on the grade. “Very
Good!” if grade > 90, if not, “Good!” if grade > 75, otherwise “Better luck next semester” using
if-else-if
Problem Analysis
Output: remarks
Input: none
Process:
grade = 68
remarks = “ ”
remarks = “Very Good!” if grade>90, if not,
remarks = “Good!” if grade>75, if not,
remarks = “Better luck next semester”
CS 01 1-2023-2024 30
DECISION CONTROL STRUCTURES
START grade = 68

Program Design
remarks=””

grade>90
true false

remarks = “Very Good!”

grade>75
true
false

remarks=“Good!” remarks=“Better
luck next
semester”
display remarks

STOP

CS 01 1-2023-2024 31
DECISION CONTROL STRUCTURES
Program Coding
public class grade {
public static void main(String []args) {
Output
int grade = 68;
String remarks = “”; Remarks: Better luck next semester!
if(grade>90) {
remarks = “Very Good!”; }
else if(grade>75) {
remarks = “Good!”; }
else {
remarks = “Better luck next semester!”; }
System.out.println(“Remarks: ” + remarks);
}}
CS 01 1-2023-2024 32
DECISION CONTROL STRUCTURES
EXAMPLE 3
Problem: Create a program that will determine the remarks based on input grade. “Excellent!”
if grade>=95.0, if not, “Very Good!” if grade>=84.5 and <=95.0, if not, “Good!” if grade>=74.5
and <85.0, if not, “Better luck next semester” using if-else-if
Problem Analysis
Output: remarks
Input: grade
Process:
remarks = “ ”
remarks = “Excellent!” if grade>=95.0, if not,
remarks=”Very Good!” if grade>=84.5 and <95.0, if not,
remarks=”Good!” if grade>=74.5 and <85.0 if not,
remarks=”Better luck next time”

CS 01 1-2023-2024 33
DECISION CONTROL STRUCTURES remarks = “ ” START

Program Design get grade

grade>=95.0
true false

remarks = “Excellent!”
(grade<95.0)&&
true (grade>=84.5) false

remarks=“Very Good!”
(grade<85.0)&&
true (grade>=74.5)
false

remarks=“Good!”

display remarks remarks=“Better


luck next
semester!”
STOP

CS 01 1-2023-2024 34
DECISION CONTROL STRUCTURES
Program Coding
import javax.swing.JOptionPane;
public class grade { Output (assuming input grade
public static void main(String []args) { is 96.70)
String remarks = “”;
double grade=Double.parseDouble(JOptionPane.show
InputDialog(“Enter grade: “)); Remarks: Excellent!
if(grade>=95) {
remarks = “Excellent!”; }
else if((grade<95) && (grade>=84.5)) {
remarks = “Very Good!”; }
else if((grade<85) && (grade>=74.5)) {
remarks = “Good!”; }
else {
remarks = “Better luck next semester!”; }
System.out.println(“Remarks: ” + remarks); } }
CS 01 1-2023-2024 35
DECISION CONTROL STRUCTURES
4) switch
The switch executes the statement or block if and only if the value in the
switch expression is equal or matches to a specific case. If none of the cases are
satisfied, the default block is executed.
The value in the switch expression should only be of either data type
int or char. The case selectors should also match the data type of the switch
expression.

CS 01 1-2023-2024 36
DECISION CONTROL STRUCTURES

As shown in the syntax and flowchart in the next slide, the int
or char value of the variable in the switch_expression will be
compared to the cases. When a case is a match, the block in that case
will be executed and all other blocks in the switch will be skipped. If
none of the cases matched, the block of the default will be executed.

CS 01 1-2023-2024 37
DECISION CONTROL STRUCTURES
Syntax: switch expression

switch(switch_expression) {
true block 1 statements
case case_selector1: case_1
statement1;
statement2;
break;
true block 2 statements
case case_selector2: case_2
statement3;
statement4;
break;
default: default block statements

statement1;
statement2; }
CS 01 1-2023-2024 38
DECISION CONTROL STRUCTURES
EXAMPLE 1
Problem: Create a program that will display “Excellent” for a grade of 100, “Very Good” for 90, “Good” for
80, or “Failed” otherwise using switch.
Problem Analysis Program Design START grade = 92 switch(grade)

Input: none
Display “Excellent!” case 100
Process: grade = 92 true
Output:
“Excellent” if 100 case 90
Display “Very Good!” true
“Very good” if 90
“Good” if 80
“Failed” otherwise Display “Good!” case 80
true

STOP Display “Failed!”


CS 01 1-2023-2024 39
DECISION CONTROL STRUCTURES
Program Coding
public class Grade {
public static void main(String []args) { Output
int grade = 92;
switch(grade) {
case 100: Failed!
System.out.println(“Excellent!”);
break;
case 90: * The output is Failed! because none of the cases
System.out.println(“Very Good”); match the value of grade
break;
case 80:
System.out.println(“Good!”);
break;
default:
System.out.println(“Failed!”); } } }
CS 01 1-2023-2024 40
DECISION CONTROL STRUCTURES
EXAMPLE 2
Problem: Create a program that will assign and display a remarks of “Excellent” for a grade of
100,“Very Good” for 90,“Good” for 80, or “Failed” otherwise using switch.
remarks=”” grade = 90 switch(grade)
Problem Analysis Program Design START

Input: none
remarks = “Excellent!” case 100
Process: true

grade = 90
remarks = “Very Good!” case 90
remarks = “Excellent” if grade is 100 true
remarks = “Very Good” if grade is 90
remarks = “Good” if grade is 80 remarks = “Good!” case 80
true
remarks = “Failed” otherwise
remarks = “Failed!”
Output: remarks display remarks

STOP
CS 01 1-2023-2024 41
DECISION CONTROL STRUCTURES
Program Coding
public class Grade {
public static void main(String []args) {
int grade = 90; Output
String remarks = “”;
switch(grade){
case 100: Remarks: Very Good
remarks = “Excellent”;
break;
case 90:
remarks = “Very Good”;
break;
case 80:
remarks = “Good”;
break;
default:
remarks = “Failed”; }
System.out.println(“Remarks: ” + remarks); } }
CS 01 1-2023-2024 42
LESSON ACTIVITY 4.1

1. Create a simple program following the steps


of the PDLC that will prompt for any whole
number input from 0-10 and output into
words the input number using any decision
control structure

Output “Error: Number not in range.” for


numbers not 0-10.

CS 01 1-2023-2024 43
LESSON ACTIVITY 4.1
2. Modify your program in Lesson Activity 3.2 number 2 to include a decision control structure
that will determine the equivalent of the student’s grade using the table below:

Grade Range Equivalent Grade Range Equivalent Grade Range Equivalent

0-69 5.0 81-83 2.50 93-95 1.50

70-74 4.0 84-86 2.25 96-98 1.25

75-77 3.0 87-89 2.0 99-100 1.0

78-80 2.75 90-92 1.75

CS 01 1-2023-2024 44
REPETITION
CONTROL
STRUCTURES
CS 01 1-2023-2024 45
REPETITION CONTROL STRUCTURES

Repetition Control Structures allow executing specific


blocks of codes to repeat a number of times.

CS 01 1-2023-2024 46
REPETITION CONTROL STRUCTURES
1) while
In a while loop, the statements inside the block are executed repeatedly
as long as the boolean expression evaluates to true.
Syntax:

while(boolean_expression) { boolean_expr

statement1; false

statement2; true

...
statements
increment/decrement; }
CS 01 1-2023-2024 47
REPETITION CONTROL STRUCTURES

The statements can be a variable declaration, a prompt for an


input, a process or an output. Also, the number of statements inside a
loop block always depends on the requirements of the problem being
solved.

CS 01 1-2023-2024 48
REPETITION CONTROL STRUCTURES
EXAMPLE 1
Problem: Create a program that displays 4 numbers in descending order
vertically using a loop.
Problem Analysis Program Design
Input: none Start i=4
Process:
i=4 i>0 Stop
i-- false
true
Output:
i while i is greater than 0 Display i

i--

CS 01 1-2023-2024 49
REPETITION CONTROL STRUCTURES
Program Coding Output

public class while_i { 4


public static void main(String []args){ 3
int i = 4; 2
while(i > 0) { 1
System.out.println(i);
i--; } } }

CS 01 1-2023-2024 50
REPETITION CONTROL STRUCTURES
EXAMPLE 2
Problem: Create a program that displays 10 numbers in ascending order
horizontally using a loop. Program Design
Problem Analysis
display x x++
Input: none
Process: true

x=0 x=0 x<10


Start
x++
Output: false

x while x is less than 10


Stop

CS 01 1-2023-2024 51
REPETITION CONTROL STRUCTURES
Program Coding
public class while_x { Output:
public static void main(String []args){ 0 1 2 3 4 5 6 7 8 9
int x = 0;
while(x < 10) {
System.out.print(x + “ “);
x++; } } }

CS 01 1-2023-2024 52
REPETITION CONTROL STRUCTURES
EXAMPLE 3
Problem: Create a program that will display Hello 4 times using a loop.
Problem Analysis Program Design
Display “Hello” x++
Input: none
Process:
true
x=1
x++ x =1 x<5
Output: Start
“Hello” while x is less than 5
false

Stop

CS 01 1-2023-2024 53
REPETITION CONTROL STRUCTURES
Program Coding Output
public class while_hello{
Hello
public static void main(String []args){
Hello
int x = 1; Hello
while(x < 5) { Hello
System.out.println(“Hello”);
x++; } } }

CS 01 1-2023-2024 54
REPETITION CONTROL STRUCTURES
2) do-while
In a do-while loop, the statement/s inside the block is/are executed
at least once and then repeated several times as long as the boolean
expression is true.
Syntax:
statements
do {
statement1;
statement2;
true
... boolean_expr
false

increment/decrement; }
while(boolean_expr);
CS 01 1-2023-2024 55
REPETITION CONTROL STRUCTURES

The statements can be a variable declaration, a prompt for an


input, a process or an output. Also, the number of statements inside a
loop block always depends on the requirements of the problem being
solved.

CS 01 1-2023-2024 56
REPETITION CONTROL STRUCTURES
EXAMPLE 1
Problem: Display 5 numbers vertically in ascending order using a loop.
Problem Analysis Program Design
Input: none x=0
Start
Process:
x =0
x++ Display x
Output:
x while x is less than 5
x++

true false
X<5 Stop

CS 01 1-2023-2024 57
REPETITION CONTROL STRUCTURES
Program Coding Output

public class do_while_1{ 0


public static void main(String []args){ 1
int x = 0; 2
do { 3
System.out.println(x); 4
x++; }
while(x<5); } }

CS 01 1-2023-2024 58
REPETITION CONTROL STRUCTURES
EXAMPLE 2
Problem: Display a word as many times as the input number.
Problem analysis Program Design
Input: loop
Process:
x=0
x++
Output:
“Programming” as long as x is less than loop

CS 01 1-2023-2024 59
REPETITION CONTROL STRUCTURES
Program Coding
import javax.swing.JOptionPane;
Output (assuming
public class do_while_2{
input is 3)
public static void main(String []args){
int x = 0;
int loop = Programming
Integer.parseInt(JOptionPane.showInputDialog(“Enter a
number for the loop:”)); Programming
do { Programming
System.out.println(“Programming”);
x++; }
while(x<loop);
}}
CS 01 1-2023-2024 60
REPETITION CONTROL STRUCTURES
3) for
Like the previous loops, the for loop allows for execution of statements a
number of times as long as the boolean expression or condition is true.
Syntax
for(InitializationExpression; LoopCondition; StepExpression) {
statement1;
statement2;
... }
where,
InitializationExpression – initializes the loop variable
LoopCondition – compares the loop variable to some limit value
StepExpression – updates the loop variable
CS 01 1-2023-2024 61
REPETITION CONTROL STRUCTURES
false

Initialization_expr Loop_cond Step_expr

true

statements

CS 01 1-2023-2024 62
REPETITION CONTROL STRUCTURES
EXAMPLE 1
Problem: Display 10 numbers in ascending order vertically using a loop.
Problem Analysis Program Design
Input: none Stop

Process:
false
i=0
i++
i=0 i < 10 i ++
Output: Start

i while i is less than 10


true

Display i

CS 01 1-2023-2024 63
REPETITION CONTROL STRUCTURES
Program Coding Output:
0
1
public class for_1{ 2
public static void main(String [] args){ 3
for(int i = 0; i < 10; i++) { 4
5
System.out.println(i); } 6
}} 7
8
9

CS 01 1-2023-2024 64
REPETITION CONTROL STRUCTURES
EXAMPLE 2
Problem: Display a message a number of times as the input number

Problem Analysis:
Input: loop
Process:
loop = o
message = “Countdown”
z=loop
z--
Output:
message + “ “ + z while z is greater than 0 then
“End Countdown.”
CS 01 1-2023-2024 65
REPETITION CONTROL STRUCTURES
Program Design

Start loop=0 message = “Countdown” get loop z=loop

true
z-- display message + “ “ + z z>0

false

Stop display “End Countdown.”

CS 01 1-2023-2024 66
REPETITION CONTROL STRUCTURES
Program Coding
import javax.swing.JOptionPane;
public class Looops{
public static void main(String []args) {
int loop = 0;
String message = "Countdown";
loop = Integer.parseInt(JOptionPane.showInputDialog("Loop how many
times?"));

for(int z = loop; z>0; z--){


JOptionPane.showMessageDialog(null, message + "… "+z);
}
JOptionPane.showMessageDialog(null, "End Countdown."); } }
CS 01 1-2023-2024 67
REPETITION CONTROL STRUCTURES
Output (assuming input is 3)

CS 01 1-2023-2024 68
LESSON ACTIVITY 4.2
1. Create a flowchart for the code given below.
import javax.swing.JOptionPane;
public class Power{
public static void main (String[] args) {
int num = 0;
int pow = 0;
int numpow = 0;
String label = "";
num = Integer.parseInt(JOptionPane.showInputDialog("Enter a whole number:"));
pow = Integer.parseInt(JOptionPane.showInputDialog("Enter Power (also another whole number):"));
numpow = num;
for(int i = 1; i<pow; i++) {
numpow*=num; }
label = num + " to the power of "+ pow +" = ";
JOptionPane.showMessageDialog(null, label + numpow); } }

CS 01 1-2023-2024 69
LESSON ACTIVITY 4.2
2. Create a simple program (flowchart and source code only) that will
prompt for a name and will display that name 100 times using:
a. while
b. do-while
c. for
3. Modify your Lesson Activity 4.1 number 2 to include a repetition control
structure that will prompt for the grades of 5 students and display the
information of the students like a class record.
4. Modify your Lesson Activity 3.2 number 3 to include a repetition control
structure that will prompt for the details of 5 employees and will display the
information of the employees like a payroll.

CS 01 1-2023-2024 70
SUMMARY
 Control structures are used in order to make more complex programs
 Decision control structures if, if-else, if-else-if, and switch are used to
create programs that can change the order in which lines of code are
executed
 Repetition control structures while, do-while, and for loops can be used to
repeat the execution of certain lines of code.
 Choosing the right control structure is the trick to creating good
programs

CS 01 1-2023-2024 71
REFERENCES

[1] Burd, B. (2017). Beginning Programming with Java For Dummies (5th Ed). New Jersey: John Wiley
and Sons, Inc.
[2] Cavida, D.G, Frio, F.M., Flores, D. (2010). Computer Programming I. Unpublished Workbook,
University of Southern Mindanao, Kabacan, Cotabato.
[3] Flask, R. (ND). Java for Beginners 2nd Edition [PDF File]. Retrieved from
http://staff.um.edu.mt/__data/assets/pdf_file/0010/57169/jn.pdf
[4] Mayfield, B. (2016). From Problem Analysis to Program Design Lab Manual (3rd ed.). Philippines:
Cengage Learning Asia Pte Ltd.

CS 01 1-2023-2024 72
THANK YOU!
Stay safe. #WeHealAsOne

CS 01 1-2023-2024 73

You might also like