Lecture 01 - Control Statements (Part I); Assignment, ++ and -- Operators
Lecture 01 - Control Statements (Part I); Assignment, ++ and -- Operators
Control Statements:
Part I; Assignment,
++ and -- Operators
From the textbook
Java How to Program 11/e
• UML notes
§ Like comments in Java.
§ Rectangles with the upper-right corners folded over.
§ Dotted line connects each note with the element it describes.
§ Activity diagrams normally do not show the Java code that implements the
activity. We do this here to illustrate how the diagram relates to Java code.
• More information on the UML
§ visit www.uml.org
4 Control Structures
Selection Statements in Java
}Three types of selection statements.
}if statement:
§ Performs an action, if a condition is true; skips it, if false.
§ Single-selection statement—selects or ignores a single action (or group of
actions).
}if…else statement:
§ Performs an action if a condition is true and performs a different action if
the condition is false.
§ Double-selection statement—selects between two different actions (or
groups of actions).
}switch statement
§ Performs one of several actions, based on the value of an expression.
§ Multiple-selection statement—selects among many different actions (or
groups of actions).
4 Control Structures
Repetition Statements in Java
}Three repetition statements (also called iteration statements or
looping statements)
§ Perform statements repeatedly while a loop-continuation condition
remains true.
}while and for statements perform the action(s) in their
bodies zero or more times
§ if the loop-continuation condition is initially false, the body will not
execute.
}The do…while statement performs the action(s) in its body
one or more times.
}if, else, switch, while, do and for are keywords.
§ Appendix C: Complete list of Java keywords.
4 Control Structures
Summary of Control Statements in Java
} Every program is formed by combining the sequence statement, selection
statements (three types) and repetition statements (three types) as
appropriate for the algorithm the program implements.
} Can model each control statement as an activity diagram.
§ Initial state and a final state represent a control statement’s entry point and exit
point, respectively.
§ Single-entry/single-exit control statements
§ Control-statement stacking—connect the exit point of one to the entry point of the
next.
§ Control-statement nesting—a control statement inside another.
5 if Single-Selection Statement
• Pseudocode
If student’s grade is greater than or equal to 60
Print “Passed”
• If the condition is false, the Print statement is ignored, and the
next pseudocode statement in order is performed.
• Indentation
§ Optional, but recommended
§ Emphasizes the inherent structure of structured programs
• The preceding pseudocode If in Java:
if (studentGrade >= 60)
System.out.println("Passed");
• Corresponds closely to the pseudocode.
5 if Single-Selection Statement