Professional Documents
Culture Documents
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
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
{
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;
}
}
}
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) {
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..