Basic C++ Syntax
#include <iostream>
#include is a preprocessor directive that tells the preprocessor to include header files in
the program.
<> indicate the start and end of the file name to be included.
iostream is a header file that contains functions for input/output operations (cin and
cout).
using namespace std
The same program that we saw in the given example, the program will still run
without this namespace statement.
#include <iostream>
int main()
{
std::cout<<”helow world”;
}
Using namespace std line can be omitted and replaced with the std keyword, followed
by the :: operator.
Some general key points regarding c++ Syntax
Every statement in C++ ends with a semicolon.
C++ compiler will start executing your code from the first line of the main function
Code inside the main function should be written between curly brackets/
#include<iostream> is the common header file for all programs in c++ as it
provides basic input output facilities.
Comments in C++
Comments – are portions of the code ignored by the compiler which allow the user to
make simple notes in the relevant areas of the source code.
Comments come either in block form or as single lines.
The // signify a single line of comment
The /**/ signify a multiline comment.
User input in C++
We have learned that cout is used to print or display the output of the program.
cin is a predefined variable that reads data from the keyboard.
Activity 3
Instruction:
1. Add a single and multiline comment in c++ code.
2. Write a code that can move the cursor to console next line.
3. Write a simple program that allows the user to input in c++.
Data Types in C++
Learning Objectives
By the end of this lesson, students should be able to:
1. Define what a data type is and why it is important in programming.
2. Identify the basic data types in C++ (int, float, double, char, bool, string).
3. Differentiate between numeric and non-numeric data types.
4. Apply proper data types when declaring variables in C++.
5. Write and run simple programs using different data types.
Introduction
In C++, every variable must have a data type.
A data type tells the computer what kind of data is stored in a variable
(numbers, letters, words, true/false values, etc.).
Choosing the correct data type is important for efficiency and correctness.
Analogy:
Think of variables as containers (like boxes). The data type defines what kind of item
the box can hold:
A “whole number” box (int)
A “decimal number” box (float/double)
A “letter” box (char)
A “true/false” box (bool)
A “word/sentence” box (string)
Data Type Meaning
Integer Stores whole numbers without decimals.
Floating-Point (float and double) Used for decimal numbers.
float = 4 bytes (7 digits precision).
double = 8 bytes (15 digits precision).
Character (char) Stores a single character, enclosed in
single quotes ' '.
Boolean (bool) Stores true (1) or false (0) values.
String (string) Stores a sequence of characters
(text/words).