Computer Science, asked by taniya55555, 11 months ago

What are the comparison between while loop, for loop and do while loop??

Don't copy from internet..

No Spamming!! ❌❌

Answers

Answered by rakeshmohata
8
ʜᴏᴩᴇ ᴜ ʟɪᴋᴇ ᴍy ᴀɴꜱᴡᴇʀ
-------------------------------------
 \\  \\    =  >  \: \huge{ \boxed{ \underline{ \bf \: while \:  \:  loop}}}


The while loop calculates the expression before every loop. If the expression is true then block of statements is executed, so it will not execute If the condition is initially false. It needs the parenthesis like the if statement.

while ( expression )
/* while expression is true do following*/
  statements... ;

=================================
 =  >  \huge{ \boxed{ \bf \underline{Do \:  \:  While  \:  \: loop}}}

This is equivalent to a while loop, but it have test condition at the end of the loop. The Do while loop will always execute at least once.

do
  statements ;
while ( expression );
/* while expression is true do...*/
==================================
 =  >  \huge{ \boxed{ \bf \underline{For \:  \:  loop}}}

This is very widely held loop.
For loops work like the corresponding while loop shown in the above example. The first expression is treated as a statement and executed, then the second expression is test or condition which is evaluated to see if the body of the loop should be executed. The third expression is increment or decrement which is performed at the end of every iteration/repetition of the loop.

for (expr1; expr2; expr3)
  statements...;
------------------------------------------------------

  In while loop it can happen that the statement will never execute But In the do-while loop, test condition is based at the end of loop therefore the block of statement will always execute at least once. This is the main difference between the while and the do-while loop.

  For example, to execute a statement 5 times:

for (i = 0; i < 5; i++)
  printf(''%d\\n'',i);

Another way of doing this is:
i = 5;
while (i--)
  statements;

While using this method, make sure that value of i is greater than zero, or make the test i-->0.
___________________________
ʜᴏᴩᴇ ᴛʜɪꜱ ɪꜱ ᴜʀ ʀᴇqᴜɪʀᴇᴅ ᴀɴꜱᴡᴇʀ

ᴩʀᴏᴜᴅ ᴛᴏ ʜᴇʟᴩ ᴜ

rakeshmohata: thanks vai
rakeshmohata: u too
Answered by aryankunalroy38
0

while loop :-while loop is a conditional loop that will repeat the instructions within itself as long as a conditional remains true

for loop:-the for loop of python is designed to process the items of any sequence,such as a list or a string ,one by one

do while loop:- a do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.

Similar questions