Learn to code in C++ (lesson 1) - Hello, World!

in steemiteducation •  7 months ago
image

I hope you have followed my previous post on getting an IDE installed? If so, let's start coding 😁

We are going to start with a very basic program - one that simply displays "Hello, World!" on your display. Type the following program into your IDE:

#include <iostream>
using namespace std;
int main()
{ cout << "Hello, World!"; return 0; }

Now compile and run your program. Your program should have displayed "Hello, World!" on your screen like this:

Hello, World!

You might have extra text before and/or after the output - this is OK - it is all dependant on the IDE that you are using.

Our program worked, but our programming style was not very good. Our programs need to be written "neatly" so that they are easier to read. It is good programming style to leave open lines, use new/separate lines for new statements and to indent our code between curly brackets (to name a few). As we progress with these lessons we'll be keeping an eye out on our programming style 😉

Retype the program above so that it looks like this:

#include <iostream>
using namespace std;

int main() { cout << "Hello, World!"; return 0; }

So why do we type #include at the beginning of the program? This allows us to use a header file called iostream. Header files allow us to use additional instructions in our programs. The io in iostream stands for input/output and allows us (in this case) to output text onto our display using the cout instruction.

And what about using namespace std? Well, all the names defined in iostream belong to a "namespace", specifically std. Hence we have to include this too.

int main() and return 0 go hand in hand. int main() declares a header for the main function (all programs need a main function). Here we are specifically returning an integer (hence int) and will be returning a value of 0. Don't worry too much about this - this will be explained later on when we deal with functions.

All statements have to end with a semicolon. Here we used two statements - one to display the text on the display and one to return the value of 0. Always remember to end all statements with a semicolon otherwise your program will not run!

Lastly, did you wonder about the cout << part? Well, cout stands for "console out" and tells our program to display whatever we place after the << operator.

So, how was your first attempt at coding? Not too difficult, right? I know our program is quite simple still at this stage, but not to worry, your skills will progress as we continue.

Interested in additional content, including activities and solutions?
See my blog here.

cheeto.blue
Team South Africa banner designed by @bearone


Please upvote if you liked this post, follow if you want to see more, and let me know your thoughts in the comments.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

This should be so beneficial to people wishing to learn to code, will share this around @cheeto.blue to let people know.

·

Thanks a mil @joanstewart - I certainly hope some ppl can learn from it 😁

Really Thanks For Share it's help for me.

·

Glad you enjoyed it 😁 There are more lessons to come 😉

You just received a Tier 0 upvote! Looking for bigger rewards? Click here and learn how to get them or visit us on Discord