0% found this document useful (0 votes)
17 views6 pages

C++ Notes

Uploaded by

mcflurrydraco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

C++ Notes

Uploaded by

mcflurrydraco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Types If / else if / else Statement

In C++, the data types include: In C++, an if/else statement is used to


test a condition. If the condition is true,
1. Primitive Data Types: These are built-in
the code within the 'if' block is executed.
data types in C++. They include:
Else if, serves as a sub condition, and If
• Integers (int)
these conditions are false, the code
• Floating point numbers (float)
within the 'else' block is executed.
• Double floating-point numbers
(double) Here is an example:
• Boolean (bool)
• Character (char)
• Wide character (wchar_t)
• Void (void)

2. Derived Data Types: These are data types


that are derived from the primitive types.
• Function
• Array
• Pointer
• Reference

3. User-Defined Data Types: These are data


types that users define themselves. They
include:
• Class Loops
• Structure
For Loop (Initialization, Condition, Update)
• Union
• Enumeration When you know exactly how many times
you want to loop through a block of code
use a for loop instead of a while loop.
While Loop (Condition) {Code to be Foreach Loop
executed, Update}
Introduced in C++ version 11
The while loop is a more general-purpose (2011), which is used exclusively to loop
loop that continues executing that block of through elements of an array, without
code as long a specified condition is true. performing initialization, testing, and
increment / decrement.

Nested Loop
A loop that is inside another loop.

Do-While Loop
The block of code is executed once (do),
Before applying the condition (while).
Variables Multiple Variable Declaration

Identifiers – Unique names given to

variables.

One Value to Multiple Variable /


Chained Assignment or Declaration

1. Variable Declaration. No value.

• Purpose: A variable declaration


informs the compiler about a
variable's name and its data type, but
it does not allocate memory or
initialize the variable. !!NOTE – The sequence when using
• Example: extern int x; - This tells the chained declaration is from right to left.
compiler that there is an integer
variable named x that is defined
somewhere else in the program.
• extern keyword in C and C++ is used
to declare a variable, function, or
object as being defined in another If you change this, the result will
source file or in an external location. be different.
This is often used when you want to
use a variable or function from 2. Variable Definition. With Value.
another source file without
duplicating its definition. • Purpose: A variable definition not
only informs the compiler about the
Different Type of Declaration: variable but also allocates memory
for the variable and may also
Single Variable Declaration initialize it. In most cases, you will
use variable definitions in your code
to create and work with variables.
• Example: int y = 42; - This both
declares and defines an integer
variable named y, allocates memory
for it, and initializes it with the value
42.
Initialization of Variables in C++ Data Type Modifiers

1. Direct Initialization Data Type Meaning Size


(byte)
signed int can store positive and 4
Direct initialization involves
negative integer value.
initializing a variable by using the
assignment operator = or parentheses.
This is a straightforward way to set the unsigned only stores non-negative 4
initial value of a variable. It is commonly int integer value.
used for fundamental data types like
integers, floats, and characters. Here's short for small integer; -32768 2
an example: to 32767.
long for large integer. 4

unsigned for large positive integer 4


long or 0.
long long for large integer. 8

unsigned for very large positive 8


long long integer or 0.
2. Constructor Initialization. ChatGPT.
long used for long floating- 8
Constructor initialization in C++ double point numbers
refers to the process of initializing the
member variables of a class within its signed for characters -127 to 1
constructor. In C++, a constructor is a char 127.
special member function that gets unsigned for character 0 to 255. 1
invoked when an object is created. char
Constructors are responsible for setting
up the initial state of an object, and this !!NOTES:
often involves initializing its member *By default, integers are signed. So just int.
variables.
*Signed and unsigned are only used for int
and char only.
Operators

*Always remember PEMDAS rule. Logical Operators


In math or any other programming AND TABLE
languages, multiplication ( * ) and T T T
&& T F F
division ( / ) are highly priority. Hence, it
F T F
is important to use parenthesis ( ). F F F
OR TABLE
cout << insertion operator. T T T
|| T F T
cin >> extraction operator. F T T
F F F
Arithmetic Operators NOT TABLE
! T F
+ Addition F T
- Subtraction
* Multiplication
/ Division
% Modulo Casting Operator
++ Increment static_cast <>
-- Decrement dynamic_cast <>
const_cast <>
reinterpret_cast <>

Relational / Comparison Operators


== Is equal to
!= Is not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

Assignment Operators
= simple assignment
+= add AND assignment
-= subtract AND assignment
*= multiply AND assignment
/= modulo AND assignment
%= modulo shift AND assignment
<<= left shift AND assignment
>>= right shift AND assignment
&= Bitwise shift AND assignment
^= Bitwise exclusive shift AND assignment
|= Bitwise inclusive shift AND assignment

You might also like