My issue
why we write t-- inside the while loop
My code
// Solution as follows
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
//accept the count of test cases given in the the 1st line
cin>>t;
//Run a loop to accept 't' inputs
while(t--)
{
int N;
//accept an integer N in each test case
cin>>N;
//output the number mirror for each test case
cout<<N<<endl;
}
return 0;
}
Learning course: C++ for problem solving - 1
Problem Link: CodeChef: Practical coding for everyone
TLDR: This is the shortest way to loop through all the test cases.
Basically we want to loop from 1 till t
so that we can handle each test. To do that we can either write a for loop like this
for (int i = 1; i <= t; i++) {
}
But for
loop needs this much code, so we use while loop. With while
loop you can write a condition like
while (t>0) {
// Do stuff
t = t-1;
}
We can then compress it more by just using while(t--)
which will stop when t
becomes zero.
This is best way to decrement the loop index and will stop when t becomes zero using while(t–).
otherwise we will do the decrement inside the loop
while (t>0) {
// statements
t=t–;
}