当前位置:网站首页>Application practice of day13 for loop distinguish the application of traversing break continue
Application practice of day13 for loop distinguish the application of traversing break continue
2022-06-27 17:36:00 【33 year old Java enthusiast】
for loop
for( initialization , Boolean expression , to update ){ Code statements }
for( Defining variables , Boolean expression , expression ){ Output }
IDEA Shortcut 100.for It will pop up automatically
package com.ckw.blog.select;
public class demo_for {
public static void main(String[] args) {
// Initial value // conditional // iteration
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
}
Although all circular bodies can be used while perhaps do…while Realization , however java Provides another efficient statement .
for The nature of the cycle
for A generic structure that supports iteration in a circular fashion , Is the most effective , The most flexible structure
for The number of times the loop is executed is determined before execution
for Circulation and while Difference between cycles
- for The number of times the loop is executed is determined before execution ,while Is to judge while executing .
for Attention to circulation
expression :for( Initial value ; conditional ; to update ){ Output }
Initial value ( Optional ) The default is 0
conditional ( Optional ) The default is true
Update the iteration ( Optional ) Default continuous update
for(;{} // Dead cycle
If 3 All conditions are empty , Will fall into the trap of while(true) The same loop
practice :
need 1-1000 Between magic and even sum
package com.ckw.blog.select;
public class demo_for02 {
// need 1-1000 Between magic and even sum
public static void main(String[] args) {
int oddsum = 0;// Define an even number and a variable
int evensum = 0;// Define an odd number and a variable
for (int i = 0; i <=100; i++) {
if (i%2!=0){
oddsum+=i;
}else {
evensum+=i;};
}
System.out.println(" Even sum is "+oddsum);
System.out.println(" Odd sum is "+evensum);
}
}
To calculate the 1-1000 Can be 3 Divisible number , And change one line for every three numbers
package com.ckw.blog.select;
public class demo_for03 {
// To calculate the 1-1000 Can be 3 Divisible number , And change one line for every three numbers
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
if (i%3==0){
System.out.print(i+"\t");
if(i%9 == 0){
System.out.println();
}
}
}
}
}
99 Multiplication table
Programming thinking
package com.ckw.blog.select;
public class demo_for {
public static void main(String[] args) {
//99 Multiplication table
/* Print the first column first , Consider the second column after the first column is normal And then put the fixed 1, Then wrap it in a circular package Remove duplicates Adjust the style again */
for (int j = 1; j <=9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+j*j+"\t");
}System.out.println();
}
}
}
Output triangle
package com.ckw.blog.select;
public class demo_3jiaoxing {
public static void main(String[] args) {
for (int j = 0; j <=5; j++) {
for (int i = 5; i >=j; i--) {
System.out.print(" ");
}
for (int i = 0; i <=j; i++) {
System.out.print("*");
}
for (int i = j; i >0; i--) {
System.out.print("*");
}
System.out.println("");
}
}
}
for Enhanced
Traversal array .
for( Declare variables : Array ){ Output }
package com.ckw.blog.select;
public class demo_for_04 {
public static void main(String[] args) {
// enhance for loop , Traversal array
int[] numbers ={
10,20,30,40};
for(int x:numbers){
System.out.println(x);
}
// The original way of writing
for(int j=0;j<4;j++){
System.out.println(numbers[j]);
}
}
}
Out of the loop
break Forcibly exit the loop , Do not execute the remaining statements in the loop .
continue Terminate a cycle , The rest are the same .
difference
break It's resignation ,continue It's asking for leave .
break
package com.ckw.blog.select;
public class demo_break_continue {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(i);
if(i>50){
break;}
}
}
}
continue
package com.ckw.blog.select;
public class demo_break_continue {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
if (i == 40){
continue;}// If it is equal to 40, Just skip this loop
System.out.println(i);
}
}
}
边栏推荐
- d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
- Autodesk NavisWorks 2022 software installation package download and installation tutorial
- [leetcode] 2. Add two numbers (user-defined listnode), medium
- Kubernetes basic self-study series | introduction to ingress API
- Construction and management practice of ByteDance buried point data flow
- Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
- tensorflow求解泊松方程
- Why should string be designed to be immutable?
- 09 route guard authenticates URL
- 树莓派初步使用
猜你喜欢

About MySQL: the phenomenon and background of the problem

2022年中国音频市场年度综合分析

428 binary tree (501. mode in binary search tree, 701. insert operation in binary search tree, 450. delete node in binary search tree, 669. prune binary search tree)

Leetcode 46 Full Permutation

10分钟掌握mysql的安装步骤

How much room does Migu video have for development without relying on sports events?

WOx WPM installing the Youdao plug-in

leetcode 70. climb stairs

Oracle concept II

2022 Liaoning's latest eight members (Safety Officer) simulated test question bank and answers
随机推荐
Sword finger offer 22 The penultimate node in the linked list
C语言课程设计
黑马程序员-软件测试基础班-02-30-45工具代开浏览器运行代码,音、视频、测试点,音视频标签,布局标签。超链接语法进阶,绝对路径,相对路径
(5) SPI application design and simulation verification 3 - verification code implementation
About MySQL: the phenomenon and background of the problem
10 minutes to master the installation steps of MySQL
Sliding window + monotone queue concept and example (p1886 Logu)
Autodesk Navisworks 2022软件安装包下载及安装教程
# Cesium实现卫星在轨绕行
Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end
Overview of Inspur Yunxi database executor
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
Missing d3d10 How to repair DLL files? Where can I download d3d10.dll
(5) SPI application design and simulation verification 2 - design code implementation
Oracle概念二
软件测试-测试的概念,单元测试的详细介绍,如何设计测试用例
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
Study on heritability and field experiment design
The European unified charging specification act was passed before the end of the year, and it is planned to expand to products such as laptop and keyboard
Event listening mechanism