Java: Process Control Summary

First, conditional statements

Java want to execute the corresponding statement when a condition is true.

If a single case

int a=6;
if (a==6)
{
    System.out.println("n=6");
}
System.out.println("已跳出循环");

If the statement is a single statement, braces may be omitted. But proposed to retain braces, conducive to reading, not easily confused.

Statement outside the loop will execute.

Single if / else case

int a=6;
if (a==6)
{
    System.out.println("n=6");
}
else 
{
    System.out.println("n!=6");
}
System.out.println("循环外");

Not you die, I die.

if / else multi-drop case

Scanner s = new Scanner(System.in);
System.out.println("请输入一个整数:");
int n = s.nextInt();
if (n==3)
{
    System.out.println("n=3");
}
else if(n>3)
{
    System.out.println("n>3");
}
else 
{
    System.out.println("n<3");
}
System.out.printf("循环之外,都会输出 %d ",n);

The choice of many. .

switch conditional statements

In fact select statement, according to the integer value of the expression, choose from a range section of code to execute.

int n;//double n;错误: 不兼容的类型: 从double转换到int可能会有损失    
switch (n)
	{
        case 1:
            System.out.println("n="+n);
            break;
        case 2:
            System.out.println("n="+n);
            break;
        case 3:
            System.out.println("n="+n);
            break;
        default:
            System.out.println("其他");
            break;
	}
  • switch (xxx), the brackets must be an integer value or an integer value expression that yields ! ! (Byte, short, char, int , enumerated types, String, and can not be a boolean type)
  • XXX case, of course, also need to integer values after case, and the value of the switch statement is an integer value expression and comparison of eleven case , consistent implementation inside the code does not correspond to go down.
  • default after similar else, the other can not be found, then turn it.
  • the break is optional, so the code jumps to the end of the switch body , if not break, the statement will be followed by the implementation part of the case back until it encounters the break.
  • If the latter case multiple conditional execution is the same, the statement is executed only need to write once in place can meet the conditions of the final, simplified structure.

while loop and for loop basic concept ...... directly on the code!

Second, the loop

while loop

int i = 0;
while(i<10)
{
    System.out.println(i);
    i++;
}
System.out.println("跳出循环!");
//输出0 1 2 3 4 5 6 7 8 9 跳出循环!
  • while (xx), in brackets is true, runs a statement or a block, or that sentence statement, proposed to retain the "block" braces , conducive to reading.
  • It is false, while a loop body code is not executed in parentheses .

do ... while loop

int a = 0;
do{
    a++;
    System.out.println(a);

}while(a<0);
System.out.println("跳出循环~");

do {statement} while (xx) , while the difference is simple, at least do ...... structures while the loop body code is executed once , followed by the determination.

for loop

for loop is a common structural support iteration, each iteration using the updated counter or similar variable to control the number of iterations.

for(int i=1;i<=10;i++)
{
    System.out.print(i+" ");
}

About the for loop

  • A first portion for initializing the counter.
  • The second loop portion for detecting execution condition.
  • The third part of the instructions for how to update the counter.

Note :

  • for(double x=0;x!=10;x+=0.1)This condition will never end, because 0.1 can not be represented exactly in binary. Can refer to: About 2.0-1.1 in Java! = 0.9 problem

  • scoped variables for the first part of the statement declared just the whole body of the for loop .

    for(int i=0;i<10;i++){
        System.out.print(i);
    }
    System.out.print(i);//false,跳出循环,i未定义。
    //可以用以下方式:
    /*  int i;
    	for(i=0;i<10;i++)
    ...*/
    
  • The for loop is actually a simplified version of the while loop. The above can be rewritten as:

    int i =0;
    while(i<10){
    	System.out.print(i);
        i++;
    }
    

Before talking about the various looping constructs, there is for recycling, ah, ah while loop can be repeated to complete the action, very convenient. So if a good number of combined cycle once again, is how to achieve the effect of it. In this regard herein, this takes a little exploration of nested loops.

Third, the nested loop

Nested loop: nothing more than a set with a loop Well, in fact, as long as the number of more than two, no matter how many cycles of structure, as long as there are completely contained relationship is completely nested. Nested into the inner loop and outer loop. Nested loop form is uncertain, any type of cycle can be combined together.

nested for loop

The following first to add a for loop for loop to achieve universal multiplication tables:

public class Test{	
	public static void main(String[] args){
		//嵌套循环
        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
	}
}

