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

SCJP-Chapter 4

Version: 1.0
Author: KhanhDM

Monday, April 19, 2021

Instructor: KhanhDM

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 1 04e-BM/NS/HDCV/FSOFT v2/2
Introduction

 Duration: 2 hours
 Purpose: Flow Control, Exceptions, and Assertions
 Audience: To be Developer
 Test: Self test

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 2 04e-BM/NS/HDCV/FSOFT v2/2
Objectives

 • Writing Code Using if and switch Statements


 • Writing Code Using Loops
 • Handling Exceptions

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 3 04e-BM/NS/HDCV/FSOFT v2/2
Contents

 Course Agenda

 Two-Minute Drill
 Self Test
 Practice

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 4 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

A. Writing Code Using if and switch Statements

❑ if-else Branching
❑ switch Statements

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 5 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

if-else Branching

❑ The basic format of an if statement is as follows:


if (booleanExpression) {
System.out.println("Inside if statement");
}

❑ Example:
if (x > 3) {
System.out.println("x is greater than 3");
} else {
System.out.println("x is not greater than 3");
}

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 6 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

switch Statements

❑ Another way to simulate the use of multiple if statements is with the switch statement.
❑ Example:
int x = 3;
If (x == 1) {
System.out.println("x equals 1");
} else if (x == 2) {
System.out.println("x equals 2");
} else if (x == 3) {
System.out.println("x equals 3");
} else {
System.out.println("No idea what x is");
}

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 7 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

switch Statements

❑ the same functionality represented in a switch construct:


int x = 3;
switch (x) {
case 1:
System.out.println("x is equal to 1");
break;
case 2:
System.out.println("x is equal to 2");
break;
case 3:
System.out.println("x is equal to 3");
break;
default:
System.out.println("Still no idea what x is");
}

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 8 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

switch Statements

❑ the same functionality represented in a switch construct:


int x = 3;
switch (x) {
case 1:
System.out.println("x is equal to 1");
break;
case 2:
System.out.println("x is equal to 2");
break;
case 3:
System.out.println("x is equal to 3");
break;
default:
System.out.println("Still no idea what x is");
}

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 9 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

switch Statements

❑ break keyword
❑ default case

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 10 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

B. Writing Code Using Loops

❑ Using while Loops


❑ Using do-while Loops
❑ Using for Loops
- Useful for flow control when already know how many times need to execute
the statements in the loop’s block.
- The for loop declaration has three main parts, besides the body of the loop:
■ Declaration and initialization of variables
■ The boolean expression (conditional test)
■ The iteration expression

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 11 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

Using for Loops (cont)

❑ The declaration and initialization happens before anything else in a for loop.
❑ The scope of variables declared in the for loop ends with the for loop!
❑ Can have only one test expression.
for (int x = 0; (x > 5), (y < 2); x++) { } // too many
//expressions
❑ Using break and continue in for Loops

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 12 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

C. Handling Exceptions

❑ Syntax:
try {
// This is the first line of the "guarded region"
// that is governed by the try keyword.
// Put code here that might cause some kind of exception.
// We may have many code lines here or just one.
} catch(MyFirstException) {
// Put code here that handles this Exception.
// This is the next line of the exception handler.
// This is the last line of the exception handler.
} catch(MySecondException) {
// Put code here that handles this exception
}
// Some other unguarded (normal, non-risky) code begins here

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 13 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

Catching an Exception Using try and catch

❑ A finally block encloses code that is always executed at some point after the
try block, whether an exception was thrown or not.

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 14 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

Propagating Uncaught Exceptions

❑ call stack

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 15 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

Exception Hierarchy

❑ The handlers for the most specific exceptions must always be placed above
those for more general exceptions.
try {
// do risky IO things
} catch (IOException e) {
// handle general IOExceptions
} catch (FileNotFoundException ex) {
// handle just FileNotFoundException
}

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 16 04e-BM/NS/HDCV/FSOFT v2/2
Two-Minute Drill

Exception Declaration and the Public Interface

❑ The throws keyword is used as follows to list the exceptions that a method can throw
void myFunction() throws MyException1, MyException2 {
// code for the method here
}
❑ Any method that might throw an exception (unless it’s a subclass of RuntimeException)
must declare the exception.
void doStuff() {
doMore();
}
void doMore() {
throw new IOException();
}
❑ Runtime exceptions are referred to as unchecked exceptions. All other exceptions,
meaning all those that do not derive from java.lang.RuntimeException, are checked
exceptions.

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 17 04e-BM/NS/HDCV/FSOFT v2/2
Questions and Answers

© Copyright
© FPT SOFTWARE 2006 FPT
– TRAINING Software
MATERIAL – Internal use 18 04e-BM/NS/HDCV/FSOFT v2/2

You might also like