You are on page 1of 9

OOPR MIDTERMS REPETITION CONTROL STRUCTURES

Module 5: Java Control - Allow to execute specific sections of


the code a number of times.
Statements – CONDITIONAL
STATEMENTS
Understanding IF Statements
JAVA STATEMENT
THE IF STATEMENT
- In Java, a STATEMENT is one or more
lines of code ending with a semi- - The IF STATEMENT executes a block
colon (;). of code only if the specified
- It generally contains expressions expression is true.
(expressions have a value).

SIMPLE STATEMENT
- Basic building blocks of a program.
- One or more lines terminated by a
semicolon (;).

COMPOUND STATEMENT OR BLOCK


- Used to organize simple statements
into complex structures.
- One or more statements enclose by
braces { } .
EVERY TIME JAVA ENCOUNTERS AN IF
- Recommended to indent blocks for STATEMENT,
readability.
1. It determines whether EXPRESSION is
TRUE or FALSE.
2. If EXPRESSION IS TRUE, then Java
EXECUTES STATEMENT.

CONTROL STRUCTURE 3. If EXPRESSION IS FALSE, then Java


IGNORES or SKIPS the remainder of
- Control structures allow to change the IF statement and proceeds to
the ordering of how the statements the next statement.
in a program is executed.
- Java statements that allows a Making a decision involves choosing
programmer to select and execute between two alternative courses of action
SPECIFIC BLOCKS OF CODE while based on some value within a program.
skipping other sections

INCLUDE TYPES SUCH AS:

- If
- If-Else
- If-Else-If
- Switch structure
TYPES OF CONTROL STRUCTURES:

DECISION CONTROL STRUCTURES


- Allow to select specific sections of
code to be executed.
IF STRUCTURE: EXAMPLE 1 IF- ELSE STRUCTURE: EXAMPLE 1
- Suppose that the passing grade on
an examination is 75 (out of 100).
Then the if statement may be written
in Java as:

IF- ELSE STRUCTURE: EXAMPLE 2


IF STRUCTURE : EXAMPLE 2

Complex Conditionals
IF- ELSE STRUCTURE: EXAMPLE 3
THE IF-ELSE STATEMENT
- The IF/ELSE STATEMENT is an extension
of the if statement. If the statements
in the if statement fails, the
statements in the else bock are
executed.

NESTED IF STATEMENT

- A NESTED IF is an if statement that is


the target of another if or else.
Nested if statements means an if
statement inside an if statement. Yes,
java allows us to nest if statements
Making a decision involves choosing within if statements. i.e, we can
between two alternative courses of action place an if statement inside another
based on some value within a program. if statement.
The Switch, Case, and Break
Statements

SWITCH CASE STATEMENT


- Also called a case statement is a
multi-way branch with several
choices.
- A switch is easier to implement than
a series of if/else statements.

Module 6: Java Control


Statements LOOPING
STATEMENTS

LOOP STATEMENT
- Allows us to 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
- Use in defining message to be
displayed that is outside of the given
case.

THREE (3) CATEGORIES OF LOOP


STATEMENTS

FOR LOOP – executes a sequence of


statements multiple times and
abbreviates the code that manages
the loop variable.

WHILE LOOP – repeats a statement or


group of statements while a given
condition is true. It tests the condition
before executing the loop body.

DO… WHILE LOOP – like a while


statement, except that it tests the
condition at the end of the loop body.
Understanding FOR LOOP Understanding WHILE LOOP

FOR LOOP WHILE LOOP


- It is useful when you know how many - A WHILE LOOP STATEMENT in Java
times a task is to be repeated. Programming Language repeatedly
- A repetition control structure that executes a target statement as long
allows you to efficiently write a loop as a given condition is true.
that needs to be executed a
WHILE LOOP Statement
specific number of times.

FOR LOOP STATEMENT

WHILE

- Here, key point of the WHILE LOOP is


FOR LOOP DEMO 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.

WHILE LOOP DEMO


Understanding DO…WHILE LOOP Module 7: Exception Handling

EXCEPTION
DO… WHILE LOOP
- An EXCEPTION is an event, usually
- A DO…WHILE LOOP is similar to a some form of error, which happens
while loop, except that a DO…WHILE during the normal course of program
LOOP is guaranteed to execute at execution. The object-oriented
least one time. technique to manage such errors
comprises the group of methods
DO… WHILE LOOP STATEMENT known as exception handling.

Exception Hierarchy

TYPES OF EXCEPTIONS

ERROR – serious errors in the java runtime


system.

EXCEPTION / CHECKED – The classes which


directly inherit throwable class except run
DO… WHILE LOOP DEMO time exception and error are known as
checked exceptions normal errors that can
occur during the execution of a program.

RUN TIME EXCEPTION / UNCHECKED –


