













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of control structures in Java programming language, focusing on selection statements (if, switch, and conditional operators) and repetition statements (while, do-while, and for loops). It includes examples, cautions, and syntax for each control structure.
Typology: Schemes and Mind Maps
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Selection Statements if Statements switch Statements Conditional Operators
Caution
Wrong
if (booleanExpression) { statements-for-the-true-case; } else { statements-for-the-false-case; }
Multiple Alternative if Statements if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; else if (score >= 70) grade = ‘C’; else if (score >= 60) grade = ‘D’; else grade = ‘F’;
case? : body statement 2 ; break; case? : body statement 3 ; break;
...
numOfYears 7 15 30 default Next Statement annualInterestRate=7.25 annualInterestRate=8.50 annualInterestRate=9.0 System.out.println("Wrong number of " + "years, enter 7, 15, or 30"); System.exit(0);
Conditional Operator
Conditional Operator Example 1: if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); Example 2: String result = (num % 2 == 0)? “ is even” : “ is odd” ; System.out.println(“The number ” + result); Example 3: System.out.println((num % 2 == 0)? num + “is even” :num + “is odd”);
Repetitions while Loops do-while Loops for Loops
false true Statement(s) Next Statement Continuation condition? while (continuation-condition) { // loop-body; }
false true Statement(s) Next Statement Continue condition? do { // Loop body; } while (continue-condition);
for (initial-action; loop-continuation-condition; action-after-each-iteration) { //loop body; } int i = 0; while (i < 100) { System.out.println("Welcome to Java! ” + i); i++; } Example: int i; for (i = 0; i < 100; i++) { System.out.println("Welcome to Java! ” + i); }
Caution Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below: for (int i=0; i<10; i++) ; { System.out.println("i is " + i); } Wrong