Java Loop Basic Type

Source: Internet
Author: User

1. Basic if Selection structure

Format: if (Boolean expression) {

The expression evaluates to True to execute this block of code

}

Example: if (week <=5) {

System.out.println ("Work")

}

2.if ELSE structure

Format: if (Boolean expression) {

The expression evaluates to True to execute this block of code

}else{

If the expression result is flase, execute this code block

}

3. Multiple if Else structures

Format: if (Boolean expression) {

}else if (Boolean expression) {

}else if (Boolean expression) {

}else{

}

Branching structure

1.SWITCH Branch Structure

Switch (byte, short, int, char) {//JDK7 can then be used to determine the string

Case 1:

Execute code

Break

Case N:

Execute code

Break

Default

Execute code

Break

}

2.default Keywords:

In a switch statement, if no case expression matches the related value, the control is passed to the

The statement following the default tag. If there is no default statement, control is passed to switch outside

3.break Keywords:

Indicates abort, break, jump out of current switch branch structure

Note: For equivalence judgments, all case options are not repeatable and do not satisfy a case

option to automatically jump out of the switch statement, you must manually add break statement interrupts

4.Continue Keywords:

End this time, go to the next cycle

Looping statements

1. Concept: A condition that allows a piece of code to cycle through

2. Composition: initial part, cyclic condition, cyclic operation, iteration part

3. Classification:

A) While loop--------first judge and execute

while (Boolean expression) {

}

Application scenario: Clear cycle times.

Example: 1 to 100 statistics of the sum

Int i = 0;

Int sum = 0;

while (i<=100) {

Sum + = i;

I + +;

}

b) do and Loop: Features: First execution, then judgment.

do{

}while (boolean expression);

Application scenario: The number of cycles is ambiguous.

Example: Complete the job first and then check the results.

Import Java.util.Scanner;

public class testbasedowhile{

public static void Main (string[] args) {

Scanner input = new Scanner (system.in);

Tom finished the homework first, the teacher gave the evaluation, unqualified, then rewrite, qualified, then the end

char answer;

do{

System.out.println ("Tom finishes his homework");

SYSTEM.OUT.PRINTLN ("Please input the teacher comments:");

Answer = Input.next (). charAt (0);//y or N

}while (Answer = = ' n ');

}

}

c) The For loop is first judged and then executed

for (initial part; loop condition; iteration part) {

Looping operations

}

Application scenario: Clear cycle times.

Nested loops

1. Concept: Nesting another complete loop structure in a complete loop structure.

2. Graphic Example: Outer control number of rows, the number of control columns in the inner layer

3. Other examples: the outer control cycle times, the inner layer control single cycle operation.

4. Example: Diamond-shaped printing

Java Loop Basic Type

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.