C++ example: part1
Output (Print Text)
1 #include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
} // Hello World!
2 #include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
} // Hello World!
3 #include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
} // Hello World!I am learning C++
New Lines
4 #include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
} // Hello World!
I am learning C++
5 #include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n\n";
cout << "I am learning C++";
return 0;
} // Hello World!
I am learning C++
6 #include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
} // Hello World!
I am learning C++
7 #include <iostream>
using namespace std;
int main() {
cout << "Hello World!\t";
cout << "I am learning C++";
return 0;
} // Hello World! I am learning C++
8 #include <iostream>
using namespace std;
int main() {
cout << "Hello World!\\";
cout << "I am learning C++";
return 0;
} // Hello World!\I am learning C++
9 #include <iostream>
using namespace std;
int main() {
cout << "They call him \"Johnny\".";
return 0;
} // They call him "Johnny".
Comments
10 #include <iostream>
using namespace std;
int main() {
// This is a comment
cout << "Hello World!";
return 0;
}
11 int main() {
cout << "Hello World!"; // This is a comment.
return 0;
}
12 #include <iostream>
using namespace std;
int main() {
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
return 0;
}
Variables
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 Variables
13 #include <iostream>
using namespace std;
int main() {
int myNum = 15;
cout << myNum;
return 0;
} // 15
14 #include <iostream>
using namespace std;
int main() {
int myNum;
myNum = 15;
cout << myNum;
return 0;
} // 15
Other Types
int myNum = 5; // Integer (whole number without
decimals)
double myFloatNum = 5.99; // Floating point number (with
decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
Display Variables
15 #include <iostream>
using namespace std;
int main() {
int myAge = 35;
cout << "I am " << myAge << " years old.";
return 0;
} // I am 35 years old.
Add Variables Together
16 #include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
return 0;
} // 11
Declare Many Variables
17 #include <iostream>
using namespace std;
int main() {
int x = 5, y = 6, z = 50;
cout << x + y + z;
return 0;
} // 61
Identifiers
18 #include <iostream>
using namespace std;
int main() {
// Good name
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
cout << minutesPerHour << "\n";
cout << m;
return 0;
} // 60
60
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 name.
Constants
19 #include <iostream>
using namespace std;
int main() {
const int myNum = 15;
myNum = 10;
cout << myNum;
return 0;
} // error
20 #include <iostream>
using namespace std;
int main() {
const int minutesPerHour = 60;
const float PI = 3.14;
cout << minutesPerHour << "\n";
cout << PI;
return 0;
} // 60
3.14
User Input
You have learned that cout is used to output values. Now we will
use cin to get user input.
21 int main() {
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x;
return 0;
}
Write a c++ program to add two numbers, the user is asked to enter two integers?
22 int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}
Data Types
. من نوع بيانات محدد++ C يجب أن يكون المتغير في
Basic Data Types
Data Type Size
boolean 1 byte
char 1 byte
int 4 bytes
float 4 bytes
double 8 bytes
23 int main () {
// Creating variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myString = "Hello"; // String
// Print variable values
cout << "int: " << myNum << "\n";
cout << "float: " << myFloatNum << "\n";
cout << "double: " << myDoubleNum << "\n";
cout << "char: " << myLetter << "\n";
cout << "bool: " << myBoolean << "\n";
cout << "string: " << myString << "\n";
return 0;
}
Numeric Data Types
24 int main () {
int myNum = 1000;
cout << myNum;
return 0;
}
int main () {
float flo
return 0;.
}
int main () {
double myNum = 19.99;
cout << myNum;
return 0;
}
Boolean Types
25 int main() {
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << "\n";
cout << isFishTasty;
return 0;
} // o
1
Character Data Types
26 int main () {
char myGrade = 'B';
cout << myGrade;
return 0;
} // B
you can use ASCII values to display certain characters:
27 int main () {
char x = 65, y = 66, z = 67;
cout << x;
cout << y;
cout << z;
return 0;
} // ABC
String Data Types
To use strings, you must include the header file #<string> library
28 #include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
Operators
Operators are used to perform operations on variables and values.
Arithmetic operators +, -, *, /, %, ++, --
29 int main() {
int x = 5;
int y = 3;
cout << x + y<<endl;
cout << x - y<<endl;
cout << x * y<<"\n";
cout << x / y<<"\n\n";
cout << x % y;
return 0;
}8
2
15
1
2
Increment & decrement
30 int main() {
int x = 5;
++x; // x ++
cout << x;
return 0;
} // 6
31 int main() {
int x = 5;
--x;
cout << x;
return 0;
} // 4
32 int main() {
int x = 5;
--x; x--;
cout << x;
return 0;
} // 3
33 int main() {
int x = 5;
int y = x+2;
--x; x--;
cout << x<<" "<<y<<endl;
return 0;
} // 3 7
Assignment operators =
Assignment operators are used to assign values to variables.
34 int main() {
int x = 10;
cout << x;
return 0;
} // 10
Operators Associativity
Addition assignment operator (+=)
35 int main() {
int x = 10;
x += 5; // x = x + 5;
cout << x;
return 0;
} // 15
36 int main() {
int x = 5;
x += 3;
x -= 3;
x *= 3;
cout << x;
return 0;
} // 15
37 int main() {
int x = 5;
x += 3;
x -= 3;
x *= 3;
x %= 3;
cout << x;
return 0;
} // 0
38 int main() {
double x = 5; // if x is integer: int x = 5 ; the output is 1
x /= 3;
cout << x;
return 0;
} // 166667
Comparison
Comparison operators are used to compare two values (or variables).
39 int main() {
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
cout << (x == y); // returns 0 (false) because 5 is not equal to 3
return 0;
}
40 int main() {
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
cout << (x < y); // returns 0 (false) because 5 is not less than 3
cout << (x >= y); // returns 1 (true) because five is greater than, or equal, to 3
cout << (x <= y); // returns 0 (false) because 5 is neither less than or equal to 3
cout << (x == y); // returns 0 (false) because 5 is not equal to 3
cout << (x != y); // returns 1 (true) because 5 is not equal to 3
return 0;
} // 101001
Logical Operators
Logical operators are used to determine the logic between variables or values.
41 int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10) <<endl; // returns true (1) because 5 is greater than 3 AND 5
is less than 10
cout << (x > 3 || x < 4)<<endl; // returns true (1) because one of the conditions are true
(5 is greater than 3, but 5 is not less than 4)
cout << (!(x > 3 && x < 10))<<endl; // returns false (0) because ! (not) is used to
reverse the result
return 0;
}
Strings
A string variable contains a collection of characters surrounded by double quotes:
42 int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
String Concatenation
The + operator can be used between strings to add them together to make a
new string.
43 #include <iostream>
#include <string>
using namespace std;
int main () {
string firstName = "Ali ";
string lastName = "Mohammad";
string fullName = firstName + lastName;
cout << fullName;
return 0;
} // Ali Mohammad
44 #include <iostream>
#include <string>
using namespace std;
int main () {
string firstName = "Ali ";
string lastName = "Mohammad";
string fullName = firstName + " " + lastName;
cout << fullName;
return 0;
} // Ali Mohammad
int main (){
Ex.
cout<<"4+7";
return 0;
} // 4+7
Append
you can also concatenate strings with the append() function
45 int main () {
string firstName = "Naser ";
string lastName = "Younes";
string fullName = [Link](lastName);
cout << fullName;
return 0;
} // Naser Younes
Adding Numbers and Strings
The + operator uses for both addition and concatenation.
46 //If you add two numbers, the result will be a number:
#include <iostream>
using namespace std;
int main () {
int x = 10;
int y = 20;
int z = x + y;
cout << z;
return 0;
} // 30
47 //if you add two strings, the result will be a string concatenation:
#include <iostream>
#include <string>
using namespace std;
int main () {
string x = "10";
string y = "20";
string z = x + y;
cout << z;
return 0;
} // 1020
48 //If you try to add a number to a string, an error occurs:
#include <iostream>
#include <string>
using namespace std;
int main () {
string x = "10";
int y = 20;
string z = x + y;
cout<<z;
return 0;
} // error