0% found this document useful (0 votes)
6 views

The While and Do-While Statements (The Java™ Tutorials - Learning The Java Language - Language Basics)

Uploaded by

justininokiumu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

The While and Do-While Statements (The Java™ Tutorials - Learning The Java Language - Language Basics)

Uploaded by

justininokiumu
Copyright
© © All Rights Reserved
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 while and do-while Statements
Conditional Operators
Bitwise and Bit Shift The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
Operators
while (expression) {
Summary of Operators
statement(s)
Questions and Exercises
}
Expressions, Statements,
and Blocks The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The
Questions and Exercises while statement continues testing the expression and executing its block until the expression evaluates to false. Using the while statement to print the values from 1 through 10 can be
Control Flow Statements accomplished as in the following WhileDemo program:
The if-then and if-then-
else Statements class WhileDemo {
The switch Statement public static void main(String[] args){
The while and do- int count = 1;
while Statements while (count < 11) {
The for Statement System.out.println("Count is: " + count);
count++;
Branching Statements
}
Summary of Control
}
Flow Statements
}
Questions and Exercises
You can implement an infinite loop using the while statement as follows:

while (true){
// your code goes here
}

The Java programming language also provides a do-while statement, which can be expressed as follows:

do {
statement(s)
} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always
executed at least once, as shown in the following DoWhileDemo program:

class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}

« 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