Basic Syntax
Note: This is the base of C++ code. We have to write this code every time we are writing C++ program.
Now we will understand the example from the previous chapter.
Description of Code
- Line 1: iostream is a library in C++ that provides input and output functionality to interact with the console or other input/output devices.
- Line 2: using namespace std; means that we can use names for objects and variables from the standard library.
- Line 3: int main() this is the starting point for the execution of your program. Any code inside its curly brackets will be executed.
- Line 4: cout < < "Hello World!" - it is used to print Hello World!
- Line 5: return 0; execution of main function stops here. This is the last line of the main function which is executed. Everything written after it is not executed.
- You need to put a semicolon (;) after every line of code or more specifically after every instruction.
- C++ compiler ignores white spaces. You can write all code in one line but multiple lines make the code more readable.
- C++ is case sensitive language. E.g. John is not same as john.
- If you have any confusion, you can read this tutorial again and do not worry about line1 and line2. If you do not understand these lines, just ignore and move ahead. You will understand them later on.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
Sign in to track your progress and earn XP!
Exercise 1 of 2Easy
Which of the following is a best practice in programming?
10 XP~2 min
Exercise 2 of 2Easy
Code that is easy to read and understand is called ___ code.
10 XP~2 min