ch04 - 4 Flow Control, Exceptions, and Assertions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4 Flow Control, Exceptions, and Assertions CERTIFICATION OBJECTIVES • Writing Code Using if and switch Statements • Writing Code Using Loops • Handling Exceptions • Working with the Assertion Mechanism ✓ Two-Minute Drill Q&A Self Test C an you imagine trying to write code using a language that didn’t give you a way to execute statements conditionally? In other words, a language that didn’t let you say, “If this thing over here is true , then I want this thing to happen; otherwise, do this other thing instead.” Flow control is a key part of most any useful programming language, and Java offers several ways to do it. Some, like if statements and for loops, are common to most languages. But Java also throws in a couple flow control features you might not have used before—exceptions and assertions. The if statement and the switch statement are types of conditional/decision controls that allow your program to perform differently at a “fork in the road,” depending on the result of a logical test. Java also provides three different looping constructs— for , while , and do-while —so you can execute the same code over and over again depending on some condition being true . Exceptions give you a clean, simple way to organize code that deals with problems that might crop up at runtime. Finally, the assertion mechanism, added to the language with version 1.4, gives you a way to do debugging checks on conditions you expect to smoke out while developing, when you don’t necessarily need or want the runtime overhead associated with exception handling. With these tools, you can build a robust program that can handle any logical situation with grace. Expect to see a wide range of questions on the exam that include flow control as part of the question code, even on questions that aren’t testing your knowledge of flow control. CERTIFICATION OBJECTIVE Writing Code Using if and switch Statements (Exam Objective 2.1) Write code using if and switch statements and identify legal argument types for these statements. The if and switch statements are commonly referred to as decision statements . When you use decision statements in your program, you’re asking the program to evaluate a given expression to determine which course of action to take. We’ll look at the if statement first. 2 Chapter 4: Flow Control, Exceptions, and Assertions if-else Branching The basic format of an if statement is as follows: if (booleanExpression) { System.out.println("Inside if statement"); } The expression in parentheses must evaluate to a boolean true or false result. Typically you’re testing something to see if it’s true , and then running a code block (one or more statements) if it is true , and (optionally) another block of code if it isn’t. We consider it good practice to enclose the blocks within curly braces, even if there’s only one statement in the block. The following code demonstrates a legal if statement: if (x > 3) { System.out.println("x is greater than 3");System....
View Full Document

This note was uploaded on 09/17/2009 for the course CMPE 126 taught by Professor Gpour during the Fall '07 term at San Jose State University .

Page1 / 80

ch04 - 4 Flow Control, Exceptions, and Assertions...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online