Lecture 1
Lecture 1
Programming
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.
• C++ is used to create computer programs, and is one of the most used
language in game development.
• C++ 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.
#include <iostream>
using namespace std;
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-
int x, y, z;
int x = 2, y = 4, z = 4;
x = y = z = 10;
cout << x + y + z;
cout << x + y + z;
Constants
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
Arithmetic operators
Assignment Operators
= x=5 x=5
+= x += 3 x=x+3 int main() { int main() { int main() {
-= x -= 3 x=x-3 int x = 3; double x = 5; int x = 5;
*= x *= 3 x=x*3 x += 3; x /= 2; x %= 2;
/= x /= 3 x=x/3 cout << x; cout << x; cout << x;
%= x %= 3 x=x%3
Comparison Operators
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;
when working with strings, we often use the getline()
cout << “Enter Your Full Name: ";
function to read a line of text. It takes cin as the first
getline (cin, fullName);
parameter, and the string variable as second
cout << "Your Name is: " << fullName;
Math's