PROGRAMMING 1 - ITCC 112
CONTROL STATEMENTS
& LOOPS IN JAVA
KYSTAL ANN SAYCO
TOPICS
01 Conditional Statements 04 for loop
02 Logical Statements 05 while loop
03 Logical Statements 06 do while loop
CONTROL STATEMENTS
used to perform different actions based on
different conditions.
Tells your program which path to take.
Divided into three conditional structure:
Selection Statement, Iteration Statement, Jumps in Statement
CONTROL
STATEMENTS
SELECTION ITERATION JUMP
if... else Switch... Case break continue
while do... While for
SELECTION STATEMENT
also called as Decision making
statements.
let your program make decision base on
conditions
Two Types: if statement & switch statement
IF STATEMENT
IF STATEMENT
used to test a condition
checks bolean condition: True or False
Various types:
1. if statement
2. if-else statement
3. Nested if-else statement
if statement syntax
example:
int score = 85;
if (score >= 60) {
System.out.println("You passed!");
}
IF - ELSE STATEMENT
followed by an optional else statement,
which executes when the condition
evaluates to false.
executes the if block if condition is true
otherwise else block is executed.
syntax
example:
int passingscore = 40;
if (passingscore <= 39) {
System.out.println("Fail");
} else {
System.out.println("You passed");
}
NESTED IF-ELSE STATEMENT
A nested if-else statement is an if-else
statement within another if or else
statement.
It allows checking multiple conditions in a
hierarchical manner.
syntax
int score = 85;
if (score >= 60) {
System.out.println("You passed!");
if (score >= 80) {
System.out.println("Great job!");
Example } else {
System.out.println("You can do better!");
}
} else {
System.out.println("You failed.");
}
ELSE-IF LADDER STATEMENTS
a way to check multiple conditions in
sequence.
It allows the program to evaluate several
conditions without nesting, making the
code more readable.
syntax
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
Example } else if (score >= 70) {
System.out.println("Grade: C");
} else if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
import java.util.Scanner;
Example public class SnackSelection {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Choose your snack (chips, cookies, or soda): ");
a Java program that lets a user String snack = scanner.nextLine();
int totalCost = 0; // Using int for total cost
select a snack from a menu. The // Determine the price based on the snack chosen
program will display the price if (snack.equals("chips")) {
totalCost = 20; // Price for chips
based on the user's selection. } else if (snack.equals("cookies")) {
totalCost = 15; // Price for cookies
} else if (snack.equals("soda")) {
totalCost = 25; // Price for soda
Menu } else {
System.out.println("Invalid snack selected.");
Chips: ₱20 scanner.close();
return; // Exit if invalid snack
Cookies: ₱15 }
Soda: ₱25 // Output the total cost
System.out.println("Your total cost is: ₱" + totalCost);
scanner.close();
}
}
Activity - Simple Drink Vending Machine
1. Write a program that asks the user to choose a drink from a
vending machine.
2. The choices are: "water", "juice", and "coffee".
3. Based on the choice, the program should display the cost of the
drink. Use the following prices:
Water: ₱10
Juice: ₱20
Coffee: ₱30
4. If the user enters something other than the three drinks, display
a message indicating the selection is invalid.
5. After displaying the total cost, the program should terminate.
SWITCH STATEMENTS
allows a variable to be tested for equality
against a list of values.
Each value is called a case, and the variable
is checked against each case.
an alternative for using multiple if-else
conditions.
syntax
switch (variable) - The variable
you're checking.
case value: - A possible value the
variable might equal.
break; - Ends each case to prevent
"fall-through.”
default: - Optional block that runs
if no case matches.
String fruit = "apple";
switch (fruit) {
case "apple":
Example System.out.println("You selected an apple.");
break;
case "banana":
System.out.println("You selected a banana.");
break;
case "cherry":
System.out.println("You selected a cherry.");
break;
default:
System.out.println("Unknown fruit.");
}
Example
case 4:
i// Ask the user to input a number between 1 and 7 System.out.println("Thursday");
System.out.print("Enter a number (1-7); break;
int day = scanner.nextInt(); case 5:
System.out.println("Friday");
switch (day) { break;
case 1: case 6:
System.out.println("Monday"); System.out.println("Saturday");
break; break;
case 2: case 7:
System.out.println("Tuesday"); System.out.println("Sunday");
break; break;
case 3: default:
System.out.println("Wednesday"); System.out.println("Invalid number");
break; }
// Close the scanner object
scanner.close();
Activity - Switch Statements
Create a simple Java program to determine the number of
days in a given month of any year. You will use a switch
statement to map the month number to the corresponding
number of days.
Input a month number: 2
Input a year: 2020
Output:
February 2020 has 28 days
case 5:
import java.util.Scanner; monthName = "May";
public class Main days = 31;
{ break;
public static void main(String[] args) { case 6:
monthName = "June";
days = 30;
Scanner scanner = new Scanner(System.in); break;
case 7:
// Input the month number and year monthName = "July";
System.out.print("Input a month number (1-12): "); days = 31;
int month = scanner.nextInt(); break;
case 8:
System.out.print("Input a year: "); monthName = "August";
int year = scanner.nextInt(); days = 31;
break;
String monthName = ""; // To store the full month name case 9:
int days = 0; // To store the number of days in the month monthName = "September";
days = 30;
break;
// Determine the month name and number of days case 10:
switch (month) { monthName = "October";
case 1: days = 31;
monthName = "January"; break;
days = 31; case 11:
break; monthName = "November";
case 2: days = 30;
monthName = "February"; break;
days = 28; case 12:
break; monthName = "December";
case 3: days = 31;
monthName = "March"; break;
days = 31; default:
break; System.out.println("Invalid month number.");
case 4: scanner.close();
monthName = "April"; return; // Exit if the month number is invalid
days = 30; }
break;
// Output the result
System.out.println(monthName + " " + year + " has " + days + " days.");
THANK YOU
Prepared by KYSTAL ANN SAYCO