1. The first program we are going to write is one of the most basic programs you can write in any language, Hello World!
  2. So, Type the following code in to the repl.it editor:

    #include <iostream>
    int main() {
      std::cout << "Hello World!" << std::endl;
    }
    

    And run it!

  3. Hopefully repl.it just printed out Hello World!, But what did that code actually do?

    1. On the first line, We typed #include <iostream> , sometimes in a programming language we need to do something extra, Which can be as complex as hosting a website or as simple as printing writing and instead of wrighting it ourselvs we can use some pre-written code, Called a library. So what we did here was import iostream, A library needed to read input and print output.
    2. On the next line we define a new function called main. For now, All you need to know is that code you put in here is run when the program starts.
    3. This is the actual line of code that does stuff, cout is what you use to print things, And to send things into it you use "<<". And then we send in a "endl" which prints an invisible new line character. We then close the line with a ";" to tell C++ that that is the end of this line of code.
    4. Finally we close off the "main" function with another bracket!
    5. But wait! Why did we write std:: before cout and endl??? std stand for "Standard" which is where cout and endl live, But it would be really annoying if we had to write this everywhere. So what we do is add this line to the top of the file, using namespace std; which tells c++ to just assume that we mean std:: unless we say otherwise! The final program will look like this:
    #include <iostream>
    using namespace std;
    int main() {
      cout << "Hello World!" << endl;
    }
    

results matching ""

    No results matching ""