Having a doubt

// bits/stdc++.h works in linux.
// It loads most of the libraries of C++ required.
#include <bits/stdc++.h>

using namespace std;

int main() {
// Read the number of test cases.
int T;
scanf("%d", &T);
while (T- -) {
// Read the input a, b
int a, b;
scanf("%d %d", &a, &b);

	// Compute the ans.
	int ans = a + b;
	printf("%d\n", ans);
}

return 0;

}

what is the meaning of the expression inside while loop??(T–)
I am new to this type of code,please help me with it…
I am confused that how is value inside parenthesis true having just T-- and no condition?

These are just basic syntax of C++.

int a,b; is used to declare the variables a and b with their data types (int).
scanf is used to take values from the input stream and store into those variables.
Then you are adding them and storing them in the new variable named ‘ans’. And then you print the result.

I suggest you to go through some programming basics on youtube or other websites. These are basic syntax you need to know before you can start programming in a language.

2 Likes

While is a loop in C language.Now the loops and conditional statements(ex. if())
works only when the condition is met. Now If the condition is right it is interpreted as true(i.e 1)
So The loops works.
In your code while(t- -) states that whatever value of test cases you store in t
is going to decrement by 1 each time the loop runs
For Ex.-> Lets take t=3;
so while(3)// first time
then while(2)//After First Iteration
then while(1)// After Second Iteration
and finally while(0) //Here the condition fails!!
So it runs 3 times.

1 Like

You should understand that while( ) or if( ) expects any NON-ZERO value to allow looping. For Eg:

while(-100)
{
      printf("Infinite Loop\n");
}

This is an infinite loop as the condition inside the while loop is non zero.

int t;
scanf("%d",&t);
while(t--)
{
      printf("%d\n",t);
}

When we give t=2. The output would be

1
0

And then the loop stops as now t is 0 in the next iteration.

1 Like

Thanks!
I understood it now…loved your explanation.