while loop nest

Then again a while loop nested example of a printing table Jiujiuchengfa formulas:

public class Test{	
	public static void main(String[] args){
        int i = 1;
        while(i<=9)
        {
        	int j = 1;
        	while(j<=i)
        	{
        		System.out.print(j+"*"+i+"="+j*i+"\t");
        		j++;
        	}
        	System.out.println();
        	i++;
        }
	}
}

Of course, the results are the same as above! Output:


to sum up

  • The inner loop can be viewed as the outer loop of the loop, the outer loop variable value declared in the inner loop still exists.
  • First, the outer loop determines a condition, the condition is true, execution loop outer loop, i.e. the inner loop.
  • The inner loop also requires determination condition to the inner loop, the same way the condition is true only performed inside the loop body. Condition is false, then out of the inner loop, i.e. the outer end of the cycle when the cycle.
  • Execution count * = the number of executions inner layer execution times.

In programming, the cycle directly jump is very important, though Java does not provide a goto statement to jump control program, but in order to control the cycle, Java provides a continue, break, and a special label to accomplish a specific interrupt and jump turn, of course, return, this relatively different number. Benpian will make a summary.

Fourth, the break statement

break

In the cycle, if you do not want to wait until the false termination of the cycle, you can use the break to complete.

For example, the following simple example:

for (int i = 0;i<10 ;i++ ) {
    System.out.println(i);
    if(i==1) break;
}
System.out.println("跳出循环");//输出 0 1 跳出循环

Until can, without if(i==1) break;the statement, this cycle will be sequentially outputted 0-9, but with the following sentence, if i is equal to 1 to execute, execute a break statement, directly out of the loop.

continue

continue a bit like giving up a part and start over again.

Still give examples:

for (int i = 0;i<10 ;i++ )
{	
    if(i%2==0) continue;
    System.out.println(i);
}
System.out.println("跳出循环");
//输出1 3 5 7 9 跳出循环
  • continue to ignore the rest of the statements in this cycle, then next cycle, but not as directly as break terminates the loop.
  • Examples of the above, as long as the output of the even part of the back is skipped, it is behind it outputs odd.

return

In fact, return is not dedicated to ending the cycle of keywords, but to the end of a method.

for (int i = 0;i<10 ;i++ ) {
    System.out.println(i);
    if(i==1) return;
}
System.out.println("跳出循环");//输出 0 1 
  • As can be seen, the first example of a break statement changed to return, "out of the loop" and will not be back output, that return is over the entire program.
  • Although the return can end a cycle, but with two different front, he ended the whole method, no matter how many layers of nested loops return hiding inside in.
  • return with break and continue there a lot of different places, you can later return with a value, and the value is returned.

Tag jump

Java does not goto however, continue and break two keywords that belong to interrupt statement after with the "tag", with a similar implementation mechanism and goto jump, can easily control multiple layers of nested loops.

break and continue with similar labels, but there are differences.

Tag needs to be placed before the loop, otherwise what's the meaning acridine, specific forms such as:label:

  • break + labels
outer:
for (int i = 0;i<5 ;i++ ) {
    for (int j = 0;j<3 ;j++ ) {
        System.out.print(" i="+i+" j="+j);
        if(j==1)
        {
            break outer;
        }
        System.out.println();
    }

}
//输出
 i=0 j=0
 i=0 j=1

When j == 1, the encounter break outer statement, leading to the end of the outer tag specifies the cycle , not the end of the loop where the break! Not the end of the cycle where the break! ! ! !

  • continue + labels
outer:
for (int i = 0;i<5 ;i++ ) {
    for (int j = 0;j<3 ;j++ ) {
        System.out.print(" i="+i+" j="+j);
        if(j==1)
        {
            continue outer;
        }
        System.out.println();
    }
}
//输出
 i=0 j=0
 i=0 j=1 i=1 j=0
 i=1 j=1 i=2 j=0
 i=2 j=1 i=3 j=0
 i=3 j=1 i=4 j=0
 i=4 j=1

The value of j never more than one, because every j = 1, encountered continue outer statement ended outer label control loop when this cycle, just start the next cycle, this time from i start i + 1, j it starts from zero.

Reference: "crazy Java handouts"

Published 79 original articles · won praise 62 · views 3470

Guess you like

Origin blog.csdn.net/Sky_QiaoBa_Sum/article/details/104326411