While Loop

When we write

  1. While(n)
    // some statements

How to its stop on 0 .


  1. While(n>0)
    // some statements

Is both meaning is same

  1. You will have to use a condition inside the loop body that checks if 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.
Like this:
while(n) //this is not a correct 'condition'
// some statements
    if (n==0)
//or any counter
  1. This uses a correct condition and the loop will terminate when 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…