encompasses all exceptions which can
ordinarily happen at runtime.
COMMON EXCEPTIONS RUNTIME EXCEPTIONS – these exceptions
can occur even though the offending
ARITHMETIC EXCEPTION – caused by math piece of code does not declare that it
errors such as division by zero.
throws such an exception.
ARRAY INDEX OUT OF BOUNDS EXCEPTION EXAMPLES OF THESE EXCEPTIONS INCLUDE:
– caused by a bad array index.
ARRAY STORE EXCEPTION – caused when
a program tries to store the wrong type of
data in an array.
USER EXCEPTIONS – these are exceptions
FILE NOT FOUND EXCEPTION – caused by that are manually thrown by the
an attempt to access a nonexistent file. programmer using the throw statement.

I O EXCEPTION – caused by general I / O - This statement takes a single


failures, such as inability to read from a file. argument which must be an object
that is a subclass of Throwable. You
NULL POINTER EXCEPTION – caused by can either throw an existing
referencing a null object. exception class or create your own.
- throw new Exception (“Problem”);
NUMBER FORMAT EXCEPTION – caused
when a conversation between string and Try, Catch, and Finally
number fails.
HANDLING EXCEPTION
OUT OF MEMORY EXCEPTION – caused
when there is not enough memory to PASSING ON EXCEPTIONS
allocate a new object.
- Passing on an exception refers to
SECURITY EXCEPTION – caused by a letting the method’s caller catch the
security violation such as when an applet exception. When a method declares
tries to perform an action not allowed by that it can throw an exception of a
the browser’s security setting. particular type. Within the method
body, it can perform actions which
STACK OVER FLOW EXCEPTION – caused may cause that type of exception to
when a program attempts to access a be thrown.
nonexistent character position in a string.
CATCHING EXCEPTIONS

Creating Exception - Catching an exception refers to


declaring that you can handle
GENERATING EXCEPTIONS exceptions of a particular class from
a particular block of code.
METHOD CALLS – If a method or - You specify the block of code and
constructor is declared to throw an then provide handlers for various
exception, then calling that method or classes of exception. If an exception
constructor may result in an exception of occurs then execution transfers to
the declared class or a subclass. the corresponding piece of handler
code.

THE TRY BLOCK


- When you create a segment of
code in which something might go
wrong, you place the code in a try
block, which is a block of code you
attempt to execute while
acknowledging that an exception
might occur.
A TRY BLOCK CONSISTS OF THE FOLLOWING SYNTAX KEY ASPECTS
FORMAT:
- The block notation is mandatory.
- A try block must be followed by at
least one catch block or one.
- For each try block, there can be one
or more catch blocks, but at most
only one finally block.
Catch and finally blocks…
- Finally block.
THE CATCH BLOCK - The catch blocks and finally blocks
must always appear in conjunction
- A catch block is a segment of code
with the try block, and in the above
that can handle an exception that
order.
might be thrown by the try block
that precedes it. - Each catch block defines an
exception handle. The header of the
A CATCH BLOCK HAS THE FOLLOWING FORMAT: catch block takes exactly one
argument, which is the exception it
will handle. The exception must be of
the throwable class or one of its
subclasses.

THROWS CLAUSE

- A throws clause lists the types of


exceptions that a method might
Finally blocks… throw. This is necessary for all
exceptions, except those of type
Error and Runtime Exception, or any
TRY CATCH DEMO 1 of their subclasses.
- All other exceptions that a method
can throw must be declared in the
throws clause. If they are not, a
compile-time error will occur.

THROWS CLAUSE EXAMPLE 1:


TRY CATCH DEMO 2

THROWS CLAUSE EXAMPLE 2:


ILLUSTRATING MORE THAN ONE EXCEPTION.
ASSERTION IN JAVA ASSERTION VS NORMAL EXCEPTION
HANDLING
- An assertion allows testing the
correctness of any assumptions that - Assertions are mainly used to check
have been made in the program. logically impossible situations. For
- Assertion is achieved using the assert example, they can be used to
statement in Java. While executing check the state a code expects
assertion, it is believed to be true. If it before it starts running or state after it
fails, JVM throws an error named finishes running. Unlike normal
AssertionError. It is mainly used for exception/error handling, assertions
testing purposes during are generally disabled at run-time.
development.
WHERE TO USE ASSERTIONS
- The assert statement is used with a
Boolean expression and can be
- Arguments to private methods.
written in two different ways.
Private arguments are provided by
SYNTAX: developer’s code only and
developer may want to check
his/her assumptions about
arguments.
- Conditional cases
- Conditions at the beginning of any
method.

WHERE NOT TO USE ASSERTIONS

- Assertions should not be used to


replace error messages.
- Assertions should not be used to
check arguments in the public
methods as they may be provided
by user. Error handling should be
used to handle errors provided by
user.
- Assertions should not be used on
WHY USE ASSERTIONS? command line arguments.

- To make sure that an unreachable


looking code is actually
unreachable.
- To make sure that assumptions
written in comments are right. if ((x &
1) == 1) { } else // x must be even {
assert (x % 2 == 0); }
- To make sure default switch case is
not reached.
- To check object’s state.
- In the beginning of the method
- After method invocation.

You might also like