{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter III Part I

# Chapter III Part I - Introduction to Control Statements...

This preview shows pages 1–12. Sign up to view the full content.

Introduction to Control Statements IT108 George Mason University

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Selection Statements if Statements switch Statements Conditional Operators
Simple if Statements Boolean Expression true Statement(s) false (radius >= 0) true area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area); false ( A ) ( B ) if (booleanExpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; System.out.println("The area" + " for the circle of radius " + radius + " is " + area); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Note if ( (i > 0) && (i < 10) ) { System.out.println("i is an " + + "integer between 0 and 10"); } ( a ) Equivalent ( b ) if ( (i > 0) && (i < 10) ) System.out.println("i is an " + + "integer between 0 and 10"); Outer parentheses required Braces can be omitted if the block contains a single statement
Caution Common Logic Error if (radius >= 0); { area = radius*radius*PI; System.out.println( "The area for the circle of radius " + radius + " is " + area); } Wrong

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Boolean Expression false true Statement(s) for the false case Statement(s) for the true case
if...else Example if (radius >= 0) { area = radius * radius * 3.14159; System.out.println(“The area for the “ + “circle of radius “ + radius + “ is “ + area); } else { System.out.println(“Negative input”); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Multiple Alternative if Statements if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Equivalent if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0 The condition is false

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0 The condition is false
Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0 The condition is true

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 36

Chapter III Part I - Introduction to Control Statements...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online