0% found this document useful (0 votes)
15 views12 pages

Java Control Structures Explained

The document is a lesson plan from Pampanga State University focused on Java control structures, including conditional statements, switch statements, and iteration statements. It outlines the definitions, syntax, and examples of each control structure, providing practical coding examples for better understanding. Additionally, it includes exercises for students to apply their knowledge in writing Java programs.

Uploaded by

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

Java Control Structures Explained

The document is a lesson plan from Pampanga State University focused on Java control structures, including conditional statements, switch statements, and iteration statements. It outlines the definitions, syntax, and examples of each control structure, providing practical coding examples for better understanding. Additionally, it includes exercises for students to apply their knowledge in writing Java programs.

Uploaded by

ronnelbalcita23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

Lesson 5: Java Control Structures


Conditional Statement, Switch Statement and Iteration Statement

Course Code: ITELEC 1


Course Title: Object-Oriented Programming (Java)
Prepared by: Alvin Herrera, Instructor 1

Lesson Content:
1. What is Control Structure?
2. What is Conditional Statement?
3. What is Switch Statement?
4. What is Iteration Statement?

1. What is Control Structure?

The programming you're doing now is sequential programming,


meaning the code is executed from top to bottom. It's very linear,
in that each and every line of code will be read, starting with the
first line of code you write and ending at the last line.

But you don't always want your programs to work like that.
Often, you want code to be executed only if certain conditions are
met. You can do this with Control Structures.

Control structures in Java are used to control the flow of


your program by making decisions or repeating actions. Java has
three primary control structures: Conditional Statement, Switch
Statement and iteration Statement.

They are essential tools for creating more complex programs.


These structures allow you to control the flow of your program and
repeat certain actions until a specific condition is met.

2. What is Conditional Statement?

Conditional statements allow you to make decisions based on


certain conditions.

1. if statement

The if statement is the most basic of all the control


flow statements. The if statement tells our program to execute
a certain section of code only if a particular test evaluates
to true.

Syntax:
if(condition){
Statement(s);
}

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS •


LUBAO • CANDABA • APALIT • SAN FERNANDO Office
Address | Moras dela Paz, Sto. Tomas, Pampanga,
Philippines Email Address |
santotomascampus@[Link] • Contact
No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines
PAMPANGA STATE UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus

Example:
Source Code

