You are on page 1of 8

COMPROG2: ACTIVITY 2- REVIEW IN

CONTROL STRUCTURES AND


ARRAYS THROWBACK

WEEK 2 DATE AUGUST 29 –


SEPTEMBER 2, 2022

SECTION II-A NETAD SCORE

OVERVIEW

This throwback activity is to ensure students have prior knowledge about control
structures and arrays which should have been learned during their Computer
Programming 1.

LEARNING OUTCOMES

1. Differentiate if, if else, if else if, and switch by writing down the syntax for each.
2. Explain when to use different control structures by providing scenarios.
3. Discuss the differences between looping/repetition structures through
syntax.
4. Apply control and looping structures in programming.

MATERIAL/S

PC/laptop or mobile with any Java compiler


Yellow paper or MS Word document
Google Meet
PROCEDURES

1. In Microsoft word, type the syntax for control structures: if, if else, if else if,
and switch; and looping structures: while, do while, and for loop.
2. Provide sample programs for each structure and briefly explain the program
flow.
1.
 Control Structures
 If Structure
Syntax:
if(condition)
{
statement/s;
}
 If-Else Structure
Syntax:
if(condition)
{
statement/s;
}
else
{
statement/s;
}
 If-Else If Structure
Syntax:
if(condition)
{
statement/s;
}
else
if(condition)
{
statement/s;
}
else
{
statement/s;
}
 Switch Structure
Syntax:
switch (identifier)
{
case constant: statement/s;
break;

case constant:
statement/s;
break;
case constant: statement/s;
break;
default: statement/s;
break;
}

 Looping Structures
 While Loop Structure
Syntax:
while (condition)
{
statement/s;
}
 Do While Loop Structure
Syntax:
do
{
statement/s;
} while (condition);
 For Loop Structure
Syntax:
for (initialization; condition; action)
{
statement/s;
}
2.
 If Structure:
public class Number
{

public static void main(String args[])


{
int num=50;
if( num < 100 )
{

System.out.println("Number is less than


100");
}
}
}
Explanation:
When the given condition is true, the statement num is executed. If the
condition is false, the statements contained within the if statement body are
completely ignored.

 If-Else Structure:
public class Number
{
public static void main(String args[])
{
int num=150;
if( num < 75 )
{
System.out.println("Number is less than 75");
}
else {
System.out.println("Number is greater than or equal 75");
}
}
}
Explanation:
If the condition is true, the statement inside if will be executed, and if the
condition is false, the statement inside else will be executed.
 If-Else If Structure:
public class Number
{
public static void main(String args[]){
int num=9912;

if(num <100 && num>=1)


{
System.out.println("Its a two digit number");
}
else if(num <1000 && num>=100)
{ System.out.println("Its a three digit
number");
}
else if(num <10000 && num>=1000)
{ System.out.println("Its a four digit number");
}
else if(num <100000 && num>=10000)
{ System.out.println("Its a five digit
number");
}
else {
System.out.println("number is not between 1 & 99999");

}
}
}
Explanation:
When the condition is met, the corresponding set of statements is
executed, and the rest are ignored. If none of the conditions are met, the
statement contained within else is executed.

 Switch Structure
class Main {
public static void main(String[] args) {

int number = 50;


String size;
switch (number) {

case 30:
size = "Small";
break;

case 40:
size = "Medium";
break;
case 55:
size = "Large";
break;

case 60:
size = "Extra Large";
break;

default:
size = "Unknown";
break;

}
System.out.println("Size: " + size);
}
}
Explanation:
In the example above, to determine the size, we used the switch statement.
We have a variable number 55 here. Each case statemen's value is compared
to the variable. Since the value is equal to 55, case 55's code gets performed.
The size variable has the value Large assigned to it..

 While Loop Structure


public class Main {
public static void main(String[] args)
{ int i = 0;
while (i < 10)
{ System.out.println(i)
; i++;
}
}
}
Explanation:
As long as a variable's value is less than 10, the loop's function will execute
again.
 Do While Loop Structure
public class Main {
public static void main(String[] args)
{ int i=0;
do{
System.out.println(i);
i++;
}while(i<=50);
}
}
Explanation:
We display integer values between 1 and 50. Unlike the for loop, the variable
used in the condition needs to be initialized and increased independently (here, i). If not,
the loop will run indefinitely.

 For Loop Structure


public class Main {
public static void main(String[] args)
{ for (int i = 3; i < 5; i++)
{ System.out.println(i);
}
}
}
Explanation:
Before the loop begins, a variable is set at line 1 (int I = 0).
Line 2 specifies the prerequisite for the loop to execute (i must be less than 5). The
loop will repeat itself if the condition is true and come to an end if it is false.
Each time the code block in the loop has been executed, line 3 increments a value
(i++).

You might also like