Professional Documents
Culture Documents
8 10 12
1 2
⊳ Understanding if statements
⊳ Complex conditionals
⊳ The switch, case, and break statements
Java Statement
▸ If-Else
▸ If-Else-If
▸ Switch structure
Understanding
1 if statements
THE if STATEMENT
NO YES
Boolean
expression
THE if STATEMENT
If Structure: Example 1
If Structure: Example 2
1. Test if the value of the variable count is greater than 10. If the
answer is yes, print “Count is greater than 10”.
3. Test if the value of the variable age is greater than 18 and less than
or equal to 40. If the answer is yes, print “You’re not getting old!”
18
2 Complex
conditionals
THE if-else STATEMENT
The simple if-else statement has the following
syntax:
if (<boolean expression>)
<statement 1>
else
<statement 2>
THE IF/ELSE STATEMENT IS AN EXTENSION OF THE IF
STATEMENT. IF THE STATEMENTS IN THE IF STATEMENT FAILS,
THE STATEMENTS IN THE ELSE BLOCK ARE EXECUTED.
THE if-else STATEMENT
THE if-else STATEMENT
1. Test if the value of the variable count is greater than 10. If it is,
print “Count is greater than 10”, otherwise, print “Count is less
than 10”.
2. Test if the value of the variable number is an odd number. If it is,
print “This number is not an even number” otherwise, print “This
number is an odd number.”
3. Test if the value of the variable age is greater than 18 and less
than or equal to 40. If it is, print “You’re not getting old!”
otherwise, print “You’re not getting younger.”
26
NESTED if STATEMENT
NESTED if STATEMENT
break statement
• defines the end of a case.
switch case STATEMENT
default statement
• use in defining message to be displayed
that is outside of the given case.
switch case
int grade = 92;
switch case
switch (grade) {
case 100:
System.out.println(“Excellent!”);
break;
case 90:
System.out.println(“Good!”);
break;
case 80:
System.out.println(“Study Harder!”);
break;
default:
System.out.println(“You failed!”);
}
Write a program to output summation of a
set of numbers.
Example Output:
Please input how many numbers you
would like to add: 4
Check your Please enter a value: 3
Please enter a value: 1
Understanding Please enter a value: 2
Please enter a value: 3
The sum of the 4 values is 9
Write a program which
should be able to reproduce
the following example
Check your output:
Understanding
QUESTIONS?
Thank you!
Credits
⊳ https://www.edureka.co/blog/what-is-java/
⊳ http://ecomputernotes.com/java/what-is-java-
language/types-of-java-programs
⊳ https://app.codechum.com/teacher/classes/504/tim
eline/lessons/java-conditionals/if-statement
OUR LADY OF FATIMA UNIVERSITY
COLLEGE OF COMPUTER STUDIES
OUR LADY OF FATIMA UNIVERSITY
COLLEGE OF COMPUTER STUDIES
OOPR211
OBJECT-ORIENTED
PROGRAMMING I
MIDTERM ROADMAP
CONDITIONAL LOOPING EXCEPTION
STATEMENTS STATEMENTS HANDLING
7 9 11
8 10 12
1 2
⊳ For Loop
⊳ While Loop
⊳ Do…while Loop
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.
CATEGORIES
• Execute a sequence of statements multiple
for loop times and abbreviates the code that manages
the loop variable.
for LOOP
A REPETITION CONTROL STRUCTURE THAT
ALLOWS YOU TO EFFICIENTLY WRITE A LOOP
THAT NEEDS TO BE EXECUTED A SPECIFIC
NUMBER OF TIMES.
for LOOP STATEMENT
while(Boolean_expression) {
// Statements
}
while
• 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.
while LOOP DEMO
OUTPUT
Understanding
3 do…while loop
do…while LOOP
A DO...WHILE LOOP IS SIMILAR TO A WHILE
LOOP, EXCEPT THAT A DO...WHILE LOOP IS
GUARANTEED TO EXECUTE AT LEAST ONE
TIME.
do…while LOOP STATEMENT
do {
// Statements
}while(Boolean_expression);
do-while
do…while LOOP DEMO
OUTPUT
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
EXERCISES:
1. Test if the value of the variable count is greater than 10. If it is,
print “Count is greater than 10”, otherwise, print “Count is less
than 10”.
2. Test if the value of the variable number is an odd number. If it is,
print “This number is not an even number” otherwise, print “This
number is an odd number.”
3. Test if the value of the variable age is greater than 18 and less
than or equal to 40. If it is, print “You’re not getting old!”
otherwise, print “You’re not getting younger.”
27
Write a program to output summation of a
set of numbers.
Example Output:
Please input how many numbers you
would like to add: 4
Check your Please enter a value: 3
Please enter a value: 1
Understanding Please enter a value: 2
Please enter a value: 3
The sum of the 4 values is 9
Write a program which
should be able to reproduce
the following example
Check your output:
Understanding
QUESTIONS?
Thank you!
Credits
⊳ https://www.edureka.co/blog/what-is-java/
⊳ http://ecomputernotes.com/java/what-is-java-
language/types-of-java-programs
⊳ https://app.codechum.com/teacher/classes/504/tim
eline/lessons/java-loops/for-loop/
33
OUR LADY OF FATIMA UNIVERSITY
COLLEGE OF COMPUTER STUDIES
OUR LADY OF FATIMA UNIVERSITY
COLLEGE OF COMPUTER STUDIES
OOPR211
OBJECT-ORIENTED
PROGRAMMING I
MIDTERM ROADMAP
CONDITIONAL LOOPING EXCEPTION
STATEMENTS STATEMENTS HANDLING
7 9 11
8 10 12
1 2
⊳ Exception Hierarchy
⊳ Creating Exceptions
⊳ Try, Catch, and Finally
What is
EXCEPTION?
Exception
ERROR
SERIOUS ERRORS IN THE JAVA RUNTIME
SYSTEM
Types of Exceptions
EXCEPTION/ CHECKED
THE CLASSES WHICH DIRECTLY INHERIT
THROWABLE CLASS EXCEPT
RUNTIMEEXCEPTION AND ERROR ARE KNOWN
AS CHECKED EXCEPTIONS NORMAL ERRORS
THAT CAN OCCUR DURING THE EXECUTION
OF A PROGRAM
Types of Exceptions
RUNTIME
EXCEPTION/UNCHECKED
ENCOMPASSES ALL EXCEPTIONS WHICH CAN
ORDINARILY HAPPEN AT RUNTIME
COMMON EXCEPTIONS
⊳ ArithmeticException – caused by ⊳ IOException – caused by general I/O
math errors such as division by zero failures, such as inability to read from
⊳ ArrayIndexOutOfBoundsException – a file
caused by a bad array index ⊳ NullPointerException – caused by
⊳ ArrayStoreException – caused when referencing a null object
a program tries to store the wrong ⊳ NumberFormatException – caused
type of data in an array when a conversion between string
⊳ FileNotFoundException – caused by and number fails
an attempt to access a nonexistent
file
COMMON EXCEPTIONS
⊳ OutOfMemoryException – caused when there is not
enough memory to allocate a new object
⊳ SecurityException – caused by a security violation
such as when an applet tries to perform an action
not allowed by the browser’s security setting
⊳ StackOverflowException – caused when a program
attempts to access a nonexistent character position
in a string
2 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.
int method() throws IOException {
...
}
try {
//statements that can generate an exception
//here
}
catch and finally blocks…
The catch Block
A catch block is a segment of code that can handle an exception that might be thrown by the
try block that precedes it. A catch block has the following format:
try {
}
catch (ExceptionType name) {
//action your program will do if an exception
//of a certain type occurs
}
finally blocks…
EXAMPLE:
First way :
assert expression;
Second way :
assert expression1 : expression2;
Assertion In Java
import java.util.Scanner;
class Test
{
public static void main( String args[] )
{
int value = 15;
assert value >= 20 : " Underweight";
System.out.println("value is "+value);
}
}
Why use Assertions?
▸ 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.
Assertion In Java
Assertion Vs Normal Exception Handling
messages
▸ Assertions should not be used to check
arguments.
QUESTIONS?
Thank you!
Credits
⊳ https://www.edureka.co/blog/what-is-java/
⊳ http://ecomputernotes.com/java/what-is-java-
language/types-of-java-programs
OUR LADY OF FATIMA UNIVERSITY
COLLEGE OF COMPUTER STUDIES