public class IfStatement {

public static void main(String[] args) {

int num = 100;

if (num<=100){
[Link]("Value of num is
"+num);
}

Output

Value of num is 100

2. if-else statement

If a condition is true then the section of code under if


would execute else the section of code under else would
execute.

Syntax:
if(condition) {
Statement(s);
}
else {
Statement(s);
}

Example:
BACOLOR (MAIN) • MEXICO • PORAC • STO.
TOMAS • LUBAO • CANDABA • APALIT • SAN
FERNANDO Office Address | Moras dela
Paz, Sto. Tomas, Pampanga, Philippines
Email Address |
santotomascampus@[Link]
• Contact No. | (0921) 373 0913
Website | [Link]

Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


Output

Value is less than 100

3. if-else-if statement

The if else if statements are used when the user has to


decide among multiple options.

Syntax:
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);
}

Example:
Source Code

public class IfElseStatement {


public static void main(String[] args) {

int marks = 76;


char grade;

if (marks >= 80) {


grade = 'A';
} else if (marks >= 70) {
grade = 'B';
} else if (marks >= 60) {

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


grade = 'C';
} else if (marks >= 50) {
grade = 'D';
} else {
grade = 'F';
}
[Link]("Grade = " + grade);

}
}

Output

Grade = B

4. Nested if statement

An if statement inside another the statement. If the


outer if condition is true then the section of code under
outer if condition would execute and it goes to the inner if
condition. If inner if condition is true then the section of
code under inner if condition would execute.

Syntax:
if(condition_1) {
Statement1(s);

if(condition_2) {
Statement2(s);
}
}

Example:
Source Code
public class IfElseStatement {
public static void main(String[] args) {
int num = 100;

if (num<=100){
[Link]("Value of num is "+num+".");

if (num>50){
[Link]("The value " + num + " is greater
than 50." );
}
}
}
}

Output

Value of num is 100.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


The value 100 is greater than 50.

3. What is Switch Statement?

The switch statement in Java is a multi-branch statement. We


use this in Java when we have multiple options to select. It
executes particular option based on the value of an expression.

Syntax:

switch(expression) {
case valueOne:
//statement(s)
break;
case valueTwo:
//statement(s)
break;
:
:
:
default: //optional
//statement(s) //This code will be executed if all cases are not
matched
}

Example Switch Case without break statement:


Source Code
public class IfElseStatement {
public static void main(String[] args) {
/*The java switch statement is fall-through.
It means it executes all statement after first match
if break statement is not used with switch cases.*/ int num=200;
switch(num){

case 100:
[Link]("Value of Case 1 is "+num);
case 200:
[Link]("Value of Case 2 is "+num);
default:
[Link]("Value of default is "+num);

}}

Output

Value of Case 2 is 200


Value of default is 200

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus

Example Switch Case with break statement:


Source Code

public class IfElseStatement {


public static void main(String[] args) {
int num=200;
switch(num){

case 100:
[Link]("Value of Case 1 is "+num);
break;
case 200:
[Link]("Value of Case 2 is "+num);
break;
// In this only case 2 will be executed and rest of
the cases will be ignored.
default:
[Link]("Value of default is "+num);
}
}
}

Output

Value of Case 2 is 200


4. What is Iteration Statement?

Iterations or Loop Statements in Java allow you to repeat


certain actions until a specific condition is met. Java has three
types of Iterations/loops: for, while, and do-while.

1. for loop
A for loop repeats a block of code a specified number of
times.

Syntax:
for(initialization; condition; increment){
// body of the loop
}

Example:
Source Code

public class ForLoop {


public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
[Link]("The value of i is: " + i); }
}
}

Output

The value of i is: 0

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5
The value of i is: 6
The value of i is: 7
The value of i is: 8
The value of i is: 9

2. while loop
A while loop repeats a block of code while a specific
condition is true.

Syntax:
initialization
while(condition){
// body of the loop
Increment
}

Example:
Source Code

public class WhileLoop {


public static void main(String[] args) {
int i = 0;
while (i < 10) {
[Link]("The value of i is: " + i); i++;
}
}
}

Output

The value of i is: 0


The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5
The value of i is: 6
The value of i is: 7
The value of i is: 8
The value of i is: 9

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

3. do-while loop
A do-while loop repeats a block of code at least once and then
repeats the code while a specific condition is true.

Syntax:
initialization
do{
// body of the loop
increment
}while(condition);

Example:
Source Code

public class WhileLoop {


public static void main(String[] args) {
int i = 0;
do {
[Link]("The value of i is: " + i); i++;
} while (i < 10);
}
}

Output
The value of i is: 0
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
The value of i is: 5
The value of i is: 6
The value of i is: 7
The value of i is: 8
The value of i is: 9

Exercises:

1. Write a Java program that checks if a number is less than or equal to


100 using an if statement.

Sample Output:
Enter a number: 100
Value of num is 100

2. Write a Java program that checks if a number is greater than 100. If it


is, print that it’s greater; otherwise, print that it’s less or equal.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

Sample Output:
Enter a number: 100
Value is less than 100

3. Write a Java program that prompts the user to enter a grade (integer
from 0 to 100), then uses an if-else-if conditional statement to classify
and display the performance based on the following criteria:

90 – 100: Outstanding
85 – 89: Very Satisfactory
80 – 84: Satisfactory
75 – 79: Fair
Below 75: Poor

Sample Output:
Enter grade: 89
89 is Very Satisfactory.

4. Write a Java program that first checks if a number is less than or


equal to 100, and then checks if it is greater than 50 using nested if
statements.

Sample Output:
Enter a number: 100
Value of num is 100.
The value 100 is greater than 50.
5. Write a Java program using a switch statement to print a message
depending on the value of a number. Do not use break statements.

Sample Output:
Enter a number: 200
Value of Case 2 is 200
Value of default is 200

6. Write a Java program using a switch statement to print a message for a


specific value. Use break statements to stop fall-through.

Sample Output:
Enter a number: 200
Value of Case 2 is 200

7. Write a Java program using for loop to print the sum and average of
numbers from 1-n.

Sample Output:
Enter a number: 6
The sum of 1 to 6 is 21.
The average of the 6 numbers is 3.5

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

8. Write a Java program using while loop to print the mean of five
numbers.

Sample Output:
Enter num1: 7
Enter num2: 9
Enter num3: 15
Enter num4: 3
Enter num5: 21

Mean: 11

9. Write a Java program using do while loop to repeat the previous


program.

Sample Output:
Enter num1: 7
Enter num2: 9
Enter num3: 15
Enter num4: 3
Enter num5: 21

Mean: 11

Do you want to try again?(y/n):

Note: if the user inputs ‘y’, repeat the program. If the user inputs ‘n’,
terminate the program.
10. Write a Java program using a switch statement that prints the name of
a day based on an integer (1 = Sunday, 2 = Monday, etc.).

Sample Output:
Enter a number: 2
Day is Monday

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

Review Questions and their answers

1. What is a control structure in Java?

Ans. A control structure is used to control the flow of a program by


making decisions or repeating actions. It allows the program to execute
certain blocks of code conditionally or repeatedly.

2. What are the three main types of control structures in Java?

Ans. The three main types are: Conditional Statements, Switch Statements,
and Iteration (Loop) Statements.

3. How does an if statement work in Java?

Ans. An if statement checks a condition, and if the condition is true, the


code inside the block runs.

4. When should you use an if-else statement instead of a simple if?

Ans. Use if-else when you want one block to run if the condition is true,
and a different block if it's false.

5. What is the purpose of the if-else-if ladder?

Ans. It is used when there are multiple conditions to check and you want
to execute only one block that matches the first true condition.

6. What is a nested if statement?

Ans. A nested if is an if statement inside another if block, allowing you


to test a second condition only when the first is true.

7. What does the switch statement do in Java?

Ans. A switch statement allows you to test the value of an expression


against multiple case values, executing the matched case block.

8. What happens if you omit the break statement in a switch case?

Ans. If break is omitted, Java performs fall-through, executing all


subsequent cases until a break or the end of the switch block.

9. What is the difference between for, while, and do-while loops?

Ans. A for loop is used when the number of iterations is known; a while
loop checks the condition before executing; a do-while loop executes once
before checking the condition.

10. When would you use a do-while loop instead of a while loop?

Ans. Use do-while when you need the loop body to run at least once,
regardless of the condition.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]

You might also like