Introducing Switch Statement and For Loop: Java Programming

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Introducing switch statement and for loop

Java Programming

Introducing switch statement and for loop

Objective:
At the end of the class, the student is expected to

1. Know the syntax of switch statement and for loop.


2. Acquire the necessary skills and understanding of
switch and for loop concepts.
Simulate a given code.

Introducing switch statement and for loop

What is switch statement?


switch statement syntax
switch sample program
What is for loop?
For loop syntax
For loop sample program
Program Simulation

Switch statement

A switch statement allows a variable to be tested


For equality against a list of values. Each value is called a
case, and the variable being switched on is checked for
each case.

Switch Statement Syntax


switch(expression){
case value : //Statements
break;
case value : //Statements
break;
default : //Statements
}
//You can have any number of case statements.

So

you start with the word switch, followed


by a pair of round brackets. The variable you
want to check goes between the round
brackets of switch. You then have a pair of
curly brackets.
For every value that you want to check, you
need the word case. You then have the
value you want to check for:
case value:

After

case value comes a colon. You then


put what you want to happen if the value
matches. This is your code that you want
executed.
The keyword break is needed to break
out of each case of the switch statement.

The

default value at the end is optional. It


can be included if there are other values
that can be held in your variable but that
you haven't checked for elsewhere in the
switch statement.

Switch Statement Sample


Sample Program
A program that prompts the user to select an operation.
Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Mul, 2 - Div, 3 - Add, 4 - Sub: 1
MULTIPLICATION
Enter 1st Number:3
Enter 2nd Number:3
The product is: 9

Switch Statement Sample


Scanner in =new Scanner(System.in);
int a, b, sel;
System.out.print("Program for Addition, Subtraction, Multiplication and Division\n");
System.out.print("Enter Your Choice: 1 - Mul, 2 - Div, 3 - Add, 4 - Sub: ");
sel = in.nextInt();
switch(sel)
{
case 1:System.out.print("MULTIPLICATION\n");
System.out.print("Enter First Number:");
a=in.nextInt();
System.out.print("Enter Second Number:");
b=in.nextInt();
System.out.print("The product is: "+(a*b));
break;

Switch Statement Sample


case 2:System.out.print("DIVISION\n:");
System.out.print("Enter First Number:");
a=in.nextInt();
System.out.print("Enter Second Number:");
b=in.nextInt();
System.out.print("The qoutient is: "+(a/b));
break;
case 3:System.out.print("ADDITION\n");
System.out.print("Enter First Number:");
a=in.nextInt();
System.out.print("Enter Second Number:");
b=in.nextInt();
System.out.print("The sum is: "+(a+b));
break;

Switch Statement Sample


case 4:System.out.print("SUBTRACTION\n");
System.out.print("Enter First Number:");
a=in.nextInt();
System.out.print("Enter Second Number:");
b=in.nextInt();
System.out.print("The difference is: "+(a-b));
break;
default: System.out.print("NONE OF THE CHOICES!");
}

For loop

A for loop is a repetition control structure that


allows you to efficiently write a loop that
needs to execute a specific number of times.
A for loop is useful when you know how many
times a task is to be repeated.

For loop syntax

for(initialization; Boolean_expression; update)


{
//Statements
}

For loop

Here is the flow of control in a for loop:


The initialization step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required to put a
statement here, as long as a semicolon appears.
Next, the Boolean expression is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and flow of control
jumps to the next statement past the for loop.
After the body of the for loop executes, the flow of control jumps back up to the
update statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after
the Boolean expression.
The Boolean expression is now evaluated again. If it is true, the loop executes
and the process repeats itself (body of loop, then update step,then Boolean
expression). After the Boolean expression is false, the for loop terminates.

For loop sample


Sample program
11111
22222
33333
44444
55555

For loop sample

For loop sample

Sample Program
12345
12345
12345
12345
12345

For loop sample

For this ACTIVITY, write a programm that asks a user to


choose between four colours: black, white, red, or blue. Use
SWITCH statements to display one of the following
messages, depending on which colour was chosen:
BLACK "You must be a Goth!"
WHITE "You are a very pure person"
RED "You are fun and outgoing"
BLUE "You're not a Chelsea fan, are you?

Program Simulation

HAVE A NICE DAY!


GOODLUCK =)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy