6

I just started with competitive programming and have been using a loop like the one below to define the number of test cases in most of the practice problems.

for(int i=1;i<=t;i++)
{
...
}

However, I have seen people using a while loop which just has the condition (t--) which runs perfectly okay too. Can someone explain to me how this condition actually works? Is it the same as while(t!=0), but instead of a decrement in the value of later in the loop, we are doing it here?

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
Anuj Saharan
  • 137
  • 1
  • 1
  • 6
  • 3
    Which part don't you understand? – juanchopanza Sep 18 '15 at 09:56
  • 1
    It's worthwile to pay close attention to the semantics of the postfix operator and its interaction with the loop: What's the end value of i? What's the value range of i visible inside the loop? – Peter - Reinstate Monica Sep 18 '15 at 09:56
  • not exactly the same but almost: [What is the “-->” operator in C++?](https://stackoverflow.com/q/1642028/995714) – phuclv Nov 21 '20 at 08:59
  • Does this answer your question? [What is the "-->" operator in C++?](https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c) – phuclv Nov 21 '20 at 09:00

10 Answers10

14

Yes.

int t = 5;
while(t--){...}

runs the loop until t is 0 during checking it's value, since 0 is equally to false in a logical context.

Because t-- is the post decremental operator on t, the value of it will be modified after the while checks it, so it will be -1 in the end. Therefore, the values of t inside the loop will be: 4,3,2,1,0.

But it creates an additional variable outside the loop scope - so I do prefer the for loop.

Gombat
  • 1,994
  • 16
  • 21
9

The body of the while loop is executed repeatedly until the value of its condition becomes false (note that if the condition is initially false, the loop will not execute at all). The condition test takes place before each execution of the loop's body. Now, let's say you have this:

int x = 5;
while(x--)
{
    cout << x;
}

Each iteration the condition of the while loop will be evaluated and checked for false. Since we use postfix decrement, x will first be used (i.e. checked for false to break the loop), and then decreased, which means the loop above will print 43210, and then finish.

It's worth noting that after the loop finishes, the value of x will be -1.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • 1
    _"A while loop exits when its condition becomes false"_ That's misleading. I've seen some newbies think that the loop will terminate as soon as the condition becomes false, no matter where in the iteration code is currently being executed. I think this statement falsely reaffirms that myth. It would be better to say that a while loop exits at the start of the first iteration for which the condition becomes false, or something like that. – Lightness Races in Orbit Sep 18 '15 at 10:36
  • @lightness, Interesting. I've never seen people misunderstanding it in such a way, but I've changed the answer with a slightly modified quote from the standard. Hope it is more clear now. – SingerOfTheFall Sep 18 '15 at 10:43
  • @TonyD, added a note for that. – SingerOfTheFall Sep 18 '15 at 11:40
2

Here the value inside while loop argument can also Execute boolean function. That means if you are providing any False value inside while loop argument

while(Any False value/Negative Value){

The While loop will break and It won't Execute further. In the Above Example when while(t--) start executing it use post decrement that means if the value of t=5 it will execute the loop first and then decrease it and it'll execute until the value of t becomes negative that is -1 and the condition becomes FALSE and Loop will terminate.

0

If there is no reason to do otherwise, I would always write a loop like this:

const int tmax = 3;
for (int t=0;t<tmax;t++){std::cout << t << std::endl;}

If you only care about how many times the loop is executed but you do not need the value of t inside the loop, there is actually no reason to use something else. However, if you want to count down, you could do this:

for (int t=tmax;t!=0;t--){std::cout << t << std::endl;}

or

for (int t=0;t<tmax;t++){std::cout << tmax - t << std::endl;}

which one may consider both as ugly (I personally do not like the first too much, because I am a devotee of counting up in for loops whenever possible. The second one isnt nice because you might use the value tmax-t several times inside the loop, i.e. either you introduce an extra varible or the opportunity for bugs) and prefer to write

int t = tmax;
while (t--){std::cout << t << std::endl;}

which has the little disadantage of introducing an extra variable outside of the loop. However, when tmax isnt const and the value isnt needed after the loop, probably the most compact way to write the loop is this:

while (tmax--){std::cout << tmax << std::endl;}

I am pretty sure they are all the same in performance and it is just about your prefered coding style, but I hope I could give you a hint, why someone would prefer one over the other.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

while(x--) stops after the result of x-- has been evaluated to false / 0

int x = 5;
while (x--) { 
    ;// do something 
}

Because of the post decrement, x has value -1 after the loop has finished.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0
while(x--) 

This loop will run 'x' times, and since x-- is post-decrement operator, it will first check value of x to be non-zero and decrement it afterwards.

So if you try printing value of x inside the loop, they will be, x-1 x-2 . . 0

pulkit
  • 1
  • 1
0

There are 2 ways to traverse from 0 to 9.

first - 0 1 2 3 4 5 6 7 8 9 to traverse in first way you need to

for(x=0; x !=9; x++){
    cout << x;
}

second - 9 8 7 6 5 4 3 2 1 0 to traverse in second way you need this loop

for(x=9; x !=0; x--){
    cout << x;
}
  • x++ is post increment. It will increase value of x by 1 after using current value. It is same as x = x + 1
  • x-- is post decrement. It will decrease value of x by 1 after using current value It is same as x = x - 1
dj1986
  • 362
  • 2
  • 9
0

Both are actually the same.

The while(t--) also runs the test cases 't' times, as the value of t is decreased in every iteration, same as with for loop. This is just another way of writing loop, and it even saves time during solving questions.

0
 #A loop has three statement.
    1. initialization
    2. condition 
    3. increment or decrement or more formally we can say change of state 

Now, look at this :

    int t = 5; -> this is the initialization part
    while(t--) -> condition and decrement both parts are used here
    
    This loop starts with t = 5 and
    it will end when t = 0,
    because 0 is considered as a false in a logical context.

Now see the whole scenario :

   Starting the first iteration, t-- = 5  (as t-- is the post decremental 
                      operator on t so first it will be used then decrease)
   Starting the second iteration, t-- = 4 
   Starting the third iteration, t-- = 3
   Starting the fourth iteration, t-- = 2
   Starting the fifth iteration, t-- = 1
   Starting the sixth iteration, t-- = 0 [As t == 0, so it will not execute]
0

Yeah, it simply means that the while loop is going to iterate t times as it happens by t subtracting itself from one every time.

zhbhuiyan
  • 11
  • 2