answersLogoWhite

0

Eg for forloop and while loop?

Updated: 11/4/2022
User Avatar

Wiki User

14y ago

Best Answer

//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).

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Eg for forloop and while loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is difference between for loop and do-while loop?

The do loop is similar to the forloop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.


What does a while loop do in javascript?

a while-loop in javascript executes a section of code while a condition is true. Eg. function () { var i = 0; while (i &lt; 326) { alert(i++) } } In the above example, the an alert is given, showing the value of i so for as long as i is less than 326. In each loop, i is increased by 1.


How can you write a c program using while loop that will print first ten odd numbers?

#include&lt;stdio.h&gt; int main () { int odd=1; int count=0; while (count++&lt;10) { printf (%d\n", odd); odd+=2; } return 0; }


Compare Do-While with While Statements?

A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.


What is a loop type?

A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop

Related questions

Is it possible to use a for loop in place of a while loop?

Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i&lt;=n;)


What is difference between for loop and do-while loop?

The do loop is similar to the forloop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.


What does a while loop do in javascript?

a while-loop in javascript executes a section of code while a condition is true. Eg. function () { var i = 0; while (i &lt; 326) { alert(i++) } } In the above example, the an alert is given, showing the value of i so for as long as i is less than 326. In each loop, i is increased by 1.


How can you write a c program using while loop that will print first ten odd numbers?

#include&lt;stdio.h&gt; int main () { int odd=1; int count=0; while (count++&lt;10) { printf (%d\n", odd); odd+=2; } return 0; }


What is a nested loop in java?

A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.


What are the similarities of while loop and a do while loop?

They both loop


Compare Do-While with While Statements?

A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.


The while loop is a type of loop?

Is loop


What is forloop?

For loop is utilize to do any specific work for specific number of times For example to print hello on computer screen 10 time we use For (start = 1; end = 10; start++) count&lt;&lt;"hello"; next


What is a loop type?

A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


Definition of do loop while in visual basic?

There are 3 type of loop 1 is for loop 2 is loop while 3 is loop untile