Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

The If-Then and If-Then-Else Statements (The Java™ Tutorials - Learning The Java Language - Language Basics)

Uploaded by

justininokiumu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

The If-Then and If-Then-Else Statements (The Java™ Tutorials - Learning The Java Language - Language Basics)

Uploaded by

justininokiumu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Documentation

Search
The Java™ Tutorials Hide TOC

Language Basics
Variables « Previous • Trail • Next » Home Page > Learning the Java Language > Language Basics
Primitive Data Types
Arrays
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no
Summary of Variables
longer available.
Questions and Exercises
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
Operators
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Assignment, Arithmetic,
and Unary Operators
Equality, Relational, and The if-then and if-then-else Statements
Conditional Operators
Bitwise and Bit Shift The if-then Statement
Operators
Summary of Operators The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. For example, the
Questions and Exercises Bicycle class could allow the brakes to decrease the bicycle's speed only if the bicycle is already in motion. One possible implementation of the applyBrakes method could be as follows:
Expressions, Statements,
and Blocks void applyBrakes() {
Questions and Exercises // the "if" clause: bicycle must be moving
Control Flow Statements if (isMoving){
// the "then" clause: decrease current speed
The if-then and if-
currentSpeed--;
then-else Statements
}
The switch Statement
}
The while and do-while
Statements If this test evaluates to false (meaning that the bicycle is not in motion), control jumps to the end of the if-then statement.
The for Statement
Branching Statements In addition, the opening and closing braces are optional, provided that the "then" clause contains only one statement:
Summary of Control
void applyBrakes() {
Flow Statements
// same as above, but without braces
Questions and Exercises
if (isMoving)
currentSpeed--;
}

Deciding when to omit the braces is a matter of personal taste. Omitting them can make the code more brittle. If a second statement is later added to the "then" clause, a common mistake
would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you'll just get the wrong results.

The if-then-else Statement

The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. You could use an if-then-else statement in the applyBrakes method to
take some action if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply print an error message stating that the bicycle has already stopped.

void applyBrakes() {
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
}

The following program, IfElseDemo, assigns a grade based on the value of a test score: an A for a score of 90% or above, a B for a score of 80% or above, and so on.

class IfElseDemo {
public static void main(String[] args) {

int testscore = 76;


char grade;

if (testscore >= 90) {


grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}

The output from the program is:

Grade = C

You may have noticed that the value of testscore can satisfy more than one expression in the compound statement: 76 >= 70 and 76 >= 60. However, once a condition is satisfied, the
appropriate statements are executed (grade = 'C';) and the remaining conditions are not evaluated.

« Previous • Trail • Next »

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.
Préférences en matière de cookies | Ad Choices

You might also like