Java Program Flow Control
This article separately organizes the knowledge points of loop structures:
As mentioned before, loop structures are divided into: for loop, while loop, do...while Loops are the three most basic loop structures; versions after JDK 1.5 also provide a foreach loop for traversing arrays and collections.
The four components of the loop statement:
Initialization part
Loop condition part
Loop body part
Iteration part
for loop:
for (initialization condition; loop condition; iteration part){
Loop body
}
1 public class TestFor {2 public static void main(String[] args) {3 //基础for循环,讲一个语句打印多次4 for(int i=0; i
Exercise: Print 1-100 All even numbers
1 public class TestFor { 2 public void PrintNum(){ 3 for(int i=1; i0 5 System.out.println("i="+i); 6 } 7 } 8 } 9 public static void main(String[] args) {10 TestFor testFor = new TestFor();11 testFor.PrintNum();12 }13 }
Exercise: Write code to loop from 1 to 150 and print a value in each line. In addition, you need to print "foo" in each line that is a multiple of 3, in Print "biz" on every line that is a multiple of 5,
Print "baz" on every line that is a multiple of 7
1 public class TestFor { 2 public void FooBizBaz(){ 3 for(int i=1; i
Note: Must It should be noted that else if(){} cannot be used in this question. Once used, it will be impossible to print three fields at the same time in the number of lines that meet the multiples of 3, 5, and 7 at the same time, because once one of them meets the judgment condition, it will not Execute the if judgment statement below to directly jump out of the current loop and execute the next loop.
There are also many small basic for loop algorithms, such as printing the sum of all odd numbers from 1 to 100, printing out all the narcissus numbers (you can search for the daffodil numbers yourself), these questions can Practicing on your own will help strengthen your understanding of the for loop, so I won’t go into details here.
while loop:
Initialization condition
while (loop condition){
Loop body
Iteration condition
}
Example: Output all even numbers within 1-100
1 public class TestWhile { 2 public void evenNum(){ 3 int i = 1; //初始化条件 4 while(i
Note: The for loop and the while loop can be transformed into each other, because they have the same four parts, but the four parts are placed in different positions.
Another looping method of while loop:
do...while loop:
Initialization conditions
##do{
Loop body
Iteration condition
}while( Loop condition);
It can be seen from the above structure that the do...while loop first performs a loop to determine whether the loop condition is satisfied. If it is satisfied, Proceed to the next cycle, and stop the cycle if it is not satisfied.
Example: Print all even numbers from 1-100
1 public class TestDoWhile { 2 public void evenNum(){ 3 int i = 1; 4 do{ 5 if(i % 2 == 0){ 6 System.out.println("i="+i); 7 } 8 i++; 9 }while(i <div class="cnblogs_code"> <p style="margin-left: 30px"><span style="color: #0000ff">do...while和while循环的区别:</span></p> <p style="margin-left: 30px"><span style="color: #0000ff">do...while循环中 初始化条件即使不满足循环条件也会直接执行一次循环体再进行循环条件判断,所以循环体至少被执行一次,而while循环是必须满足循环条件才会执行循环体。</span></p> <h2 id="span-style-color-嵌套循环-span"><span style="color: #000000">嵌套循环</span></h2> <p><span style="color: #000000"> <strong>顾名思义,嵌套循环就是再一个循环中还能再声明一个循环</strong></span></p> <p><strong><span style="color: #000000"> 几种嵌套方式:</span></strong></p> <p><strong><span style="color: #000000"> 1.for循环中能够嵌套一个for或多个for;</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">2.while循环中可以嵌套一个或多个while循环;</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">3.for循环中可以嵌套一个或多个while循环;</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">4.while循环中可以嵌套一个或多个for循环;</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">5.for循环中可以嵌套一个或多个for/while循环;</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">6.while循环中可以嵌套一个或多个for/while循环。</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">示例:</span></strong></p> <p style="margin-left: 30px"><strong><span style="color: #000000">For之间嵌套</span></strong></p> <div class="cnblogs_code"><pre class="brush:php;toolbar:false"> 1 public class TestForFor { 2 /*打印出 ***** 3 * ***** 4 * ***** 5 * ***** 6 * */ 7 public void forQianTao(){ 8 for(int i = 1; i
其它几种嵌套循环可以参考上面的示例,结构类似。自己可以进行练习,如通过嵌套循环打印九九乘法表、或者打印一个由星号组成的菱形图案,每个星号之间要有一个空格,这些题可以加强对嵌套循环的理解。
The above is the detailed content of Java program flow control example tutorial. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools