0% found this document useful (0 votes)
3 views31 pages

ch04 pt5

Chapter 4 discusses control statements in Java, focusing on break and continue statements, as well as the switch multiple-selection statement. It explains how these statements alter the flow of control in loops and conditional structures. Additionally, it includes an exercise on extracting a birthdate from a civil ID number using Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

ch04 pt5

Chapter 4 discusses control statements in Java, focusing on break and continue statements, as well as the switch multiple-selection statement. It explains how these statements alter the flow of control in loops and conditional structures. Additionally, it includes an exercise on extracting a birthdate from a civil ID number using Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 4: Control Statements: Part 2;

Logical Operators

Badour AlBahar
Kuwait University

Slides adopted from:


ENG-200 Java How to Program, 10/e Late Objects Version
Fall 2024
Announcement
• Anonymous Feedback Form: Still available on Moodle. Your input is
valuable!

• Midterm will be on Sunday, 27 October 2024, 11:00 AM.


break Statement
• The break statement, when executed in a while, for, do…while or
switch, causes immediate exit from that statement.
• Execution continues with the first statement after the control
statement.
• Common uses of the break statement are to:
• Escape early from a loop
• Skip the remainder of a switch
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
continue Statement
• The continue statement, when executed in a while, for, do…while,
skips the remaining statements in the loop body and proceeds with
the next iteration of the loop.
• In while and do…while statements, the program evaluates the loop-
continuation test immediately after the continue statement executes.
• In a for statement, the increment expression executes, then the program
evaluates the loop-continuation test.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
switch Multiple-Selection Statement
• switch multiple-selection statement performs different actions based
on the possible values of a constant integral expression of type byte,
short, int or char.
• As of Java SE 7, the expression may also be a String.
switch (expression) {
case value1:
// code to be executed if expression equals value1
break; // optional, but important in most cases
case value2:
// code to be executed if expression equals value2
break;
// you can have any number of case statements
default:
// code to be executed if expression doesn't match any case
}
switch Multiple-Selection Statement
• The switch statement consists of a block that contains a sequence of
case labels and an optional default case.
• The program evaluates the controlling expression in the parentheses
following keyword switch.
• The program compares this expression’s value (which must evaluate to
an integral value of type byte, char, short or int, or to a
String) with each case label.
• If a match occurs, the program executes that case’s statements.
• The break statement causes program control to proceed with the first
statement after the switch.
switch Multiple-Selection Statement
• switch does not provide a mechanism for testing ranges of values—every
value must be listed in a separate case label.
• Each case can have multiple statements.
• switch differs from other control statements in that it does not require
braces around multiple statements in a case.
• Without break, the statements for a matching case and subsequent cases
execute until a break or the end of the switch is encountered. This is
called “falling through.”
• If no match occurs between the controlling expression’s value and a case
label, the default case executes.
• If no match occurs and there is no default case, program control simply
continues with the first statement after the switch.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
switch Multiple-Selection Statement
• Most switch statements use a break in each case to terminate the
switch statement after processing the case.
• The break statement is not required for the switch’s last case (or
the optional default case, when it appears last), because execution
continues with the next statement after the switch.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
switch Multiple-Selection Statement
• When using the switch statement, remember that each case must
contain a String or a constant integral expression.
• An integer constant is simply an integer value.
• In addition, you can use character constants—specific characters in
single quotes, such as 'A', '7' or '$'—which represent the integer values
of characters and enum constants.
• The expression in each case can also be a constant variable—a
variable that contains a value which does not change for the entire
program. Such a variable is declared with keyword final.
• Java has a feature called enum types. enum type constants can also be
used in case labels.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
End-of-file Indicator
• The end-of-file indicator is a system-dependent keystroke combination
which the user enters to indicate that there’s no more data to input.
• On UNIX/Linux/Mac OS X systems, end-of-file is entered by typing the
sequence
• <Ctrl> d on a line by itself.
• This notation means to simultaneously press both the Ctrl key and the d key.
• On Windows systems, end-of-file can be entered by typing
• <Ctrl> z
• Windows typically displays the characters ^Z on the screen when the end-of-file
indicator is typed.
• On some systems, you must press Enter after typing the end-of-file key
sequence.
© 1992-2015 by Pearson Education, Inc. All Rights Reserved.
Scanner hasNext
• Scanner method hasNext determine whether there’s more data to
input. This method returns the boolean value true if there’s more
data; otherwise, it returns false.
• Method hasNext returns false once the user types the end-of-file
indicator.
What do you think the output will be?
public class ClassWork{
public static void main (String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
}
}
What do you think the output will be?
public class ClassWork{
public static void main (String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
What do you think the output will be?
public class ClassWork{
public static void main (String[] args) {
int i = 1;
while (i <= 5) {
if (i == 3) {
System.out.println("Continue...");
continue;
}
System.out.println(i);
i++;
}
}
}
What do you think the output will be?
public class ClassWork{
public static void main (String[] args) {
int i = 1;
while (i <= 5) {
if (i == 3) {
System.out.println("Continue...");
i++;
continue;
}
System.out.println(i);
i++;
}
}
}
Exercise
Birthday from Civil ID
Write a Java program that takes a civil ID number as input and extracts
the birthdate from it. The program should extract the day, month, and
year from the civil ID and print the birthdate in the format
DD/MM/YYYY.

Example:
• For the civil ID number 219060500000, the program should print the
birthdate as 05/06/1919.
• For the civil ID number 319060500000, the program should print the
birthdate as 05/06/2019.

You might also like