Four weeks JAVA Advanced learning process the first week (Day03) cycle

Today's main learning content is looping constructs, following mainly from the FOR loop, WHILE loop, DO-WHILE loop, keywords break, continue and nested loop structure.

A, FOR loop

(1) FOR loop format:

for(int i = 0 (1)循环变量初始化;i < 10(2)循环条件;i++(3)循环自增量){
(4)循环体
}

FOR循环的执行顺序为:(1)->(2)->(4)->(3)->(2)->(4)->(3)...条件为假,则执行循环体(4)

FOR loop is executed as shown in the flowchart;

Simple exercise: Print Jiujiuchengfa formulas table

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

The output is:

Supplementary knowledge: variable scope

In JAVA, variable scope limited by {}, {a} is equivalent to a spatial effect.

Simple examples:

{
int a = 10;
}
//全局变量
		
int a = 100;
{
	System.out.println(a);
}

IF FOR these structures may be divided and local scope.

The value of the random number, there is a range of random numbers acquired, rules are as follows:

Suppose the random number in the range: [min, max];

Random number acquisition: random.nextInt (max-min + 1) + min;

Two, WHILE loop structure

while loop format;

while (loop condition) {

Cycle operation

}

It is a flowchart of execution cycles:

Note: Use this cycle but must pay attention if the condition is always set up, then it will die if this happens cycle needs to terminate the program.

while more suitable for an unknown number of cycles, only know the condition of the processing loop circulation problems.

A simple example:

//给你10个亿,每天花一半,几天花完?
long money = 1000000000;
int day = 0;
while(money != 0) {
money = money / 2;
day++;
}
System.out.println("10个亿仅需要"+day+"天可以花完!");

Three, DO-WHILE loop

Format do-while loop structure is;

do{

Cycle operation

} While (loop condition)

Execution Flow loop structure:

Note: do ... while loop condition is true whether or not to perform an circulation;

A simple example:

int i = 1;
do {
	System.out.println("先吃一个包子"+i);
	i++;	
    }while(i <= 10);

IV Summary break, continue keyword

break: the end closest thereto is one cycle, continue: the cycle is skipped to continue the next cycle;

break Summary:

(1) end the current cycle of the whole, the implementation of the current cycle below the statement. Ignore any other statements in the loop and loop test conditions.

(2) only one out of the loop, if your loop is nested loops, then you need to follow your nested levels and steady use to break out of.

continue Summary:

(1). Terminates execution of this cycle, i.e. the cycle is skipped after the current statement continue statement has not been executed, followed by the next condition determination cycle. 

(2) The end of the current cycle, is determined for the next cycle.

(3) Termination of the current cycle, but he is not out of the loop, but continues to judge down cycle conditional execution. He can only end once the process cycle, but can not terminate the cycle continues.

文章末尾:感谢worker003用户画的程序流程图,本文进行了引用,再次感谢。

文章总结粗陋浅显,如有不足或者错误,欢迎大家指导,让我们共同学习,共同进步。谢谢大家!

发布了9 篇原创文章 · 获赞 0 · 访问量 3502

Guess you like

Origin blog.csdn.net/gjw9987654321/article/details/104302788