You are on page 1of 12

JAVA

Flow Controls
IF
• Java's if statement is a conditional control flow
structure which allows a program to test for a
condition and select for the execution of a block
of statements depending on the result of the test
• Syntax:
if (condition) {
if-part statements...
} else {
else-part statements...
}
• the condition expression being tested must be a
boolean condition, i.e., its value must evaluate to
either a true or a false value
• the if statement provides an optional else part;
the block of statements associated with the if
part is executed if the condition evaluates to
true; if the condition evaluates to false, the
block of statements associated with the else part
gets executed; in case the else part is not
provided and the condition evaluates to false,
no statement block will be executed
• the comparison (>, <, >=, <=) and equality (==,
!=) operators are typically used to construct the
condition expression evaluated by the if
statement
• the logic conditional operators && and || can be
used to combine simple conditions into more
complex conditions; && returns true if both of
its operands are true, while || returns true if at
least one of its operands is true
• the logic complement operator ! can be used
to negate a condition
• The component statements of the if statement can
consist of just single statements, in which case they
need not be enclosed in braces. It is good programming
practice though to always use statement blocks in the
component statements of the if statement, even when
the block consists only of a single statement.
// allowed... // preferred...
if (x > 0) if (x > 0) {
z = x / 2;
z = x / 2;
} else {
else z = x * 2;
z = x * 2; }
• Always use indentation, whitespace, and
where appropriate, comments to depict
the flow of control of the statement. Note,
however, that the indentation by itself does
not enforce the flow of control!
// badly formed, // misleading...
// but valid... if (x > 0)
if(x >0){ z = x / 2;
z =x/2;} y = x * 3;
else{z= x * 2;} w = y + z;
• Always fully parenthesize complex condition expressions
instead of relying on operator precedence. This should
also be practiced for other expressions used in the
program.
• When testing for equality of expressions which involve
floating-point values, keep in mind that values of type
double and float are stored as binary approximations of
their true decimal values, and may not yield exact
results. This is not true of integer types, whose values
are always exact.
• Results of comparisons between values of type char are
based on the integer value of the Unicode character
code associated with the character. Thus, char values
can be compared with char values as well as numbers.
• String comparisons are performed using
lexicographic order. To test for string equality,
use the methods equalsIgnoreCase and/or
equals of the String class (the equality and
inequality operators have a different meaning
when applied to values of type String).
// case-sensitive string
// equality test...
if (answer.equals("YES")) {
...
}
• For relative string value comparisons, use the
methods compareToIgnoreCase and/or
compareTo.
// relative string value test...
if (s1.compareTo(s2) > 0)
// s1 “greater than” s2...
else
if (s1.compareTo(s2) < 0)
// s1 “less than” s2...
else
// s1 “equal to” s2...
Problem
• Determine if three input integer
values can represent the lengths of
the three sides of a triangle, and, if
so, indicate the type of the triangle
formed (whether it is an equilateral,
isosceles, or scalene triangle), and
compute and output the perimeter as
well as the area of the triangle.
Analysis
• The sum of the lengths of any two sides of a
triangle is always greater than the length of the
third side.
• A triangle is an equilateral triangle if all its sides
have equal lengths. It is an isosceles triangle if
two of its sides have equal lengths. It is a
scalene triangle if no sides have equal lengths.
• The perimeter of a triangle is equal to the sum of
the lengths of its sides.
• The area of a triangle, given the length of its
three sides, can be calculated using Heron’s
formula, given by
• area =  s (s - a) (s - b) (s - c)
– where a, b, c are the lengths of the sides and
– s is the semiperimeter (i.e., perimeter / 2)
• Sample Output 12
5
12

ISOSCELES
3
29
4 29.341736485763757
5
7
SCALENE 3
12 2
6.0 Values cannot form a valid triangle

You might also like