Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Control Flow

Module Overview
This module covers how the code flows within the Java
environment and how block and conditional statements control
code behavior.
After completing this module, students are expected to
1. understand what expressions are;
2. identify Java statements;
3. work with blocks; and
4. manipulate conditional statements.
Expressions, Statements and Blocks
• Just like in anything you've read from your writing, newspapers
or your favorite book, programming is basically made up of:
Clauses = Expressions
Sentences = Statements
Paragraphs = Blocks
• If you apply this analogy to writing your program or codes, it
wouldn’t be so hard to understand.
• That is why proper naming of the variables is important so
anyone looking at your code can easily understand it.
Expressions
• Expressions consist of methods, variables and
operators which follow certain syntax depending on the
language used, which in this case is Java.
• An expression concludes to a single value after evaluation; an
int, a boolean and so on.
“string1” + “string2”
100 + 1
1 || 2
• These examples are singular expressions. You can also
produce compound expressions like:
1 + 1 * 2 / 3
Expressions (cont.)
• Always watch out when writing compound expressions like the
example above; most of the time that kind of expression will not
produce the value that you want.
1 + 1 * (2 / 3)
Or
1 + ((1 * 2) / 3)
• Always disambiguate your expressions so you spend less time
tweaking and debugging your expressions.
Statements
• Statements are mostly the same as expressions but they are
terminated by a semicolon (;).
• The following expressions can be turned into statements by
ending it with a semicolon;
1. assignment expressions;
2. increments and decrements;
3. invoking methods; and
4. expressions that create objects.
Statements (cont.)
Here are some examples of Statements:
1. Assignment expressions
sStr = “string”;
nValue = 5;
bool = true;
2. Increments/decrements
number++;
number--;
Statements (cont.)
3. Invoking methods
System.out.println(“Hi!”);
System.in.read( );
4. Object creation
Time myTime = new Time( );
Blocks
• Blocks are single or multiple statements within braces.
• They can be one liners or a huge class containing methods and
functions.
Blocks
• An example can be
found at the right.
• The whole “public class
Helloworld { }” is a
block; the “public
static void
main(String[])
args){ }” is another
block; the ifs and
thens are all blocks
sitting inside larger
blocks.
Ifs, Thens and Elses
• In this part we will be taking on conditional statements that
utilize the clauses if, then and else.
• These Java clauses work just like sentences in real life.
• For example: “If” I don’t get candy “then” I’m getting ice
cream. If neither is available then I’ll get something “else”--.
Ifs, Thens and Elses (cont.)
• If you analyze carefully the example, you will notice that the three
clauses are used with corresponding options for every available
item.
Is there candy? Yes. Then go for candy.
Is there candy? No. Then go for ice cream.
Is there ice cream? Yes. Then go for ice cream.
• Is there ice cream?
• No. Then go for something else.
Ifs, Thens and Elses (cont.)
• The syntax in writing an if statement is:
if (<condition>){
statement(s);
}

Example:
Ifs, Thens and Elses (cont.)
• The syntax in writing an if-else statement is:
if (<condition>){
statement(s);
} else {
statement(s);
}
Example:
Ifs, Thens and Elses (cont.)
•And the syntax in writing an if-else if-else statement is:

if (<condition>){ Example:
statement(s);
} else if (<condition>){
statement(s);
} else {
statement(s);
}
• In this example, two
Boolean values to
true and run the
code.
• The 1st “If” statement
checks if the value of
the Boolean candy is
true.
• If the statement gets
a true value then it
executes the
immediate line of
code, which is a print
out.
• If the value
encountered is
false, it will yield
results as shown in
the figure at the
right.
Switches
• Switches normally work just like your regular if-then-else
statements.
• This time though, you can now choose where you want the
code to go and what statement to pick.
• Switch is just another version of an if-else if-else
statement but instead of a simple binary branch, it performs a
multiway branch.
Switches (cont.)
• A switch statement starts with the keyword switch, open
parenthesis, expression, closed parenthesis, open curly brace,
cases (which include statement/s and break) and closed curly
brace.
Switches (cont.)
• The syntax in writing a switch statement is:
switch (<expression>){ Example:
case <value>: statement(s);
break;
case <value>: statement(s);
break;
...
default: statement(s);
break;
}
Switches (cont.)
• NEVER FORGET TO BREAK.
• Not with your partner, with the
cases in switch statement.
• If you forget to place break after a
case, then, it will go to the next
case.
• So in the example at the right,
even if you choose 6 (Friday) but
it doesn’t have a break, it will go
to case 7 which is a Saturday.
Switches (cont.)
• The default case is executed
when none of the cases were
selected.
• In the sample at the right, your
choices are 1-7, so if you input
any value aside from 1-7, the
program will print Invalid
choice.
Switches (cont.)
• Let’s say you have 10 Pesos
and you want something to eat
that maximizes the money you
have.
• Your choices are 1 Peso for a
piece of candy, 10 Pesos for
a cone of ice cream.
• The obvious choice here would
be ice cream, right?
References
• Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/

You might also like