Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
int k=2;
do
{
l=100;
printf("%d%d",k,l);
k--;
l--;
}
while(k);

What I have tried:

pliz tell me what while(k) does or mean??
Posted
Updated 3-Oct-22 1:10am
Comments
Member 15627495 2-Oct-22 2:45am    
hello !

It's not "while(k) alone .

It's do{...}while(k)

by while(1) the loop continue for one lap.
and the loop ends when k=0 ( = false )

the values for k goes : 2 -> 1 -> 0
at k=0 the loop ends.

while(1) or while(true) is an infinity loop;
while(false) is like 'break' or exit from the loop
Rick York 2-Oct-22 13:48pm    
The word is PLEASE.

The while is attached to the do at the top of the block, and it's one of the three types of loops that C supports: for, while, and do ... while
The do ... while statement is very similar to the while loop, except that the loop body is always executed once, because the loop / don't loop condition is only checked after the body, not before.

The condition here is just k and because C doesn't have boolean operators, a value of zero is always taken as "false" and any non-zero value is always taken as "true".
so your code:
do
    {
    ...
    } while (k);
is the same as:
do
    {
    ...
    } while (k != 0);
 
Share this answer
 
v2
Comments
Richard MacCutchan 2-Oct-22 3:29am    
Er, foreach in C?
OriginalGriff 2-Oct-22 3:44am    
:doh:
Fixed!

I blame a lack of sleep ...
CPallini 3-Oct-22 7:07am    
5.
Did you Google for it, yet ?
See, for instance: C While Loop[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900