Beruflich Dokumente
Kultur Dokumente
Decision Control
Structures
Types include:
- If
- If-Else
- If-Else-If
- Switch Structure
Decision Control
Structures
If Structure
- The if statement specifies that a statement ( or block
of code) will be executed if and only if a certain Boolean
statement is True.
The general format of an if statement is:
if (expression)
Statement or block;
or
if (expression) statement;
If Structure
no
Boolean
Expression
yes
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!);
If Structure
If Structure
If Structure
Example 2:
int grade = 68;
if (grade > 60)
{
System.out.println(Congratulations!);
System.out.println(You Passed!);
}
If-Else Structure
if (expression)
statement1;
else
statement2;
Or
if (expression) statement1;
else statement2;
If-Else Structure
no
Boolean
Expression
yes
If-Else Structure
Example 1
int grade = 68;
if (grade > 60)
System.out.println(Congratulations!);
else
System.out.println(Sorry you failed!);
If-Else Structure
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!);
System.out.println(Try again!);
}
The statement in the else clause of an ifelse block can be another if-else structure.
{
statement1;
}
else if (expression2) {
statement2;
}
else {
statement3;
{
no
Boolean
Expression
no
yes
Boolean
Expression
yes
Common errors