Lecture 1
Lecture 1
Lecture 1
Tc :Hadeel Al Garadi
Lecture 1
C++ Syntax
Let's break up the following code to understand it better:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Example explained
Line 1: #include <iostream> is a header file library that lets us
work with input and output objects, such as cout (used in line 5).
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.
Don't worry if you don't understand how #include
<iostream> and using namespace std works. Just think of it as
something that (almost) always appears in your program.
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Omitting Namespace
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:
Example
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Syntax
type variable = value;
Where type is one of C++ types (such as int), and variable is the
name of the variable (such as x or myName). The equal sign is used
to assign values to the variable.
Example
Create a variable called myNum of type int and assign it the
value 15:
You can also declare a variable without assigning the value, and assign
the value later:
Example
int myNum;
myNum = 15;
cout << myNum;
Example
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
Constants
However, you can add the const keyword if you don't want others (or
yourself) to override existing values (this will declare the variable as
"constant", which means unchangeable and read-only):
Example
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'
Other Types
A demonstration of other data types:
Example
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
The cout object is used together with the << operator to display
variables.
Example
int myAge = 35;
cout << "I am " << myAge << " years old.";
Example
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
Example
int x = 5, y = 6, z = 50;
cout << x + y + z;
C++ Identifiers
All C++ variables must be identified with unique names.
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
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; // Display the input value
In this example, the user needs to input two numbers, and then we
print the sum:
Example
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
C++ Data Types
As explained in the Variables chapter, a variable in C++ must be a
specified data type:
Example
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 myText = "Hello"; // String
int
int myNum = 1000;
cout << myNum;
float
float myNum = 5.75;
cout << myNum;
double
double myNum = 19.99;
cout << myNum;
The precision of a floating point value indicates how many digits the
value can have after the decimal point. The precision of float is only
six or seven decimal digits, while double variables have a precision of
about 15 digits. Therefore it is safer to use double for most calculations.
Scientific Numbers
A floating point number can also be a scientific number with an "e" to
indicate the power of 10:
Example
float f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;
Booleans
A boolean data type is declared with the bool keyword and can only
take the values true or false. When the value is
returned, true = 1 and false = 0.
Example
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
Boolean values are mostly used for conditional testing, which you will
learn more about in a later chapter.
Characters
The char data type is used to store a single character. The character
must be surrounded by single quotes, like 'A' or 'c':
Example
char myGrade = 'B';
cout << myGrade;
Example
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
Strings
The string type is used to store a sequence of characters (text). This is
not a built-in type, but it behaves like one in its most basic usage.
String values must be surrounded by double quotes:
Example
string greeting = "Hello";
cout << greeting;
To use strings, you must include an additional header file in the source
code, the <string> library:
Example
// Include the string library
#include <string>