Topic Overview and Lesson Goal

Hello! Are you ready to step into the world of C++ programming? Today, we will explore basic syntax and examine an array of data types in C++. Syntax dictates the structure of programs, similar to grammar in English. Data types, on the other hand, characterize the kind of data we manipulate. Let's jump in!

Understanding C++ Syntax

Syntax in programming is akin to traffic rules; it governs how a program is written. Let's analyze a basic C++ code segment:

// This line includes an essential library for input/output operations.
#include <iostream> 

// This is the main function where our program starts.
int main() {
    // This line prints "Hello, CodeSignal!" to the console.
    std::cout << "Hello, CodeSignal!";
    // We end the function by returning zero, signaling to the program that everything went fine.
    return 0;
}
  1. Semicolons: Statements end with a semicolon (;), similar to a period in English.
  2. Code Blocks: Code enclosed in braces {} forms a block treated as one unit.
  3. Indentation: This improves code readability without affecting execution.
  4. Case Sensitivity: main and Main would refer to two different entities in a case-sensitive language like C++.

Experiment with the example above for a better grasp of these basic rules!

Variables in C++

Variables are like containers for data. When we declare a variable, we reserve space in memory to store a value. Think of variables as boxes that hold some contents.

In C++, we have three key actions associated with variables:

  1. Declaring a Variable: A variable must be declared before use, specifying its type followed by a name for the variable, E.g., int age;.
  2. Assigning Value to a Variable: A value can be assigned to a declared variable. E.g., age = 14;.
  3. Declaring and Assigning at the Same time: Both actions can be performed at once. E.g., int age = 14;.
  4. Printing a variable: A variable can be printed using std::cout, similar to printing text. E.g., std::cout << age;.

Here's how to do it:

#include <iostream> 

int main() {
   int age = 21;
   // The line below will print "Age: 21"
   std::cout << "Age: " << age << std::endl;

   float average = 98.75;
   // The line below will print "Average: 98.75"
   std::cout << "Average: " << average << std::endl;

   return 0;
}

Let's remember this good practice: Choose variable names relevant to their purposes.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal