Beruflich Dokumente
Kultur Dokumente
C++ BASICS
September 2015
An example
#include <iostream.h>
using namespace std;
int main()
{
cout << \Hello world \n";
return 0;
}
Statement is isolated by
;
Variables
A variable is a storage location paired with an associated symbolic
name (an identifier), which contains some known or unknown
quantity of information referred to as a value.
To define a variable:
Data type variable name;
or
Data type variable name = value;
Example:
int a;
int a, b = 10;
Variables
I
I
I
Case sensitive
Only contains: a-z, A-Z, 0-9 and
Example:
int total_student;
char _student_name;
Variables
I
I
I
Case sensitive
Only contains: a-z, A-Z, 0-9 and
Example:
int total_student;
char _student_name;
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
I
I
It must be in .
Special charater:
n,
tab,
,...
bool: true/false
Variables
Type
char
short
int
long
float
double
long double
Low
-128
-32768
-2147483648
-2147483648
3.4 1038
1.7 10308
3.4 104932
High
127
32767
2147483647
2147483647
3.4 1038
1.7 10308
3.4 104932
Bytes
1
2
4
4
4
4
10
Constant variables
#define PI 3.14
Constant variables
#define PI 3.14
Constant variables
#define PI 3.14
Constant variables
#define PI 3.14
Constant variables
#define PI 3.14
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cout
cout is an object defined by stdio in C++, it is responsible to
display the output and corresponds to standard output stream.
Operation << directs the data to object cout. Example
I
int i = 8;
Homework: learn about setw - How to use it? How does it work?
Using cin
int i;
cin i;
Using cin
int i;
cin i;
Using cin
int i;
cin i;
Using cin
int i;
cin i;
Using cin
int i;
cin i;
Using cin
int i;
cin i;
Using cin
int i;
cin i;
Operators
Mathematical operator
Relational operator
Logical operator
Operators
Mathematical operator
Relational operator
Logical operator
Operators
Mathematical operator
Relational operator
Logical operator
Operators
Mathematical operator
Relational operator
Logical operator
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Mathematical Operator
I
Addition +
Subtraction -
Multiple *
Division /
Reminder
int a = 7/3; // a = ?
float b = 1/3; //b = 0.333
int b = 10 % a; // b = 0
Example
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Relational Operator
Only release one in two value: TRUE or FALSE
I
Greater than:
Lower than:
Equal:
Different:
int a
float
(a ==
(a >=
Example
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) // false
b) //true
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
Logical operator
It combines several logical values and release one in two value:
TRUE or FALSE
I
AND:
OR:
NOT:
= 7/3; // a = ?
b = 1/3; //b = 0.333
b) || (a != b) // true
b) && (a != b) // false
= (a < b) ? 5 : 7; // c = 5? c = 7?
FOR loop
The init step is executed first, and only once. This step allows
you to declare and initialize any loop control variables. You
are not required to put a statement here, as long as a
semicolon appears.
Next, the condition is evaluated. If it is true, the body of the
loop is executed. If it is false, the body of the loop does not
execute and flow of control jumps to the next statement just
after the for loop.
After the body of the for loop executes, the flow of control
jumps back up to the increment statement. This statement
allows you to update any loop control variables. This
statement can be left blank, as long as a semicolon appears
after the condition.
The condition is now evaluated again. If it is true, the loop
executes and the process repeats itself (body of loop, then
Example
int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;
WHILE loop
A while loop statement repeatedly executes a target statement as
long as a given condition is true.
while(condition)
{
statement(s);
}
I
Example
int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;
DO loop
A do...while loop is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time.
do
{
statement(s);
}while(condition);
I
Example
int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;
IF.. THEN
An if statement consists of a boolean expression followed by one or
more statements.
if(boolean_expression)
{
// statement(s) will execute if the boolean expression i
}
else
{
// statement(s) will execute if the boolean expression is
}
I
IF..THEN: Example
int i;
for (i = 1; i <= 15; i++)
cout << i*i << " ";
cout << endl;
break, continue
break:
continue:
Function: An example
Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum between the two:
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function: An example
Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum between the two:
#include <iostream>
using namespace std;
int max(int num1, int num2);// function declaration
int main ()
{
int a = 100, b = 200, ret;
// local variable declarat
ret = max(a, b);
// calling a function to get max valu
cout << "Max value is : " << ret << endl;
return 0;
}
int max(int num1, int num2)
{
Function: Arguments
This method copies the actual value of an argument into the formal par