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