0% found this document useful (0 votes)
53 views

Lesson3 Java Conditional Statements

Control structures or flow of control is a block of programming that checks variables and chooses a direction in which a condition body should go based on given parameters. There are three basic types of control structures: sequence, selection/conditional, and repetition/iteration. Conditional statements are used to make decisions based on conditions and execute code sequentially when there is no condition or change the execution flow based on a condition's result. They include if, else if, else, and switch statements.

Uploaded by

Joshua Delgado
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)
53 views

Lesson3 Java Conditional Statements

Control structures or flow of control is a block of programming that checks variables and chooses a direction in which a condition body should go based on given parameters. There are three basic types of control structures: sequence, selection/conditional, and repetition/iteration. Conditional statements are used to make decisions based on conditions and execute code sequentially when there is no condition or change the execution flow based on a condition's result. They include if, else if, else, and switch statements.

Uploaded by

Joshua Delgado
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/ 20

CONTROL STRUCTURES

CONTROL STRUCTURES

or flow of control is a block of programming


structure that check variables and choose a
direction in which condition body should go
based on given parameters.
3 BASIC TYPES OF CONTROL STRUCTURES

 Sequence
 Selection/Conditional
 Repetition/Iteration
SEQUENTIAL FLOW

Sequential execution Statement 1

takes place when


statements are
Statement 2
executed one after
another in order
Statement 3
CONDITIONAL FLOW

The selection structure used


for decisions, branching – Test
False
Expression

choosing between 2 or Statement if


False
more alternative paths. True

Statement if
 If Statement True

 Switch Case
ITERATION FLOW

Loops can execute a block of code Initialization (Counter)

as long as a specified condition is


False
reached. Condition

It repeats a collection of the same Update counter


statement multiple times while the True
statement is true. Loop when
True
 While
STOP
 For
CONDITIONAL
STATEMENTS
CONDITIONAL STATEMENTS

• Conditional Statements are used to make decisions based on


the conditions.
• Conditional statements execute sequentially when there is
no condition around the statements.
• If you put some condition for a block of statements, the
execution flow may change based on the result evaluated
by the condition.
CONDITIONAL STATEMENTS

Use if to specify a block of code to be executed, if a specified


condition is true
Use else to specify a block of code to be executed, if the same
condition is false
Use else if to specify a new condition to test, if the first condition
is false
Use switch to specify many alternative blocks of code to be
executed
CONDITIONAL STATEMENTS (IF STATEMENTS)
Problem Case #1
Display “Positive Number” if the value is 1.

Problem Case #2
Display “Positive Number” if the value is a positive number (1,2,3..so
on...)

Problem Case #3
Display “Positive Number” if the value is a positive number.
Otherwise, display “Negative Number”.

Problem Case #4
Display “Zero” if the value is zero (0). Display “Positive Number” if the
number is a positive number. Otherwise, display “Negative Number”.
CONDITIONAL STATEMENTS - IF

Use the if statement to specify a block of Java


code to be executed if a condition is true.

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
CONDITIONAL STATEMENTS - IF

Problem Case #1

Display “Positive Number” if the value is 1.

OUTPUT:
Positive Number
CONDITIONAL STATEMENTS - IF
Problem Case #2

Display “Positive Number” if the value is a positive


number (1,2,3..so on...)

OUTPUT:
Positive Number
CONDITIONAL STATEMENTS – IF/ELSE

Use the else statement to specify a block of code


to be executed if the condition is false.

https://www.w3schools.com/java/java_conditions.asp
CONDITIONAL STATEMENTS - IF/ELSE
Problem Case #3

Display “Positive Number” if the value is a positive


number. Otherwise, display “Negative Number”.

OUTPUT:
Negative Number
CONDITIONAL STATEMENTS – IF/ELSE IF/ELSE

Use the else if statement to specify a new


condition if the first condition is false.

https://www.w3schools.com/java/java_conditions.asp
CONDITIONAL STATEMENTS - IF/ELSE IF/ELSE
Problem Case #4

Display “Zero” if the value is zero (0). Display “Positive Number” if the
number is a positive number. Otherwise, display “Negative Number”.

OUTPUT:
Zero
CONDITIONAL STATEMENTS – SWITCH CASE

Use the switch statement to select one of many


code blocks to be executed.

https://www.w3schools.com/java/java_switch.asp
CONDITIONAL STATEMENTS – SWITCH CASE

The switch expression is evaluated once.


The value of the expression is compared with the values
of each case. If there is a match, the associated block of
code is executed.
break takes the flow of control out of the switch
statement
default works if no match on the cases above
https://www.w3schools.com/java/java_switch.asp
CONDITIONAL STATEMENTS (SWITCH CASE)
Problem Case #5
Display the weekday name based on the following values:

1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
OUTPUT:
Wednesday

You might also like