Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 1, Topic 5
In Progress

C++: Understanding Codes

Yasin Cakal 27 Oct 2021
Lesson Progress
0% Complete

C++ programs can be written in a specific structure only to execute it and receive the required output. It uses a few key elements that must be understood to get a better command on the language. Look at the code below to understand better.

//We will use this code to print 'Hello, World!' on the screen.

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

Output:

Hello, World!

Now, let’s start understanding the code line by line. The output Hello, World! is printed onto your terminal. C++ is a case sensitive language. This means that capital and small letters may mean different things.

For example, we use the cout command to print text onto the terminal. Typing COUT or Cout will render the code wrong because the compiler only understands the cout command.

Now, let’s study this line from the code:

//We will use this code to print 'Hello, World!' on the screen.

This line uses // at the beginning. This single line entered at the top of the code tells us what the code is for. This line is sometimes added to the codes to display the name or the function of the code. The compiler automatically ignores any text added after //, and it moves to read the following line. This is also used to add comments so that the other readers of the code can easily understand what the programmer did in the code.

Moving onto the following line of the code:

#include <iostream>

This part of the code is known as the pre-processor directive. The compiler is instructed to include a library like iostream. This library allows the compiler to input or output data through the cin and cout commands, respectively. cin enables the compiler to take an input from the keyboard and cout is used to print output on the screen.

Every C++ code has to have a main() function like the line below:

int main()
{
    //input code
}

The function is where we add our instructions for the compiler in a sequence. We enter our code here.

C++ codes are easier to read because they use white spaces generously. Any spaces added are ignored by the compiler except if added in the cout command. C++ codes use indentation. However, their absence or presence does not change the understanding of the code by the compiler. They are added to make the code easier to understand and separate different commands and lines in the multiple lines code. Use of these white spaces and indentation when writing a C++ program is highly recommended.

%d bloggers like this: