TOPIC
SUB-TOPIC int main() {
SUB-SUB-TOPIC cout << "Hello World!";
return 0;
}
I. Intro
What is C++? Example explained
● C++ is a cross-platform language that can be ➔ Line 1: #include <iostream> is a header file library
used to create high-performance applications. that lets us work with input and output objects, such
● C++ was developed by Bjarne Stroustrup, as an as cout (used in line 5). Header files add
extension to the C language. functionality to C++ programs.
● C++ gives programmers a high level of control ➔ Line 2: using namespace std means that we can
over system resources and memory. use names for objects and variables from the
● The language was updated 4 major times in 2011, standard library.
2014, 2017, and 2020 to C++11, C++14, C++17, ➔ Line 3: A blank line. C++ ignores white space. But
C++20. we use it to make the code more readable.
➔ Line 4: Another thing that always appear in a C++
Why use C++? program, is int main(). This is called a function. Any
● C++ is one of the world's most popular code inside its curly brackets {} will be executed.
programming languages. ➔ Line 5: cout (pronounced "see-out") is an object
● C++ can be found in today's operating systems, used together with the insertion operator (<<) to
Graphical User Interfaces, and embedded output/print text. In our example it will output "Hello
systems. World".
● C++ is an object-oriented programming language Note: Every C++ statement ends with a semicolon ;.
that gives a clear structure to programs and Note: The body of int main() could also been written
allows code to be reused, lowering development as:
costs. int main () { cout << "Hello World! "; return 0; }
● C++ is portable and can be used to develop ➔ Remember: The compiler ignores white spaces.
applications that can be adapted to multiple However, multiple lines makes the code more
platforms. readable.
● C++ is fun and easy to learn! ➔ Line 6: return 0 ends the main function.
● As C++ is close to C, C# and Java, it makes it easy ➔ Line 7: Do not forget to add the closing curly bracket
for programmers to switch to C++ or vice versa. } to actually end the main function.
The Difference Between C and C++? Omitting Namespace
● C++ was developed as an extension of C, and
You might see some C++ programs that runs
both languages have almost the same syntax.
without the standard namespace library. The using
● The main difference between C and C++ is that
namespace std line can be omitted and replaced with
C++ support classes and objects, while C does
the std keyword, followed by the :: operator for some
not.
objects:
II. Syntax #include <iostream>
#include <iostream> int main() {
using namespace std;
1
std::cout << "Hello World!"; * Multiplication Multiplies two x*y
values
return 0;
} / Division Divides one value x/y
by another
% Modulus Returns the x%y
III. Output division remainder
C++ Output (Print Text) ++ Increment Increases the value x ++ y
of a variable by 1
The cout object, together with the << operator, is -- Decrement Decreases the --x
used to output values/print text: value of a variable
by 1
IV. Comments
V. Variables Assignment Operators
Assignment operators are used to assign values to
VI. OC++ Conditions and If Statements
variables. In the example below, we use the assignment
Operators are used to perform operations on
operator (=) to assign the value 10 to a variable called x:
variables and values. In the example below, we use
the + operator to add together two values:
int x = 100 + 50; int x = 10;
Although the + operator is often used to add The addition assignment operator (+=) adds a
together two values, like in the example above, it can value to a variable:
also be used to add together a variable and a value, or a
variable and another variable: int x = 10;
x += 5;
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400) A list of all assignment operators:
OPERATOR Example Same As
C++ divides the operators into the following groups:
● Arithmetic operators = x=5 x=5
● Assignment operators += x += 3 x=x+3
● Comparison operators
● Logical operators -= x -= 3 x=x-3
● Bitwise operators *= x *= 3 x=x*3
/= x /= 3 x=x/3
Arithmetic Operators
Arithmetic operators are used to perform %= x %= 3 x=x%3
common mathematical operations.
&= x &= 3 x=x&3
|= x |= 3 x=x|3
OPERATOR Name Description Example
^= x ^= 3 x=x^3
+ Addition Adds together two x+y
values
>>= x >>= 3 x = x >> 3
- Subtraction Subtracts one x-y
value from another <<= x <<= 3 x = x << 3
2
VII. User Input
Assignment Operators
Comparison operators are used to compare two
values. Note: The return value of a comparison is either
true (1) or false (0). In the following example, we use the
greater than operator (>) to find out if 5 is greater than 3:
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater
than 3
A list of all comparison operators:
OPERATOR Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal x >= y
to
<= Less than or equal to x <= y
Logical Operators
Logical operators are used to determine the
logic between variables or values:
OPERATOR Name Description Example
&& Logical Returns true x < 5 && x < 10
and if both
statements
are true
|| Logical Returns true x < 5 || x < 10
or if one of the
statements
is true
! Logical Reverse the !(x < 5 && x < 10)
not result,
returns false
if the result
is true
3
SOURCES:
● https://www.w3schools.com/cpp/cpp_operator
s.asp