0
 #include<bits/stdc++.h>
 using namespace std;
 int main()
     {
      int prev=-1;
      int next=1;
      int n;
      cin>>n;
      for(int i=1;i<=n;i++)
       {
          cout<<prev+next<<endl;
          next=prev+next-next+(prev=next);//assigning prev and next
       }
      }

I can not understand assigning prev and next.In second line between for loop how operator precedence works?

  • 8
    Simple answer: Don't even try. Code should be clear and this is the opposite of clear. Throw it away and find better code to look at. – NathanOliver May 09 '16 at 18:17
  • Behaviour of your program is not strictly defined. `prev` and `prev=next` can be evaluated in any order. – Revolver_Ocelot May 09 '16 at 18:18
  • This: `#include` combined with `using namespace std;` can shoot you in the head countless different ways. The first says, "Include the entire C++ standard library in my file." The second says, "Put the standard library into the global namespace." Now you have the entire standard library in the global namespace. That's a lot of function, class, and variable names you now have to watch out for. You could find yourself sharing a name like `reverse`, `swap`, `find`, `max`, `min`, `copy` to comedic effect. – user4581301 May 09 '16 at 18:40

1 Answers1

2

First of all, never #include<bits/stdc++.h>. Include headers you need as per specification.

Second, your example has undefined behavior in it - you are modifying and reading the same value before reaching sequence point.

SergeyA
  • 61,605
  • 5
  • 78
  • 137