Article From:https://www.cnblogs.com/MrTanJunCai/p/9906734.html

*****************************Circular sentence ***********************************

When a condition is met, doing something (executing a piece of code all the time) is called a loop.

1.Always attend classes when you don’t graduate.
2.When our strength is not enough to sustain our ambition, we must always strive.
3.The gluttonous snake kept going right when it did not touch the wall.

Classification of Cycles

1. forloop

2. whileloop

3. do…while

 

**************************for Programming should go from simple to complex (writing pseudocode)********************************************

for(Expressions 1; 2; 3) {

//Circulatory body
}

//Jump here

forImplementation process:

Expression 1: Initialize expressions, commonly used for initialization functions
Expression 2: Conditional expression
Expression 3: Iterative expression i++i–

First, expression 1 is executed, and expression 1 is executed only once.

If expression 2 is true, the loop body is executed, the loop body is executed, and expression 3 is executed.

Execute expression 2 to continue judging. If expression 2 is true, continue executing the loop body.

Jump out of the loop until the result of expression 2 is false

 

Writing programs should first write pseudo code, and then change pseudo code into real executable code.

Demonstration code:

/**
Authors: zhangzy & lt; BR & gt;
Company: Blue Bridge Software College & lt; BR & gt;
Time: 2017-06-23 am
Function: Demonstrate the use of for loop
Explaining and Writing Procedures from Simple to Complex
*/

public class TestFor{

public static void main(String[] args){

//Demand 1: Add 1 to 100
//The sum of odd numbers of requirements 2: 1 to 100
//Average sum of odd numbers of demand 3: 1 to 100
int sum = 0;

for(int i=1,j=2;i<=100;i++){

if(i%2!=0){//iis odd
sum = sum + i;
}

}

int avg = sum/50;

System.out.println(“1..100The sum of odd numbers is “+sum”;
System.out.println(“1..100The average of the sum of odd numbers: “+avg”;

}
}

*****************************************whileCirculating****************************************************************************

while(Condition) {

//Circulatory body
}

First judge the condition, if the condition is true to execute the loop body, then judge the condition, if the condition is true, continue to execute the loop body;

Exit the loop until the condition is false

 

forEquivalent Conversion of Cycles and While Cycles

1. for————->while

 

2. for ————–> for(;;)

 

for( ; 😉

Amount to

while(true){

}

Dead cycle

Demonstration code:

/**
Authors: zhangzy & lt; BR & gt;
Company: Blue Bridge Software College & lt; BR & gt;
Time: 2017-06-23 am
Function: Demonstrate the use of while loops
1.Explain the Equivalent Conversion of For Cycle and While Cycle

2. for————–>for(;;)
*/

public class TestWhile{

public static void main(String[] args){

int sum = 0;

/**************I. For —> while *****************************
int i = 1;

while(i<=100){

//Circulatory body
sum = sum + i;
//Iterative expression
i++;
}*/

/**************2. For – – – gt; for (;)**********************************************/

/*
for(int i=1;i<=100;i++){

sum = sum + i;

}*/

int i = 1;

for( ; ;){

if(i<=100){

//Circulatory body
sum = sum + i;
//Iterative expression
i++;
}else{

break;//Exit the loop body
}
}

//Jump here

System.out.println(“1..100The cumulative sum of =”+sum”;

}
}

 

Link of this Article: Java Process Control (II)

Leave a Reply

Your email address will not be published. Required fields are marked *