Java entry _ loop in java _ for a simple example

Source: Internet
Author: User

Java basics-a simple example of a for Loop in a loop

First, let's look at the syntax structure of:


Or more intuitively:


The execution process of the for Loop is:

First, execute expression 1. Generally, initialize the variable, and then execute expression 2 to determine the loop condition. If the result is true, execute the loop body. After the loop body is executed, execute expression 3, change the value of the loop variable, and execute expression 2 again. If the result is true, continue the loop. If the result is false, terminate the loop and execute the following statement.

Expression 1 is executed only once no matter how many times it is executed.

Let's take an example and write a small code to achieve the sum of java code 1 to 100?

1 + 2 + 3 + ...... + 100 =?

* Calculate the sum of 1 to 100
*/
Sum = 0
(I = 1; I <= 100; I ++ = I; System. out. println ("sum =" +

Running result: sum = 5050

A for Loop is suitable for scenarios where the number of cycles is fixed. If the number of loops is not fixed, for example, array loops and set traversal, you can use foreach loops. Foreach loop does not need to know the number of cycles.

In addition, java is used to implement the sum between 1 and 100.

(1 + 100) + (2 + 99) + ...... (50 + 51) =?

                           sum = 0         ( i=1,j=100;i<j;i++,j--             sum+=i+          System.out.println("sum= "+   }

The for loop code for calculating students' scores is as follows:
Assuming that the score of a student's five courses is given, the average value must be output.

Score; sum = 0; avg = 0.0; average SC = average (System. in); System. out. println ("Enter the Student name:" String name = SC. next (); I = 0; (; I <5; I ++) {System. out. println ("Enter the number of five courses" + (I + 1) + "score:" score = SC. nextInt (); sum + = score; System. out. println (name + "before" + (I + 1) + "course score and:" + System. out. the total result of println (name + "is:" + avg = sum/5; System. out. the average score of println (name + "is:" +}

Because this is the basic part of Java and belongs to the initial java, every line of code provides comments. for beginners to better understand it, I replace the English identifier with Chinese characters.

Note: although Chinese characters can run normally in Eclipse, full English naming is recommended in actual development.

Total score = 0 average score = 0 average keyboard input = System. out. println ("Enter the Student name:" String Student name = I = 0 (; I <5; I ++ System. out. println ("Enter the number of course assignments" + (I + 1) + ":" single score = total score + = System. out. println (Student name + "before" + (I + 1) + "course score and:" + System. out. println (the total score of the Student name + "is:" + average score = total score/5 System. out. println (the average score of Student name + "is:" +}

Finally, we can use the for Loop in java to output dot matrix images.

                                             ( i=1;i<=5;i++             ( j=1;j<=5;j++                 System.out.print("*"                         ( i=1;i<=5;i++             ( k=1;k<=5-i;k++                 System.out.print(" "              ( j=1;j<=5;j++                 System.out.print("*"                         ( i=1;i<=5;i++             ( k=1;k<=5-i;k++                 System.out.print(" "              ( j=1;j<=2*i-1;j++                 System.out.print("*"               }

The result is as follows:
*****
*****
*****
*****
*****
*****
*****
*****
*****
*****

*
***
*****
*******
*********

----------------

 

 

 

 

 

 

 

 

 

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.