[go: up one dir, main page]

0% found this document useful (0 votes)
30 views42 pages

OOPR211 Module 5

Uploaded by

Beast MO Damn
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)
30 views42 pages

OOPR211 Module 5

Uploaded by

Beast MO Damn
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/ 42

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

CONDITIONAL LOOPING MIDTERM


STATEMENTS STATEMENTS EXAMINATION
5
Module : JAVA CONTROL STATEMENTS –
CONDITIONAL
STATEMENTS
Module Objectives:

1 2

Define what is a Practice problem


control structure and solving solution using
differentiate the types a different approach
of conditional like conditionals
statements. statement.
Topics:

⊳ Understanding if statements
⊳ Complex conditionals
⊳ The switch, case, and break statements
Java Statement

In Java, a statement is one or more lines


of code ending with a semi-colon (;).

It generally contains expressions


(expressions have a value).
Java Statement

⊳ Simple Statement ⊳ Compound Statement or


Block
▸ Basic building blocks of a
program ▸ Used to organize simple
▸ One or more lines terminated statements into complex
by a semicolon (;) structures
▸ One or more statements
printf(Hello, World!”);
enclosed by braces { }
▸ Recommended to indent
int main() { blocks for readability
System.out.println (“Hello, ”);
System.out.println (“World!”);
}
Control Structure

⊳ Control structures allow to change the ordering of


how the statements in a program is executed
⊳ Types of control structures:
▸ Decision control structures

▸ allow to select specific sections of code to be


executed
▸ Repetition control structures
▸ allow to execute specific sections of the code a
number of times
Control Structure

⊳ Java statements that allows a programmer to select


and execute specific blocks of code while skipping
other sections.
⊳ Include types such as:
▸ If

▸ If-Else

▸ If-Else-If

▸ Switch structure
Understanding
1 if statements
THE if STATEMENT

The simple if statement has the following syntax:


if (<boolean expression>)
<statement action>

THE IF STATEMENT EXECUTES A BLOCK OF CODE


ONLY IF THE SPECIFIED EXPRESSION IS TRUE.
THE if STATEMENT
THE if STATEMENT
Every time Java encounters an if statement,
1. It determines whether expression is true or
false.
2. If expression is true, then Java executes
statement.
3. If expression is false, then Java ignores or skips
the remainder of the if statement and proceeds
to the next statement.
THE if STATEMENT
Making a decision involves choosing between two alternative
courses of action based on some value within a program.

NO YES
Boolean
expression
THE if STATEMENT

If 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 (grade >= 75)


System.out.println(“You Passed!”);
THE if STATEMENT

If Structure: Example 2

int grade = 68;


if (grade > 60)
{
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”);
}
EXERCISES:

1. Test if the value of the variable count is greater than 10. If the
answer is yes, print “Count is greater than 10”.

2. Test if the value of the variable number is an odd number. If the


answer is yes, print “This number is not an even number.”

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

⊳ Making a decision involves


choosing between two
alternative courses of NO YES
Boolean
action based on some value expression
within a program.
THE if-else STATEMENT

If-Else Structure: Example 1


int grade = 68;
if (grade > 60)
System.out.println(“Congratulations!”);
else
System.out.println(“Sorry you failed!”);
THE if-else STATEMENT
If-Else Structure: Example 2
int grade = 68;
if (grade > 60) {
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”);
}
else {
System.out.println(“Sorry you failed!”);
}
THE if-else STATEMENT
If-Else Structure: Example 3
int choice = 0;
if (choice == 1)
{
num = 1;
num2 = 10;
}
else
{
num = 2;
num2 = 20;
}
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.”
26
NESTED if STATEMENT
NESTED if STATEMENT

⊳ A nested if is an if statement that if (condition1)


is the target of another if or else. {
Nested if statements means an if // Executes when
statement inside an if statement. condition1 is true
Yes, java allows us to nest if if (condition2)
statements within if statements. {
i.e, we can place an if statement
// Executes when
inside another if statement.
condition2 is true
}
}
3 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.
switch case STATEMENT
switch case 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

Special thanks to all the people who made and released


these awesome resources for free:
⊳ Presentation template by SlidesCarnival
⊳ Photographs by Unsplash
Additional References:

⊳ 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

You might also like