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

Lecture1s 2

Uploaded by

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

Lecture1s 2

Uploaded by

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

Introductio

n to
Programmi
ng
Contents
• C++
• Escape Sequence & Comments
• Variables
• Constants
• User Input
• Data Types
• Operators
• Strings & String Concatenation
• Append
• User Input Strings
• Math’s
What is C++

• C++ is one of the most popular programming language.


• 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.
• to create computer programs, and is one of the most used language
in game development.
• It is an object-oriented programming language which gives a clear
structure to programs and allows code to be reused.
Example
• To start using C++, you need two things:
1. A text editor, like Notepad, to write C++ code.
2. A compiler, like GCC, to translate the C++ code into a language that the computer
will understand.
header file library that lets us
#include <iostream> work with input and output
objects

using namespace std; we can use names for objects and


variables from the standard library
int main()
{
cout << "Hello World!";
return 0;
}
Escape Sequence & Comments

New Line--To insert a new line, you can use the \n character or endl.
Horizontal tab-- \t
Backslash character (\)--\\
Double quote character-- \”……\”

Comments can be singled-lined or multi-lined.


Single-line comments start with two forward slashes (//)
Multi-line comments start with /* and ends with */
Escape Sequence & Comments…
#include <iostream>
using namespace std;
int main() {
cout << “Hello World\n";
cout << "Hello World" << endl; // endline means new line
cout << "Hello World\t"; /* Multi line comments
cout << "Hello World\\"; example*/
cout << “Hello \“World\".";
return 0;
}
Variables
• Variables are containers for storing data values.
• There are different types of variables-

Data Type Description


int stores integers (whole numbers), without decimals, such as 120 or -220
double stores floating point numbers, with decimals, such as 12.24 or -12.24
char stores single characters, such as 'a' or ‘A'. 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
Variables…
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 (myName and myname 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
Identifiers
• All C++ variables must be identified with unique names. These unique
names are called identifiers.
Variables…
• Syntax--
datatype variableName = value;
int myNumber = 10;
cout<< myNumber;
Or,
int myNumber;
myNumber = 10;
cout<< myNumber;
Variables…
int myNumber = 10; //
myNumber = 20; //
cout << myNumber; //
……………….
int myNumber = 50;
double myFloatNumber = 5.45;
char myLetter = 'A';
string myText = "Hello";
bool myBoolean = true;
Variables…
1
int x = 3;
int myAge = 20;
int y = 7;
cout << "I am " << myAge
int sum = x + y;
<< " years old.";
cout << sum;

int x = 2, y = 4, z int x, y, z;
= 4; x = y = z = 10;
cout << x + y + z; cout << x + y + z;
Constants

• When you do not want others (or yourself) to override existing


variable values, use the const keyword (this will declare the variable
as "constant", which means unchangeable and read-only)-

const int myNumber = 20;//myNumber will always


be 20
myNumber = 12; // error
const int perDayHr = 24;
const float PI = 3.14;
User Input
• cin is a predefined variable that reads data from the keyboard with
the extraction operator (>>).
int x;
cout << “Enter a number: ";
cin >> x;
cout << "Your number is: " << x;
NB: cout is pronounced "see-out". Used for output, and uses the
insertion operator (<<)
cin is pronounced "see-in". Used for input, and uses the extraction
operator (>>)
Example…
int x, y;
int sum;
cout << “Enter a number: ";
cin >> x;
cout << “Enter another number: ";
cin >> y;
sum = x + y;
cout << “The Sum is: " << sum;
Data Types
Data Type Size Description
boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or ASCII values
int 2 to 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing
7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing
15 decimal digits
Operators

• Operators are used to perform operations on variables and values.


• C++ divides the operators into the following groups:

1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
Arithmetic operators
Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example


+ Addition Adds together two values a+b
- Subtraction Subtracts one value from another a-b
* Multiplication Multiplies two values a*b
/ Division Divides one value by another a/b
% Modulus Returns the division remainder a%b
++ Increment Increases the value of a variable by 1 a++, ++a
-- Decrement Decreases the value of a variable by 1 a--, --a
Assignment Operators
• Assignment operators are used to assign values to variables.
Assignment Operators
= x = 5, x=5
int main() { int main() { int main() {
+= x += 3 x=x+3 int x = 3; double x = 5; int x = 5;
x += 3; x /= 2; x %= 2;
-= x -= 3 x=x-3 cout << x; cout << x; cout << x;
*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3
Comparison Operators
• Comparison operators are used to compare two values.
• The return value of a comparison is either true (1) or false (0).

Comparison Operators int x = 5;


int x = 6;
== Equal to x == y int y = 2; int y = 2;
!= Not equal x != y cout << (x > y); cout << (x >= y);
> Greater than x>y
< Less than x<y
>= Greater than or equal to x>= y
<= Less than or equal to x <= y
int x = 5; int x = 5;
int y = 3; int y = 5;
cout << (x !=y); cout << (x >= y);
Logical Operators
• Logical operators are used to determine the logic between variables
or values.
Operator Name Description Example
&& Logical Returns true if both statements are x < 5 && x < 10
and true
|| Logical Returns true if one of the statements x < 5 || x < 4
or is true
! Logical Reverse the result, returns false if the !(x < 5 && x < 10)
not result is true

int x = 5; int x = 5; int x = 5;


int y = 2; int y = 2; int y = 2;
cout << (x > 3 && y < 8); cout << (x > 6 || y < 3); cout << (!(x > 4 && y < 8));
Strings & String Concatenation
• Strings are used for storing text.
• A string variable contains a collection of characters surrounded by double
quotes.
string Mycourse = “C++” ;
cout<< Mycourse;
• The + operator can be used between strings to add them together to
make a new string. This is called concatenation.
string firstName = “Rahim";
string lastName = “Miah";
string fullName = firstName + "" + lastName;
cout << fullName;
Append
• A string in C++ is actually an object, which contain functions that can
perform certain operations on strings. For example, you can also
concatenate strings with the append() function.
• NB: C++ uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.

string firstName = “Rahim "; string x = “20";


string x = “20";
string lastName = “Miah"; string y = “30";
int y = 30;
string fullName = firstName.append(lastName); string z = x + y;
string z = x + y;
cout << fullName; cout << z;
User Input Strings

string fullName;
However, cin considers a space (whitespace, tabs, etc)
cout << “Enter Your Full Name: ";
as a terminating character, which means that it can only
cin >> fullName;
display a single word (even if you type many words):
cout << "Your Name is: " << fullName;

string fullName;
cout << “Enter Your Full Name: "; when working with strings, we often use the getline()
getline (cin, fullName); function to read a line of text. It takes cin as the first
cout << "Your Name is: " << fullName; parameter, and the string variable as second
Math's
Precedence of Operators
b/a+c

Precedence
!, ++, --, (type)
*, /, %
+, -
=

You might also like