J2SE learning notes Program Control Structure

Source: Internet
Author: User

Structural Design of a program: a structured programming language that emphasizes the use of modular and modular components to build a program. The structured program design method can make the logic structure of the program clear, hierarchical, readable, and reliable, thus improving the development efficiency of the program and ensuring the quality of the program, improves program reliability. Generally, the program structure includes the sequence structure, selection structure, and cyclic structure.
Sequential structure: a program segment consisting of statements executed in the written order
Select structure: Also called branch structure. You need to select different statement groups for execution based on the input data and intermediate results. In this case, you must make a judgment based on the value of a variable or expression, to determine which statement to execute or skip which statement not to execute;
In java, the selection structure provides two types of branch structures. Www.2cto.com
Condition branch: determines the program segment of a branch based on the following conditions. It mainly includes the if statement and if... else statement.
Switch branch: Judge based on the value of the given integer expression, and then decide to execute one of the multiple branches, which is implemented by the switch statement.
Loop Structure: when a given condition is set, a program segment is repeatedly executed.
Conditional OPERATOR :'? : 'Determines whether the result is a ':' or ':' expression based on the condition. Using conditional operators, there are three operands in the following format: Condition judgment? Expression 1: expression 2 has similar functions as if... else statements.
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int a = 8, B = 9, max;
Max = (a> B )? A: B;
System. out. println ("max num is:" + max );
}
}

Multiple options: switch statement. First, the value of an integer expression is calculated. The different values of the expression determine to execute one of the multi-branch program segments.
The format is as follows: The selected value can only be a character or constant. Calculate the calculation result of the expression value first, and check whether the selected value after the execution case is met according to the expression value, if all the selected values do not match, execute the statements contained in the default statement, and then exit the switch statement. If the value of the expression to be executed matches the selected value, the statement following the selected value is executed, and the switch statement is left after the break statement is encountered, if no default statement exists, the switch statement is left after the judgment is completed.
Switch (expression ){
Case Selection value 1: Statement Subject 1;
Break;
Case Selection value 2: Statement Subject 2;
Break;
Case Selection value 3: statement subject 3;
Break;
Default: Statement body;
}
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int a = 100, B = 7;
Char character = '/';
Switch (switches ){
Case '+ ':
System. out. println (a + "+" + B + "=" + (a + B ));
Break;
Case '-':
System. out. println (a + "-" + B + "=" + (a-B ));
Break;
Case '*':
System. out. println (a + "*" + B + "=" + (a * B ));
Break;
Case '/':
System. out. println (a + "/" + B + "=" + (a/B ));
Break;
Case '% ':
System. out. println (a + "%" + B + "=" + (a % B ));
Break;
Default:
System. out. println ("location operand ");
}
}
}

Loop statement:
While loop: When the loop body has a statement, you can remove the braces. In a while loop, there is only one judgment condition. It can be any expression. When the value of the judgment condition is true, the loop will be executed once, and then the judgment condition will be tested again and the loop body will be executed again, when the judgment condition is false, the while loop is skipped.
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int I = 1, sum = 0;
While (I <= 10 ){
Sum + = I;
I ++;
}
System. out. println ("1 + 2 + 3 + 4 +... + 10 =" + sum );
}
}

Do ...... while loop: first execute a loop body, and then judge whether the value of the expression needs to continue the loop.
For Loop: this loop can be used when you clearly know the number of times you want to loop. Format;
For (initial values of values; judgment conditions; increase or decrease of values ){
Statement 1 ;...... Statement n
}
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int sum = 0;
For (int I = 0; I <= 10; I ++ ){
Sum + = I;
}
System. out. println ("1 + 2 + 3 + 4 +... + 10 =" + sum );
}
}

Nested in the for Loop:
[Java]
Package hello.csdn.com;
Public class HelloWorld {
Public static void main (String [] args ){
Int I, j;
For (I = 1; I <= 9; I ++ ){
For (j = 1; j <= I; j ++)
System. out. print (I + "*" + j + "=" + (I * j) + "\ t ");
System. out. print ("\ n ");
}
}
}

Loop jump:
Break statement: In the while, for, do ...... Using the break statement in the while or switch statement body or statement group can immediately exit the structure and execute the 1st-hop statement below the structure. A break statement is also called an interrupt statement. It is usually used to exit a loop when appropriate, or terminate a case and jump out of the switch structure.
Continue statement: In the while and do ...... In the while statement's loop body, executing the continue statement will end this loop and immediately test the cycle condition to determine whether to perform the next loop. That is, the function is to terminate this cycle.

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.