Click here to Skip to main content
15,915,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
while(i = 0 )
{
------ (expression )
______(EXPRESSION)
.
.
}

What I have tried:

READING MORE INFORMATION ABOUT LOOPS
Posted
Updated 27-Dec-17 21:09pm

For starters, that won't work:
C++
while(i = 0 )
Is not a test, it's an assignment - so it always returns zero, which in C++ terms is always false. So the loop would never enter, as while loops continue as long as the guard expression is true (i.e. non-zero).
Try this:
C++
while(i == 0)
And it'll work better.
But yes, you should explicitly set the value of i before you try to enter the loop:
C++
i = 0;
while(i == 0)
   {
   ...
   }
to be sure of what will happen.
 
Share this answer
 
Quote:
Is it necessary to initialize a variable under the while loop ?

Variable initialization is mandatory, everywhere, for every variables.
Variable initialization means that you must store a value in the variable before any attempt to use its contain.

C++
while(i = 0 )

Here the initialization of i is misplaced because this place is for loop condition and the 0 will prevent from entering the loop.

C++
i = 0;
while(i == 0)
   {
   ...
   }

This is probably what you want to do as suggested by OG.

C++
a = 10;
while(i = a)
   {
   ...
   a--;
   }

but this is working because it means.
C++
while((i = a) != 0)
 
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