Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C++, created by Bjarne Stroustrup in 1983, evolved from C to enhance

object-oriented programming (OOP) capabilities while maintaining C's


efficiency. It became widely adopted for system and application
software development, offering features like classes, inheritance, and
polymorphism. Its versatility spans from operating systems to game
development, making it a cornerstone language in computer science.

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.

A header is a file that contains functions

In programming, a function is a self-contained block of code that


performs a specific task or set of tasks

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;

We declare a variable named age of type int (integer).


We initialize the variable with the value 25 using the assignment operator =. This
means that age now holds the value 25.
Floating-point: Used for decimal numbers. examples 2.34,78.9,pie,9.82 etc.

// Declaring a float variable


float floatNumber = 3.14f; // Note
the 'f' suffix to indicate a float

// Declaring a double variable


double doubleNumber = 3.14159265359

Character: Used for individual characters. example is ‘H’,’D’,’W’. They denoted


by single quotation marks.
char grade = 'A';

Boolean: Used for representing true or false values. The integral value for true is
“1” and that of false is “0”.

bool online = true;

String : Used for text . example: “Hello World”,

string message = "Hello, World!";

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.

 Decrement (--): Decreases the value of a variable by 1.

int a = 10;

int b = 5;

int sum = a + b; // sum is now 15

int difference = a - b; // difference is now

int product = a * b; // product is now 50

int quotient = a / b; // quotient is now 2

int remainder = a % b; // remainder is now 0

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.

When it comes to separating text and comments in C++, comments serve as


explanatory text within the code to provide context or explanation. They are
prefixed with "//" for single-line comments or enclosed within "*/ */" for multi-line
comments. This practice enhances code readability and maintainability by
documenting the purpose and logic behind each segment of code .
// Declare and initialize variables using auto
auto number = 10; // auto infers the type as int
// Declare and initialize variables using auto
auto message = "Hello, World!";
auto number = 10; // auto infers the type as
/* auto infers the type as const char*/
int
auto message = "Hello, World!"; // auto
infers the type as const char*

 The "auto" keyword is used to declare and initialize variables number


and message without explicitly specifying their types. The compiler
infers their types based on the provided initializers.
 Comments are used to provide explanations and context for each
segment of code. They clarify the purpose of the variables and the
output statements.

You might also like