JAVA for Loop

Source: Internet
Author: User
Tags greatest common divisor terminates

The Java for loop is the manual control of the number of cycles for some operations. The simplest, such as printing numbers.


Example: Iterating through an array

1234 int[] a = newint[]{1,2,3,4,5,6,7,8,9,10,11,12};     for(intiloop : a){     System.out.println(iloop);}



The 2.while structure cycle is a type loop (when type loop), which is typically used for situations where the number of loops is not known. The maintenance loop is a conditional expression, the condition is set up to execute the loop body, the condition does not set up the exit cycle.

    • The While statement format is:

650) this.width=650; "Src=" http://e.hiphotos.baidu.com/baike/s%3D250/sign=dd10d818a044ad342abf8082e0a30c08/ 9825bc315c6034a812ecc274ca1349540923763c.jpg "style=" border:0px;margin:0px auto;width:250px;height:86px;/>

while (conditional expression)

{

Circulation body;

}

The conditional expression is judged before each execution of the loop body.

The expression here is the condition that the loop can continue, and the statement is the loop body. As long as the expression is true, the loop in-body statement is executed. Otherwise terminates the loop and executes the loop out-of-body statement.

    • Example: f7_2.c

# include H

Main ()

{

int u,v,temp;

printf ("Please enter two non-negative integers: \ n");

scanf ("% d% d",& u,&v);

while (v!=0)

{

temp=u% v;/* Loop Body statement */

U=v; /* Loop BODY statement */

V=temp; /* Loop BODY statement */

}

printf ("Greatest common divisor is:%d\n", u);

}

Compile and execute the results, the screen displays:

Please enter two non-negative integers:

If you enter from the keyboard: 150 35 and enter, the screen displays:

Greatest common divisor is: 5

In the program, the while Loop has three statements in its body, enclosed in curly braces, to represent a block of statements. When the expression v!=0 is true, the program loops through three statements in the loop body until v!=0 is false, terminates the loop, executes the printf () statement, and displays the greatest common divisor on the screen.

Do While

The DO...WHILE statement structure is until the type loop (until type loop) and is also used for situations where the number of loops is not known. The difference between the do...while and the while is that the do...while structure is the condition of the re-judging of the cyclic body after execution.

    • The Do and statement format is:

650) this.width=650; "Src=" http://g.hiphotos.baidu.com/baike/s%3D250/sign=024d4cfe5ab5c9ea66f304e6e53bb622/ B8389b504fc2d562b3e3aa18e61190ef77c66c5f.jpg "style=" border:0px;margin:0px auto;width:250px;height:72px;/>

Do

{

Circulation body;

}while (conditional expression);

Each time the loop body is executed, the DO...WHILE structure must judge the conditional expression.

After the program enters the Do and loop, the loop body statement is executed first, and then the true and false expression is judged

The next loop, or false to terminate the loop. The characteristic of this loop statement is that the expression is also executed once in the loop body statement.

    • The Do and statement syntax flow is as follows:

In general, the for and while loops are tested at the top of the loop termination condition, and the Do While loop is then tested at the bottom so that the loop body executes at least once.

A menu program can be written using the features of the Do and loop.

    • Examples:

#include <stdio.h>

Main ()

{

Char ch;

printf ("1. Input record \ n");

printf ("2. Delete record \ n");

printf ("3. Display record \ n");

printf ("Please select: \ n");

Do

{

Ch=getchar ();

Switch (CH)

{

Case ' 1 ':p rintf ("input record \ n");

Break

Case ' 2 ':p rintf ("delete record \ n");

Break

Case ' 3 ':p rintf ("Display record \ n");

Break

default:printf ("Wrong choice!") \ n ");

}

}while (ch!= ' 1 ' &&ch!= ' 2 ' &&ch!= ' 3 ');

}

The difference between the while and do while

Suppose the conditional expression is not tenable at first.

The loop body of the while structure does not run again.

The loop body of the do...while structure runs over again.


JAVA for Loop

Related Article

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.