int i = 100;
while(i > 0) { // Conditional loop
--i;
if((i % 2) == 0) { // Conditional statement inside a conditional loop
System.out.println(i + " is even.");
}
}
The 'while' statement evaluates its expression at the beginning of the loop, while a 'do while' statement evaluates its expression at the end of the loop. The 'while' statement might execute no times. The 'do while' statement will execute at least one time. It depends on what you want to do, and on how you want to use the side effects, if any, of the expressions in the expression. (Before or after)
From inside any loop statement, the continue; statement will skip any remaining statements and re-evaluate the loop's conditional expression. If that expression remains true, a new iteration of the loop begins, otherwise control passes to the statement that follows the loop. Note that in a for or while loop, the conditional expression is defined before the loop body but in a do loop it is defined after the loop body.
Easy: if-else is not a loop; while, for and do-while are loops.if-else just run once, but do-while run many times.
A while loop evaluates the conditional expression at the start of each iteration, whereas a do..while loop evaluates the conditional expression at the end of each iteration. Thus the do..while loop always executes at least one iteration.
A while loop evaluates a conditional expression at the start of each iteration. If the conditional expression evaluates false, execution passes to the statement that immediately follows the body of the loop. If the conditional expression evaluates true, the body of the loop executes one iteration. When the end of the loop is reached, or a continue statement is encountered within the body of the loop, control passes back to the while statement where the conditional expression is re-evaluated. If a break statement is encountered within the body of the loop, the loop terminates and control passes to the next statement. If a return statement is encountered within the body of the loop, the loop terminates and control passes to the calling function.while (expression) {// repeats until the expression evaluates false}The braces are optional when the body of the loop is a simple statement. Compound statements must be enclosed in braces.A flag-controlled while loop has a simple conditional expression that evaluates a Boolean value:bool x;// ...while (x==true) { // flag-controlled loop// ...}}The above loop can also be written without the equality operator:while (x) { // flag-controlled loop // ...}Moreover, any integral type (such as int or char) will implicitly convert to a bool such that non-zero values evaluate true and zero evaluates false. As such, any integral type can be used in a flag-controlled loop.
A do-while loop is a statement or series of statements that are executed at least once. At the end of each iteration, a conditional expression enclosed in a while() statement is evaluated to determine if the loop should start a new iteration or not.
You use a while statement to introduce a conditional loop. Unlike a for loop which is typically used for counted loops, a while loop is used whenever a statement must iterate while an expression evaluates true. The expression is evaluated at the start of each iteration.
In the C language, the continue statement can only be used within a loop (for, while, or do). Upon execution, control transfers to the beginning of the loop.
A conditional loop will only continue to loop while the given condition is true: while( i < 10 ) { ... } An unconditional loop either has no condition (a GOTO loop), or the condition is guaranteed to always evaluate to true: while( true ) { ... }
//Both loops will print out the alphabet {A-Z} int i = 65; while(i<65+26) { System.out.println((char)i); i++; } ... for(int i=65; i<65+26; i++) System.out.println((char)i); As you can see, both loops accomplish the same thing. A while loop simply has a conditional statement that tells the loop what needs to be true for it to keep on looping. A for loop is more concise: it has 3 parts: a starting value, a conditional statement, and a(n) action for what the loop should do after every iteration (in this case increase i by 1).
break
To iterate the same command or series of commands while a conditional expression remains true. The parameters of those commands can (optionally) be altered by each iteration of the loop, as can the parameters used in the conditional expression.Conditional expressions may also be evaluated anywhere within the loop, forcing the loop to continue (start a new iteration, skipping any remaining commands in the loop), break (exit the loop completely and begin execution at the first command following the loop) or goto (jump to a specified label either inside or outside of the loop).The for() loop is the most common type of loop, usually used for counting a series of values in a sequence of increments. It accepts three parameters: the start value, a conditional expression and a function. All parameters are optional, but if a conditional expression is specified it is evaluated at the end of each loop, immediately after invoking the optional command.The while() loop must specify a conditional expression which is evaluated before each iteration of the loop.The do...while() loop must also specify a conditional expression which is evaluated at the end of each iteration. Such loops are guaranteed to loop at least once.All three are capable of creating infinite loops. Often this is desirable, but great care must be taken to ensure an infinite loop can exit gracefully at some point, otherwise your program will stall. The worst-case scenario is that the loop could bring the entire system to a halt either because the loop continually allocates memory or makes recursive calls to the function containing the loop. Infinite loop must therefore contain a conditional expression to exit the loop from within the body of the loop itself.