Computer Science, asked by pavneet96, 11 months ago

write a short note on while,do-while and for- loop?​

Answers

Answered by SahilNarang
1

while loop:-

It is entery control loop it check the condition first and then it increment a value in a block.

do-while loop:-

it is the exit control loop it run atlees one

time it check the condition at the end of a block.

for loop:-

it is the most frequent used loop in loops it is entry control loop it check the condition and incriment value at first.

Answered by Hacket
2
While loop: A while is a conditional -- just like an if -- except that a while keeps executing its internal code as long as the condition remains true.

Given a variable of x with a value of 1, consider the following conditional:

if ( x < 5 ) { x = x + 1; }

Since x is less than 5, the if condition is true and so the gate is passed and the expression within the conditional will execute, therefore adding 1 to the variable value of x to make it equal to 2.

However, with one small difference, something else happens:

while ( x < 5 ) { x = x + 1; }

Since x is less than 5, the while condition is true and so the gate is passed and the expression within the conditional will execute... and then execute again... and again and again, until such a time that x becomes equal to 5 (which is no longer less than 5).

A while loop like this is a convenient way to execute some code for a certain number of times, for instance to create the 64 squares of a chess board.

However, it is very important for the while condition to contain something that is being affected by the expressions within. Otherwise, the while could continue on forever. This is a common mistake, and usually the reason why a for condition is used instead of a while.

A for loop is essentially the same as a while loop except that it includes all the ingredients that should make is safe from running on forever: the conditional variable, the while condition, and then the increment of the conditional variable (in the following example, x++ is the same as x = x + 1):

for ( x = 1; x < 5; x++ ) { /* do something 4 times */ }

In programming, you will find the for loop almost everywhere. It is very common, and very safe.


do-while: The do-while statement is a type of loop that iterates a block of statements while a condition is true.

do {
statements
} while (expression)
It evaluates an expression at the bottom of the loop, which returns a boolean value. If the expression evaluates to true, it will reiterate the statements within the do-while block.

Example:

int n = 1;
do {
System.out.println("Loop No. " + n);
n++;
} while (n <= 3);

For loop: For loops are for when you want absolute control over every aspect of the loop. If your looped work needs to know what step it’s on, where it’s going, and how fast it’s getting there, then for loops are what you need. Use a for loop when you know exactly how many steps you want your loop to take.

What you get: An iterator, an increment, and a condition. The latter is just like the condition in the while and do-while loops. The iterator is an integer variable that represents your iterating position, and the increment is how much that iterator is changed after every iteration. Traditionally, a for loop starts at 0 and increments by 1. So, for example, let’s say we have an array called myThings, and we wanted to iterate through the whole array using a for loops, it would look something like this:

for (i = 0; i < myThings.length; i++) {
// Get a thing out of the array using the iterator and then do something with it.
}
In this example, i = 0 is the initialization of the iterator (i starts at 0), i++ is the incrementing of the iterator (i is incremented by 1 before each subsequent iteration), and i < myThings.length is the condition (after being incremented, if i greater than or equal to the length of the myThings array, then exit the loop).

The gotchas: If you don’t configure this correctly, you could end up trying to index beyond the end of an array and crashing your application. Also, it’s still possible to get into an infinite loop, even in a for loop.


Thanks for asking
Similar questions