To start coding with C++ it is mandatory to learn and know the syntax of C++ and some important notations otherwise you will not understand what is happening with elements and whats the flow of working.
In this session we will learn the following.
Lets have an overview of all these points.
Variables:
Data Types:
In C++, data types are used to specify the type of data that a variable can store. C++ provides several built-in data types to handle different types of data. Here are some commonly used data types in C++:
- Integer Types:
- int: Used to store integer values.
- short: Short integer.
- long: Long integer.
- long long: Long long integer.
- unsigned int, unsigned short, unsigned long, unsigned long long: Unsigned integer types (can't store negative values).
int age = 25;
unsigned int count = 100;
- Floating-Point Types:
- float: Single-precision floating-point number.
- double: Double-precision floating-point number.
- long double: Extended precision floating-point number.
float pi = 3.14;
double gravity = 9.81;
- Character Types:
- char: Used to store a single character.
- char16_t, char32_t: 16-bit and 32-bit character types for Unicode characters.
- wchar_t: Wide character type.
char grade = 'A';
- Boolean Type:
Example:
bool isStudent = true;
- Void Type:
Example:
void function() {
// This function doesn't return any value
}
- Derived Types:
- Arrays: Contiguous collection of elements of the same type.
- Pointers: Variables that store memory addresses.
- References: Alias to an existing variable.
- Structures: User-defined composite data type that groups related data members.
- Enums: User-defined data type used for defining named constants.
Comments:
- Single-Line Comments:
- Multi-Line Comments:
Statements:
1. Expression Statements
2. Declaration Statements
3. Compound Statements (Blocks)
4. Selection Statements
- if Statement
- if-else Statement
- switch Statement
- while Loop
- do-while Loop
- for Loop
- break Statement
- continue Statement
- return Statement
Constants and Identifiers:
Constants in C++ are values that do not change during the execution of a program. They are used to represent fixed values such as numbers, characters, or strings. Constants can be of various types, including integer constants, floating-point constants, character constants, and string literals. In C++, constants are declared using the const keyword or using preprocessor directives like #define etc. For example const double PI = 3.14159;
#define MESSAGE "Hello, world!"
While Identifiers in C++ are names given to various programming elements such as variables, functions, classes, and labels. Identifiers are used to uniquely identify these elements within the program.
There are certain rules that must be followed while defining identifiers:
- They can include letters (both uppercase and lowercase), digits, and underscores.
- The first character must be a letter or an underscore.
- Identifiers are case-sensitive.
- They cannot be C++ keywords.
int class; // variable identifier
void calculateHeight(); // function identifier
class MyBooks {}; // class identifier
const int MAX_VALUE = 320; // constant identifier
0 Comments