Basic Concepts of C++
Basic Concepts of C++
Basic Concepts of C++
STATEMENTS
In C++, statements are individual instructions that make up a program's
logic and functionality. They are the basic building blocks of C++ code
and are executed sequentially within a program. Here are some common
types of statements in C++:
1.Expression Statements: These are statements that consist of an
expression followed by a semicolon. For example:
x = y + z;
2.Declaration Statements: These statements declare variables and their
types. For example;
int x;
3.Assignment Statements: These statements assign values to variables.
For example:
x = 10;
NB
Every statement in C++ must end with a semicolon ;. This includes
declaration statements, assignment statements, function calls, and
control flow statements.
C++ ignores spaces, tabs, and newline characters between tokens.
However, proper indentation and spacing are essential for code
readability.
C++ is case-sensitive, meaning uppercase and lowercase letters are
treated differently. For example, myVariable and myvariable are
considered distinct identifiers
Blocks of code are enclosed within curly braces {}. This defines a
scope for local variables and control flow statements.
Variables must be declared before they are used. This typically
occurs at the beginning of a block or function.
C++ is a statically typed language, meaning variable types must be
explicitly declared before use.
And to separate two block of code use the “endl” function
STRUCTURE
#include <iostream>
Using namespace std;
int main(){
return 0; }
int before main indicates that the function returns an integer value.
main() is the name of the function
iostream is a header that contains other functions and function use
to output text and its called “cout<<”.
Namespace std also is a subfunction of the iostream that contains
the output function and others that we will deal with
The curly braces {} enclose the body of the function, containing
the code to be executed.
return 0; at the end of main is a convention to indicate that the
program executed successfully. A non-zero value typically
indicates an error.
VARIABLES
In C++, variables are placeholders for storing data values. They have a data type
that determines what kind of data they can hold, such as integers, floating-point
numbers, characters, etc. Variables must be declared before they can be used,
specifying their name and type. They can be modified throughout the program,
allowing for dynamic data manipulation. Additionally, variables can have different
scopes, affecting where they can be accessed within the program. Naming of a
variable depends on you the coder.
DATA TYPES
In C++, data types specify the type of data that variables can store. Common data
types include:
Integer: Used for whole numbers. examples 1,2,3,4….
int age = 25;
Boolean: Used for representing true or false values. The integral value for true is
“1” and that of false is “0”.
Always the data type is declared followed by the variable name as it is in the black
backgrounds above.
MATHS OPERATORS
Addition (+): Adds two operands.
Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides the first operand by the second.
Modulus (%): Returns the remainder of the division of the first operand by
the second.
Increment (++): Increases the value of a variable by 1.
int a = 10;
int b = 5;
In C++, the "auto" keyword allows for automatic type inference, where the
compiler determines the type of a variable based on its initializer. This enhances
code readability and flexibility, especially with complex types. Auto can only be
used when you are initializing a code.