Day4 java loop structure, day4java Loop

Source: Internet
Author: User

Day4 java loop structure, day4java Loop

For Loop Structure:
Format: ① initialization condition; ② cyclic condition; ③ iteration condition; ④ cyclic body

for(①;②;③){    //④}

Execution Process: ①-②-④-③ -... -④-③-②: Exit the current loop until the cycle condition is not met.

class myfor{        public static void main(String[] args){                for(int i = 0;i<4;i++){                System.out.println("hello java");                }        }}
root@debian:/home/jeff/java_coding/day004# java myfor hello javahello javahello javahello java

For Loop Execution Process

class myfor1{        public static void main(String[] args){                int j = 1;                for(System.out.print('a');j<4;System.out.print('b'),j++){                System.out.println('c');                }        }}
root@debian:/home/jeff/java_coding/day004# java myfor1 acbcbc

Use the for loop to solve the problem

// Class myfor2 {public static void main (String [] args) {int sum = 0; for (int I = 1; I <100; I ++) {if (I % 2 = 0) {sum + = I ;}} System. out. println (sum );}}
root@debian:/home/jeff/java_coding/day004# java myfor2 2550

Daffodils

/* Output all the numbers of daffodils. The so-called Number of daffodils refers to a three-digit number, and each digit is equal to the number cube and itself. Example: 153 = 1*1*1 + 3*3*3 + 5*5*5*5 */class shuixianhua {public static void main (String [] args) {for (int I = 100; I <1000; I ++) {int bai = I/100; int shi = (I % 100)/10; int ge = I % 10; if (I = bai * bai + shi * shi + ge * ge) {System. out. println (I );}}}}
root@debian:/home/jeff/java_coding/day004# java shuixianhua 153370371407

While Loop

Format:

①while(②){    ④    ③}

The for loop and while can be converted to each other.

Class myfor3 {public static void main (String [] args) {// sum of less than 100 and int I = 1; int sum = 0; while (I <= 100) {if (I % 2 = 0) {System. out. println (I); sum + = I;} I ++;} System. out. println (sum );}}

Do-while

Format: ① initialization condition; ② cyclic condition;③ Iteration conditions;④ Cyclic body

①do{    ④    ③}while(②);

The only difference between do-while and while is that do-while will be executed at least once.

Class mywhile {public static void main (String [] args) {// do-while and while differences int I = 10; int j = 10; do {System. out. println (I); I ++;} while (I <10); while (j <10) {System. out. println (j); j ++ ;}}}

The running will print a 10, that is, I do it once before it ends the loop body.

Infinite Loop: for (;) {} or
While (true ){}
Note: Under normal circumstances, a program termination statement must be provided within an infinite loop, which is implemented using break. If not, it is an endless loop!

Endless Application

// Enter a random number, exit with 0, and output the number of positive and negative numbers import java. util. role; class deadforloop {public static void main (String [] args) {s = new role (System. in); int a = 0; // The number of positive records int B = 0; // The number of negative records // for (;) {while (true) {System. out. println ("enter an integer:"); int num = s. nextInt (); if (num> 0) a ++; else if (num <0) B ++; else break;} System. out. println ("the number of positive numbers is:" + a); System. out. println ("the number of negative numbers is:" + B );}}
Root @ debian:/home/jeff/java_coding/day004 # For java deadforloop, enter an integer: 12. Enter an integer: 11. Enter an integer: 0: 2. The number of negative numbers is: 0.

Loop nesting

Nested loop: the loop can also be declared in the loop. It is equivalent to the overall loop of the inner layer acting as the loop body of the outer loop for (;) {(;;){}}
while(){    for(;;){}}

Loop To print various positions *

Class mystar {public static void main (String [] args) {for (int I = 0; I <4; I ++) {// outer loop for (int j = 0; j <5; j ++) {// inner loop System. out. print ("*");} System. out. println (); // line feed per layer }}}

Running result:

root@debian:/home/jeff/java_coding/day004# java mystar ********************
Class mystar1 {public static void main (String [] args) {for (int I = 1; I <5; I ++) {// outer loop for (int j = 0; j <I; j ++) {// inner loop System. out. print ("*");} System. out. println (); // line feed per layer }}}

Running result:

root@debian:/home/jeff/java_coding/day004# java mystar1 **********
Class mystar2 {public static void main (String [] args) {for (int I = 4; I> 0; I --) {// outer loop for (int j = 0; j <I; j ++) {// inner loop System. out. print ("*");} System. out. println (); // line feed per layer }}}

Running result:

root@debian:/home/jeff/java_coding/day004# java mystar2 **********
Class mystar3 {public static void main (String [] args) {for (int I = 1; I <6; I ++) {// outer loop for (int j = 0; j <I; j ++) {// inner loop System. out. print ("*");} System. out. println (); // line feed per layer} for (int m = 4; m> 0; m --) {// outer loop for (int n = 0; n <m; n ++) {// The inner loop System. out. print ("*");} System. out. println (); // line feed per layer }}}

Running result:

root@debian:/home/jeff/java_coding/day004# java mystar3 *************************
Class mystar4 {public static void main (String [] args) {for (int I = 1; I <6; I ++) {// outer loop for (int j = 0; j <5-i; j ++) {// inner loop System. out. print ("") ;}for (int j = 0; j <I; j ++) {// inner loop System. out. print ("*");} System. out. println (); // line feed per layer} for (int I = 4; I> 0; I --) {// outer loop for (int j = 1; j <6-i; j ++) {// The inner loop System. out. print ("") ;}for (int j = 0; j <I; j ++) {// inner loop System. out. print ("*");} System. out. println (); // line feed per layer }}}

Running result:

root@debian:/home/jeff/java_coding/day004# java mystar4     *    * *   * * *  * * * * * * * * *  * * * *   * * *    * *     * 

Multiplication tips

Class chengfabiao {public static void main (String [] args) {for (int I = 1; I <10; I ++) {// outer loop for (int j = 1; j <= I; j ++) {// inner loop System. out. print (I + "*" + j + "=" + I * j + "");} System. out. println (); // line feed per layer }}}

Running result:

root@debian:/home/jeff/java_coding/day004# java chengfabiao 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

Calculate the prime number less than 100000:

/* All prime numbers less than 100 */class zhishu {public static void main (String [] args) {boolean flag = false; long start = System. currentTimeMillis (); // obtain the current system millisecond count for (int I = 2; I <= 100000; I ++) {// implement the traversal of a natural number less than 100 // determine whether I is a prime number for (int j = 2; j <= Math. sqrt (I); j ++) {if (I % j = 0) {flag = true; break ;}} if (! Flag) {// if (flag = false) {System. out. println (I);} flag = false;} long end = System. currentTimeMillis (); System. out. println ("the time spent is:" + (end-start ));}}

It takes 103 milliseconds.

The break ends the current loop and the continue ends the current loop. This is also true for other languages. You cannot add other statements after the break and continue statements, because they will never be executed!

Break and continue jump out of the outermost layers:

Class breakcontinue {public static void main (String [] args) {label: for (int I = 1; I <5; I ++) {for (int j = 1; j <= 10; j ++) {if (j % 4 = 0) {// break; // continue; break label; // continue label; // This loop that jumps out of this label. In this loop, the outermost layer is used.} System. out. print (j);} System. out. println ();}}}

Use this method to calculate the prime number less than 100000.

Class zhishu1 {public static void main (String [] args) {long start = System. currentTimeMillis (); // obtain the current system millisecond count l: for (int I = 2; I <= 100000; I ++) {// implement the traversal of a natural number less than 100 // determine whether I is a prime number for (int j = 2; j <= Math. sqrt (I); j ++) {if (I % j = 0) {// The next data is tested cyclically in the outermost layer once it is divisible by other numbers; continue l ;}} System. out. println (I);} long end = System. currentTimeMillis (); System. out. println ("the time spent is:" + (end-start ));}}

This code is hosted on my github: day004.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.