Oopr Midterm Reviewer
Oopr Midterm Reviewer
CONDITIONAL STATEMENTS
Topics:
⊳ Understanding if statements
⊳ Complex conditionals The simple if statement has the following syntax:
⊳ The switch, case, and break statements
if (<boolean expression>)
Java Statement <statement action>
1. Simple Statement
Basic building blocks of a program
One or more lines terminated by a semicolon (;)
printf("Hello, World!");
1. IF STATEMENT
executes a block of code only if the specified
expression is true.
Making a decision involves choosing between two 2. IF-ELSE STATEMENT
alternative courses of action based on some value
within a program. Extension of the if statement. if the statements in the if
statement fails, the statements in the else block are
executed.
Making a decision involves choosing between two
alternative courses of action based on some value
within a program.
3. NESTED if STATEMENT
The simple if-else statement has the following syntax:
LOOPING STATEMENTS
1. LOOP STATEMENT
Execute a statement or group of statements multiple
times and following is the general form of a loop
statement in most of the programming languages.
Break statement
• defines the end of a case.
Default statement
CATEGORIES
• use in defining message to be displayed that is outside of
the given case.
1. FOR LOOP
Here, key point of the while loop is that the loop might
not ever run. When the expression is tested and the
result is false, the loop body will be skipped and the
first statement after the while loop will be executed.
Topics:
⊳ Exception Hierarchy IOException – caused by general I/O failures, such as
⊳ Creating Exceptions inability to read from a file
⊳ Try, Catch, and Finally NullPointerException – caused by referencing a null
object
1. EXCEPTION NumberFormatException – caused when a
conversion between string and number fails
An exception is an event, usually some form of error, OutOfMemoryException – caused when there is not
which happens during the normal course of program enough memory to allocate a new object
execution. The object-oriented technique to manage SecurityException – caused by a security violation
such errors comprises the group of methods known as such as when an applet tries to perform an action not
exception handling. allowed by the browser’s security setting
StackOverflowException – caused when a program
Exception Hierarchy attempts to access a nonexistent character position in
a string
Creating Exceptions
GENERATING EXCEPTION
Method Calls
If a method or constructor is declared to throw an
exception, then calling that method or constructor
may result in an exception of the declared class or a
subclass.
Types of Exceptions
2. Catching Exceptions
1. Throws Clause
EXAMPLE:
finishes running. Unlike normal exception/error
handling, assertions are generally disabled at run-time.
Syntax