Why Use C++
Why Use C++
APPLICATIONS OF C++
int main() {}
It is a main function which means it will be executed first. Any code inside its curly brackets will
be executed
cout<<;
Cout is an object used together with insertion operator (<<), it is used to print text of the output
return 0;
Return 0 ends the function
Note:
● Every C++ statement should end with a semicolon
● The compiler ignores white spaces. However, multiple lines make the code more
readable
● Do not forget to add the closing curly bracket } to actually end the main function
\n
It is a new line character which is called an escape sequence and it forces the cursor to change
its position to the beginning of the next line on the screen. This results in a new line
endl
It is used to insert a new line
Comment
Comments in C++ are meant to explain the code as well as to make it more readable. The
purpose of comment is to provide information about code lines
Cin
The cin object in C++ is an object of class iostream. It is used to accept the input from the
standard input device
The extraction operator(>>) is used along with the object cin for reading inputs. The extraction
operator extracts the data from the object cin which is entered using the keyboard
Cout
The cout object in C++ is an object of class iostream. It is used to display the output from the
standard output device
The data needed to be displayed on the screen is inserted in the standard output stream (cout)
using the insertion operator (<<)
DATA TYPES
● Data type is an attribute associated with a piece of data that tells the computer system
how to interpret its value
● Understanding data types ensures that data is collected in the preferred format and the
value of each property is as expected
● A good understanding of data types is required to properly define event properties and
entity properties
These are data types that are built in or predefined data types that can be used directly by the
user to declare variables
Integer
● Keyword used for integer data type is int.
● Integer typically requires 4 bytes of memory space and ranges from “-2147483648 to
2147483647”
Character
● Character data type is used for storing characters.
● Keyword used for character data type is char.
● It typically requires 1 byte of memory space and ranges from “-128 to 127” or “0 - 255”
Boolean
● It is used for storing boolean or logical values.
● A boolean variable can store either true or false.
● Keyword used is bool
Floating point
● It is used for storing single precision floating point values or decimal values.
● Keyword used is float.
● Float variable typically requires 4 bytes of memory space.
Double floating point
● It is used for storing double precision floating point values or decimal values.
● Keyword used is double.
● It typically requires 8 bytes of memory space.
Wide character
● Wide character is also a character data type, but this data type has size greater than the
normal 8 bit data type.
● It is represented as wchar_t. It is generally 2 or 4 bytes long.
sizeof
● The sizeof is a keyword, but it is a compile time operator that determines the size in
bytes of a variable or a data type
● The sizeof operator can be used to get the size of classes, structures, unions or any
other user defined data type
● The syntax of sizeof is: sizeof (data type)
Logic Gates
A logic gate is a basic building block of any digital circuit. It takes 1 or 2 inputs and produces an
output based on those inputs. Outputs maybe high / true(1) or low / false(0)
There are a few basic logic gates and they are:
1. AND gate
2. OR gate
3. NOT gate
4. NAND gate
5. NOR gate
6. XOR gate
7. XNOR gate
AND Gate
● It is performed in C++ using && operator
● The AND gate gives an output of 1 if both the inputs are 1, otherwise it gives 0
OR Gate
● It is performed in C++ using || operator
● The output of OR gate becomes one if anyone of its inputs is 1
● OR gate is also called an any or all gate
● It can be called as inclusive OR gate because it includes the condition both the inputs
can be present
NOT Gate
● NOT gate acts as an inverter
● It takes only one input
● If the input is given as 1 / true, it will invert the result as 0 / false and vice versa
XOR Gate
● The bit wise XOR (^) operator returns 1 if and only if one of the operands is 1. However ,
if both of the operands are 0, or if both are 1 then the result is 0
Operators
Operators are used to perform operations on variables and values.
The following is a list of operators we are going to learn
1. Addition operator (+)
2. Subtraction operator (-)
3. Multiplication operator (*)
4. Division operator (/)
5. Increment operator (post and pre)
● Prefix - increment will be done before the rest of the expression (x = x+1; can be
written as ++x;)
● Postfix - increment will be done after the complete expression is evaluated (x =
x+1; can be written as x++;)
6. Decrement operator (post and pre)
7. Addition assignment operator (+=)
The addition assignment operator (+=) adds the value of the right operand to a variable
and assigns the result to the variable
8. Subtraction assignment operator (-=)
9. Mod operator (%)
● The modulo operator, denoted by “%” is an arithmetic operator.
● The modulo division operator produces the remainder of a division
● Division should be performed using integers to get proper results
● Syntax : if x and y are integers, then the expression:
x%y
return value;
● If y completely divides x, the result of expression is 0
● If x is not complete divisible by y, then the result will be the remainder [1, x-1] ]
● If x is 0, then division by 0 is a compile time error
STRING
● Strings are used to store text values, it is basically a collection of characters
● String library is required to use strings in C++ which is included using #include<string>
● Values can be assigned to them using text in “” and assignment operator
● == and != are also applicable in string operation
● Strings cannot be changed, that means if you make a mistake while giving an input into
the program, even using backspace and rectifying the mistake on screen would not work
● Cin and getline (cin, string_name) are used to get string input from users, for one word
and multiple word inputs respectively. We can use numbers, symbols as strings as well
Substring
● String is the collection of characters, so we can access a part of string using index, for
which a particular string the index starts with 0 and goes up to the length of the string
● The access is performed using substring method
Cmath
● The C++ <cmath> header file declares a set of functions to perform mathematical
operations such as: sqrt() to calculate the square root, log() to find the natural logarithm
of a number
● It is the standard library of C++
● This library also provides functionality for trigonometric functions such as sin, cos, tan
etc
● Power and exponential functions are also available through the <cmath> header
Sqrt() function
● Sqrt() function does what it simply suggests, it calculates the square roots of a number
● The number can be int, float, double, etc datatype
Round() function
● It takes an input as a number and convert into whole number by simply rounding it off to
its nearest integer
● It takes input in the form of double, float and long double data type
Pow() function
● The pow() function returns the result of the first argument raised to the power of the
second argument
Abs() function
● The abs() function removes the sign of the number supplied (mostly used for negative
numbers) to it
Remainder() function
● The remainder function in C++ computes the flotation point remainder of
numerator/denominator (round to nearest)
Conditionals
● If condition
● Else if condition
● Else condition
Logical conditionals
● Less than (a<b)
● Less than equal to (a<=b)
● Greater than (a>b)
● Greater than equal to (a>=b)
● Equal to (==)
● Not equal to (!=)
● Or (||)
● And (&&)
● Not (!)
If - else if - else
The if else statement is used to execute a block of code among two alternatives. However, if we
need to make a choice between more than two alternatives, we use the if - else if - else
statement.
Syntax
if (condition1) {
//code
}
else if (condition2) {
//code
}
else (condition3) {
//code
}
● If condition 1 evaluates to true, the code block 1 is executed
● If condition 1 evaluates to false, the condition 2 is executed
● If condition 2 evaluates to true, the code block 2 is executed
● If condition 2 evaluates to false, the code block 3 is executed
Note
There can be more than 1 else if statement but only of if and else statements
If the body of if else has only 1 statement, you can omit { } in program
Nested conditionals
● Sometimes, we need to use an if statement inside another conditional statement. This is
known as nested conditional statements
● Think of it as multiple layers of conditional statements.
● There is a first outer conditional statement, and inside it is another inner conditional
statement
Ternary operator
● In C++, the Ternary operator (also known as conditional operator) can be used to
replace if else in certain scenarios
● A Ternary operator evaluates the test condition and executes a block of code based on
the result of the condition
Syntax
● Here the condition is evaluated and if the condition is true, expression 1 is executed
● And is the condition is false, expression 2 is executed
● The ternary operator takes 3 operands (condition, expression 1, expression 2) hence the
name ternary operator
Note:
We should only use the ternary operator if the resulting statement is short
● It is also possible to use one ternary operator inside another ternary operator. It is called
the nested ternary operator
Switch
Syntax
switch(expression) {
Case x:
//code block
Break;
Case y:
//code block
Break;
Default:
//code block
}
How it works
Break keyword
● When C++ reached the break keyword, it breaks out of the switch block
● This will stop the execution of more code and case testing inside the block
● When a match is found, and the job is done it is time for a break. There is no need for
more testing
Default keyword
The default keyword specifies some code to run if there is no case match
While loop
● It is the most simple loop in C++
● It is an entry controlled loop. In a while loop, a condition is evaluated before processing
the body of a loop. If a condition is true, only then the body of the loop is executed
● After the body of the loop is executed, then the control goes back at the beginning, and
the condition is checked if its true, the process is executed until the condition becomes
false
● Once the condition becomes false, the control goes out of the loop
● After exiting the loop, the control goes to the statements which are immediately after the
loop
● The body of a loop can contain more than 1 statement. If it contains only one statement
the the curly braces are not compulsory
● In while loop, if the condition is not true then body of a loop will not be executed, even
once
Syntax
while(condition){
statement;
}
Do While loop
● The do while loop is a variant of the while loop.
● This loop will execute the code block once, before checking if the condition is true, then it
will repeat the loop as long as the condition is true
Syntax
do{
body of loop;
}
while (condition);
● Here the body of the loop is executed at first, then the condition is evaluated
● If the condition evaluates to true, the body of the loop inside the do statement is
executed again
● The condition is evaluated again
● If the condition evaluates to true, the body of the loop inside the do statement is
executed again
● This process continues until the condition evaluates to false, then the loop stops