0

Can please someone explain me what is going on with this code. I mean I don't get it. Here is the source code.

#include <iostream>
using namespace std;

int main(){
    int a = 0;
    int c = 0;
    for (int b = 1; b <= 144 ; b++){
        a = b;
        b = c;
        c = a + b;
        
        cout << a << "\n";
    }
}

Output :

1
1
2
3
5
8
13
21
34
55
89
144

Thanks in advance!

newcode
  • 3
  • 2
  • 2
    Suggestion: State what you think it does and your reasoning and then ask if you are right. Generally gets a better response. One tool you can use to help you understand what code is doing is a debugger. With a debugger you can run the program at your speed and watch what happens as it happens. One of the best programmer productivity tools you'll ever encounter, so the sooner you get used to using one, the sooner you can reap the rewards. – user4581301 Sep 29 '21 at 04:19
  • 1
    It outputs some [Fibonacci numbers](https://en.wikipedia.org/wiki/Fibonacci_number). – Blastfurnace Sep 29 '21 at 04:24
  • Thanks mate. To be honest i am new in this field and i want to understand and learn every single detail what the code does. I want to be great programmer someday. Copied that! – newcode Sep 29 '21 at 04:26
  • 1
    The value of `c` is accessed before it is initialised. So the behaviour of this code is undefined. The output of the code is consistent with `c` being initialised to zero before its value is accessed for the first time, but that is *not* guaranteed. There are implementations (compilers, OS, etc) for which the value `c` will be initialised to a random value. – Peter Sep 29 '21 at 05:07
  • so in order to view a better result. I'll assign the values of variable a and c to = 0; ? – newcode Sep 29 '21 at 05:14
  • In order to have a *defined* behaviour of the code, it is necessary that `c` be initialised or assigned to a value (zero for the output you describe, which happened for you by luck) before the first time its value is accessed. It is not necessary to initialise `a`, since the first operation on `a` (during the first iteration of the loop) assigns it a value (i.e. its value is not accessed before it is initialised). – Peter Sep 29 '21 at 05:21
  • Yes, at least for `c`. `a` gets a value before it is used. Notes: `int a,c = 0;` will only initialize `c`. If you want to initialize both you need to do each one separately: `int a = 0,c = 0;`. I use initialize rather than assign because if you define the variable and give it a value at the same time you get initialization, not assignment. The difference isn't that important with something simple like an `int`, but becomes very important with an object. When you define an object, a constructor runs. If you define an then assign sometimes you repeat a bunch of work. – user4581301 Sep 29 '21 at 05:28

1 Answers1

2

Well, I'll go by explaining it line by line.

Before main()

First, the include <iostream> imports the cout function to output to the terminal. The using namespace std; is like a shortener and imports the standard library. Instead of std::cout, now you are writing cout.

During main()

The int main() calls the main function of the file, which runs the program. The int keyword references that it should return an integer value. int a,c; references that you are declaring 2 variables, both integers, named a, and c.

The for loop has multiple parameters:

  • int b = 1; creates a variable that the for loop uses in its next arguments.
  • b <= 144; specifies that as b is being modified if it is less than or equal to 144 then the loop will continue to execute.
  • b++ means that for every iteration of the loop you will increase the value of b by one.

Here is an example of the first for loop iteration, with the b value filled in:

a = 1; // a is now 1
b = c; // c hasn't exactly been defined, but it is an integer
c = a + 1; // or 1 + 0. c is now 2

In the next iteration, I will fill some more values in:

a = 1; // b is 1 and a is set to b
b = 1; // b is set to c
c = 1 + 1;

Now, why is b the same as the last iteration? Because c was technically 0, b was set to 0, and after the loop, it was increased by 1, so then it was back to 1. The only difference was that c was 1, which set the second iteration value of c to 2.

In the comments above, someone referenced this is the Fibonacci sequence which if you aren't aware about, it is an interesting topic to google.

simpl360
  • 34
  • 2
  • so basically "=" sign is not about equals to like that? sorry for my questions. i am new to this – newcode Sep 29 '21 at 04:42
  • Which equals sign? If you mean the <=, that is less than or equal to. In setting variables, the = is just changing the variable contents. – simpl360 Sep 29 '21 at 04:47
  • in part of a = b; b = c; c = a + b; this part giving me confusion – newcode Sep 29 '21 at 04:48
  • Equals in c++ is `==`. A single `=` is an initialization or an assignment, depending on the context. Stack overflow isn't a very good place to learn the basics of a programming language. It's better suited to filling in the blanks or when you stumble across something really weird. I recommend spending some time with a [good introductory book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and then reading Meyers' Effective Modern C++. After that, go where your exploration takes you. – user4581301 Sep 29 '21 at 04:50
  • You need the book because C++ can get really nuts. Someone could add code that [overloads the `==` operator](https://en.cppreference.com/w/cpp/language/operators) to make the computer play the Theme from Hunt for Red October instead of comparing two objects. I [strongly recommend against doing that](https://en.wikipedia.org/wiki/Principle_of_least_astonishment), but the language allows it. – user4581301 Sep 29 '21 at 04:54
  • Can you suggest me a specific introductory book that good for beginners like me – newcode Sep 29 '21 at 04:58
  • I sidestepped that one because I'm not a big fan of the two most recommended books ([See this link](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)) and what I learned on is hopelessly out of date now. Of the two, I'd go with *Programming: Principles and Practice Using C++* because it is more up-to-date. The Sixth edition of C++ Primer might be better, but I haven't had cause to read it yet. – user4581301 Sep 29 '21 at 05:13