When we write
- While(n)
{
// some statements
}
How to its stop on 0 .
And
- While(n>0)
{
// some statements
}
Is both meaning is same
When we write
How to its stop on 0 .
And
Is both meaning is same
n==0
. Your code uses while(n)
which shall always be true
no matter what. You are supposed to use a condition to terminate the loop.while(n) //this is not a correct 'condition'
{
// some statements
if (n==0)
break;
//or any counter
}
n==0
. This is because according to your condition while(n>0)
, the loop will iterate as many times as it finds n
to be greater than 0. When the condition returns false
, that is n is not greater than 0
, iteration stops.So, both are definitely not the same.
In C/C++ “1”(or any other integer apart from 0) denotes for true & “0” denotes for false.
while(true) is same as while(1).
so according to your query:
while(n){ //code } , the loop run only when n!=0 ( -∞<n<0 || 0<n< ∞) and
while(n>0){ //code }, the loop run only when n>0 ( 0<n< ∞).
VERDICT: These are not the same at all.
Hope it helps…