0% found this document useful (0 votes)
50 views

C++ Programing

Abcd

Uploaded by

tmengistutk1970
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

C++ Programing

Abcd

Uploaded by

tmengistutk1970
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C++ programing

Introduction to C++ programing


Language
What is C++ programing Language?
• C++ is a popular programming language.
• C++ is used to create computer programs, and is one of the
most used language in game development.
• C++ is a cross-platform language that can be used to create
high-performance applications.
• C++ was developed by Bjarne Stroustrup, as an extension to
the C language.
• C++ gives programmers a high level of control over system
resources and memory.
• The language was updated 4 major times in 2011, 2014,
2017, and 2020 to C++11, C++14, C++17, C++20.

C++ Get Started
• To start using C++, you need two things:
• A text editor, like Notepad, to write C++ code
• A compiler to translate the C++ code into a
language that the computer will understand.
C++ Install IDE
• An IDE (Integrated Development Environment)
is used to edit AND compile the code.
• Popular IDE's include Code::Blocks, Eclipse,
and Visual Studio. These are all free, and they
can be used to both edit and debug C++ code.
• We will use Dev C++ to learn.
• To start code go to File > New > Source File.
• Write C++ code and save the file. File name is
followed by file extention.
Cont’d
• Line 1: #include <iostream> is a header file
library that lets us work with input and output
objects, such as cout . Header files add
functionality to C++ programs.
• Line 2: using namespace std means that we
can use names for objects and variables from
the standard library.
• Line 3: A blank line. C++ ignores white space. But we use it
to make the code more readable.
• Line 4: Another thing that always appear in a C++ program
is int main(). This is called a function. Any code inside its
curly brackets {} will be executed.
• Line 5: cout (pronounced "see-out") is an object used
together with the insertion operator (<<) to output/print
text. In our example, it will output "Hello World!".
• Note: Every C++ statement ends with a semicolon ;.
• Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
• Remember: The compiler ignores white spaces. However,
multiple lines makes the code more readable.
• Line 6: return 0 ends the main function.
• Line 7: Do not forget to add the closing curly bracket } to
actually end the main function.
To insert a new line, you can use
the \n character or endl manipulator:
C++ Comments
• Comments can be used to explain C++ code, and
to make it more readable.
• It can also be used to prevent execution when
testing alternative code.
• Comments can be singled-lined or multi-lined.
• Single-line comments start with two forward
slashes (//).
• Any text between // and the end of the line is
ignored by the compiler (will not be executed).
• Multi-line comments start with /* and ends
with */.
C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables
(defined with different keywords), for example:
• int - stores integers (whole numbers), without
decimals, such as 123 or -123
• double - stores floating point numbers, with
decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
• string - stores text, such as "Hello World". String
values are surrounded by double quotes
• bool - stores values with two states: true or false
Declaring (Creating) Variables
• Syntax
type variableName = value;
• The general rules for naming variables are:
• Names can contain letters, digits and underscores
• Names must begin with a letter or an underscore (_)
• Names are case-sensitive (myVar and myvar are
different variables)
• Names cannot contain whitespaces or special
characters like !, #, %, etc.
• Reserved words (like C++ keywords, such as int) cannot
be used as names
Constants
• When you do not want others (or yourself) to
change existing variable values, use the const
keyword (this will declare the variable as
"constant", which means unchangeable and
read-only):
const int minutesPerHour = 60;
const float PI = 3.14; // PI will always be 3.14
C++ User Input
• You have already learned that cout is used to
output (print) values. Now we will use cin to
get user input.
• cin is a predefined variable that reads data
from the keyboard with the extraction
operator (>>).
• In the following example, the user can input a
number, which is stored in the variable x. Then
we print the value of x:
Example
Condition
• C++ Conditions and If Statements
• You already know that C++ supports the usual logical
conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
• You can use these conditions to perform different actions for
different decisions.
• C++ has the following conditional statements:
• Use if to specify a block of code to be executed, if a
specified condition is true
• Use else to specify a block of code to be executed, if
the same condition is false
• Use else if to specify a new condition to test, if the first
condition is false
• Use switch to specify many alternative blocks of code
to be executed
• The if Statement
• Use the if statement to specify a block of C++ code to
be executed if a condition is true.
• Use the else statement to specify a block of
code to be executed if the condition is false.
The else if Statement

• Use the else if statement to specify a new


condition if the first condition is false.

You might also like