3

This is a model solution code of the following problem;

"The input consists of T test cases. The first line of the input is given a T. Each test case consists of three rows of integers separated by a single space, each consisting of three random points of x and y coordinates. The coordinates of the top left pixel in the browser viewport are (1, 1) and the coordinates of the bottom right pixel are (1000, 1000). All coordinates are located within the viewport and the positions of each point are different."

And here is the sample of the input.

2
5 5
5 7
7 5
30 20
10 10
10 20
7 7
30 10

The last two lines are the answers of the problem.

And here is my three questions. 1) What happened when we use cin statement in initialization?? It receives how many actions it will perform from the user in the initialization of for loop. I understand that this cin statement works properly. I cannot understand how this code knows how many times this for loop has to be repeated. This is because there is no action on T after initialization with a value of T from the user. There is no actrion in 'increment/decrement' also.

2) After googling, I understand when there is cin in condition, the loop ends when there is no more inputs or the inputs' type does not match the variables' type. But in this code, the for loop ends when the repeated time (T) is over. How could this happen???

3) Finally, the outcomes should be presented after all inputs are finished not by one-by-one. Then how could this for loop memorize the outcome of each set(3 inputs)??

I'm not English speaker T.T Thank you for reading my question.

#include<iostream>

int main()
{
int T,a,b,c,A,B,C;
for(std::cin>>T; std::cin>>a>>A>>b>>B>>c>>C; printf("%d %d\n",a^b^c,A^B^C));

}
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
김동현
  • 31
  • 3
  • 2
    Please don't spam tags. This is plain `c++`. there is no `c`, so please remove the `c`-tag. – skratchi.at Oct 25 '19 at 06:40
  • As any [decent text-book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) should have taught you, the [`for` statement](https://en.cppreference.com/w/cpp/language/for) have really three arbitrary *expressions* in it, the first which could also be a definition statement. And all streams have [operator that allows them to be used in condition](https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool). – Some programmer dude Oct 25 '19 at 06:45
  • 1
    and it's really bad idea to ask several question in one.Not very helpful to other and for answers, because all of those three were answered before – Swift - Friday Pie Oct 25 '19 at 06:46
  • And regarding point 3, the behavior is explained by knowing what `for` loops does and how they work. If you want to save the results for later, then you need to do it explicitly in the loop body. – Some programmer dude Oct 25 '19 at 06:46

2 Answers2

1
  1. What happened when we use cin statement in initialization??

    That part of for() loop can contain any simple statement, not just initialization statement. This statement is done only once. For loop

    for ( init condition ; iteration ) 
       statement
    

    is actually equivalent of this code:

    {
       init
       while ( condition ) 
       {
          statement
          iteration;
       }
    }
    
  2. But in this code, the for loop ends when the repeated time (T) is over. How could this happen???

    The operator >> overloaded for streams return stream it acted on. Class ios_base which is common parent of all streams, contains this operator

    std::ios_base::operator bool()

    This operator is an equivalent of good() method. When >> fails to read and parse values from input stream, good() returns false, loop breaks. T is not used in the provided code.

  3. Then how could this for loop memorize the outcome of each set(3 inputs)??

    It doesn't. It prints result after reading each set.

PS. People who read\proof-check code after programmer, would tend to have murderous intent toward those who write for() loops like that.

JeJo
  • 30,635
  • 6
  • 49
  • 88
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
0
  1. If we use a cin statement as our initialization, we execute this once. It will receive the input and place the value in the variable T. You are completely right; there is no action on T after initialization such as incrementing or decrementing its value.

  2. This is not true. The code does not end when the it repeated T times. As long as (valid) input is given, this for-loop will continue. This is because the condition part of your for-loop consist of a cin statement. That is, as long as your cin statement succeeds, the for-loop will continue.

  3. It cannot. Each time the loop runs, you overwrite the variables a, A, b, B, c and C. Hence, the old values are lost.

Daniel
  • 81
  • 3