Table of Contents
java loop structure
There are not many loop structures used in java. With the deepening of learning, you are more inclined to use loop thinking, as long as you master this kind of thinking , the loop can be used very flexibly" >There are not many loop structures used in java. With the deepening of learning, you are more inclined to use loop thinking, as long as you master this kind of thinking , the loop can be used very flexibly
1. while loop
2. do while loop
3. for loop
4. Special: Enhanced for loop // Mainly used for array traversal, that is, starting from the subscript 0 to the last data
The basic structural pattern is very simple. If you want to use the loop body flexibly, you need to pass A large number of questions are provided for training and this kind of thinking; here are some questions for everyone to experience for themselves, and I will publish the answers in the future.
Home Java javaTutorial A brief introduction to Java loop structures: while loop and do while loop

A brief introduction to Java loop structures: while loop and do while loop

Jul 30, 2018 pm 01:40 PM

java loop structure

There are not many loop structures used in java. With the deepening of learning, you are more inclined to use loop thinking, as long as you master this kind of thinking , the loop can be used very flexibly

  1. while loop: a straightforward operation that is repeatedly performed under a certain condition

  2. do while loop: Compared with the while loop, the do while loop is executed first, and then judged

  3. for loop: Under certain conditions, an operation is performed with a certain regularity

1. while loop

  • while(Boolean expression){

  • Loop body; // As long as the expression result is true, the loop content is executed

  • }

2. do while loop

  • do{

  • Loop body; //Execute the loop once, and then determine the expression result

  • }while(Boolean expression) ;

3. for loop

  • for (initialization①; Boolean expression②; loop condition update④){

  • Loop body ③; //The execution sequence is as marked above. As long as the Boolean expression is still satisfied after the loop condition is updated, the loop body will continue to be executed.

  • }

4. Special: Enhanced for loop // Mainly used for array traversal, that is, starting from the subscript 0 to the last data

  • for (declaration variable: array name) {

  • System.out.println(variable name); //The size of the variable name does not need to be defined. In this loop, it will be customized as The length of the array -1

  • }

The basic structural pattern is very simple. If you want to use the loop body flexibly, you need to pass A large number of questions are provided for training and this kind of thinking; here are some questions for everyone to experience for themselves, and I will publish the answers in the future.

  1. Find the cumulative value of the integers 1 to 100, but it is required to skip all numbers with a single digit of 3.

  2. Input a positive integer N, determine whether the number is a prime number, if it is a prime number, output "N is a prime number", otherwise output "N is not a prime number". Tip: The meaning of a prime number: it is not divisible by any number except 1 and itself.

  3. Print out a pyramid pattern of n lines on the screen, for example, if n=5,The pattern is as follows:

# ***** *******
*********


## 5.
Lucky guess: The game randomly gives a number from 0 to 99 (including 0 and 99), and then asks you to guess what number it is. You can guess a number at random, and the game will tell you whether it's too big or too small, narrowing down the results. After several guesses and hints, the answer was finally revealed. During the game, record the number of times you finally guessed correctly, and announce the result after the game is over.

Tips: Generate random numbers between 0 and 99: int number = (int)(Math.random()*100) 6. Enter two positive integers m and n, Find their greatest common divisor and least common multiple. 7. Find the value of s=a aa aaa aaaa aa...a, where a is a number. For example, 2 22 222 2222 22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the keyboard.

8. A ball falls freely from a height of 100 meters. Each time it hits the ground, it bounces back to half of its original height. When it falls again, how many meters does it pass in total when it hits the ground for the 10th time? How high is the 10th rally?

9. There are four numbers 1, 2, 3, and 4. How many different three-digit numbers can be formed without repeated numbers? How many are they?

10. Request the output sequence 1 2 3 5 8 13 21 .......

Related articles:

javascript process control Statements while loop and do... Detailed explanation of while loop syntax examples

JavaScript For loop and While loop

Related videos:

Methods without parameters and return values ​​- the latest Java complete video tutorial

The above is the detailed content of A brief introduction to Java loop structures: while loop and do while loop. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

How to Share Data Between Steps in Cucumber How to Share Data Between Steps in Cucumber Mar 07, 2025 pm 05:55 PM

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

See all articles