StudyingJAVABasics - Chapter 3 Statements A computer...

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

View Full Document Right Arrow Icon
Chapter 3: Statements A computer program is a compilation of instructions, and each of these instructions is called a statement. There are many types of statements in Java and some—such as if, while, for, and switch —are conditional statements that determine the flow of the program. Even though statements are not features specific to object-oriented programming, they are vital parts of the language fundamentals. This chapter discusses Java statements, starting with an overview and then providing details of each of them. The return statement, which is the statement to exit a method, is discussed in Chapter 4 , " Objects and Classes ." An Overview of Java Statements In programming, a statement is an instruction to do something. Statements control the sequence of execution of a program. Assigning a value to a variable is an example of a statement. x = z + 5; Even a variable declaration is a statement. long secondsElapsed; By contrast, an expression is a combination of operators and operands that gets evaluated. For example, z + 5 is an expression. In Java a statement is terminated with a semicolon and multiple statements can be written on a single line. x = y + 1; z = y + 2; However, writing multiple statements on a single line is not recommended as it obscures code readability. Note In Java, an empty statement is legal and does nothing : ; Some expressions can be made statements by terminating them with a semicolon. For example, x++ is an expression. However, this is a statement: x++; Statements can be grouped in a block. By definition, a block is a sequence of the following programming elements within braces: statements local class declarations local variable declaration statements A statement and a statement block can be labeled. Label names follow the same rule as Java identifiers and are terminated with a colon. For example, the following statement is labeled section A. sectionA: x = y + 1; And, here is an example of labeling a block: start: { // statements } The purpose of labeling a statement or a block is so that it can be referenced by the break and continue statements. The if Statement The if statement is a conditional branch statement. The syntax of the if statement is either one of these two: if ( booleanExpression ) { statement (s)
Background image of page 1

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

View Full Document Right Arrow Icon
} if ( booleanExpression ) { statement (s) } else { statement (s) } If booleanExpression evaluates to true, the statements in the block following the if statement are executed. If it evaluates to false, the statements in the if block are not executed. If booleanExpression evaluates to false and there is an else block, the statements in the else block are executed. For example, in the following if statement, the if block will be executed if x is greater than 4. if (x > 4) {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

StudyingJAVABasics - Chapter 3 Statements A computer...

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

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