Professional Documents
Culture Documents
Lesson 02 - 02. C++ Language Basics
Lesson 02 - 02. C++ Language Basics
C++
Language Basics
Oritented Object Programming C++: Chapter 02
Previous lessons
1. Introduction C++
C is (almost) a subset of C++
Learn the basics of many other languages by
yourself
Learn by doing
Do homeworks and project in group
Use Code::blocks IDE
I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control
#include <iostream>
using namespace std;
/*
My first program in C++
*/
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 4 Duy Tan University
I. Structure of a program
#include <iostream>
#include substitutes
using namespace std; whole file
/*
My first program in C++
*/
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 5 Duy Tan University
I. Structure of a program
#include <iostream>
using namespace std;
/*
My first program in C++
*/ Without this line, “cout”
has to be “std::cout”
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 6 Duy Tan University
I. Structure of a program
#include <iostream>
using namespace std;
/* Comments
#include <iostream>
using namespace std;
/*
My first program in C++ main function
called when the
*/ program is run
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 8 Duy Tan University
I. Structure of a program
#include <iostream>
using namespace std;
/*
My first program in C++ main returns an
int, 0 mean “no
*/ error”
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 9 Duy Tan University
I. Structure of a program
#include <iostream>
using namespace std;
/*
My first program in C++
*/ Print “Hello world!” to
the console
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 10 Duy Tan University
II. Primitive Types and Literals
Integer types
Floating Point types
Character type
Boolean type
float
Almost always 32 bits, literals: 12.3F, -11.3F
Range: ±1.17549E-38 to ±3.40282E+38
Precision: approximately 7 decimal digits
double
Almost always 64 bits, literals: 13.2, -13.2, 3E-12
Range: ±2.22507E-308 to ±1.79769E+308
Precision: approximately 16 decimal digits
14 Duy Tan University
Character types
char
Almost always 8 bits
Character literals:
'a', 'B', '1', '#'
'\n', '\t', '\b', '\?', '\"', '\\'
'\142'
'\x61'
Others: char16_t, char32_t, wchar_t
bool
Almost always 8 bits
Boolean literals:
true
false
We can use true as 1 and false as 0
Arithmetic operators:
+ - * / % ++ --
Assignment operators:
= += -= *= /= %=
Relational and Comparison operators:
== != > < >= <=
Logical operators:
! && ||
Conditional ternary operator:
condition ? result1 : result2
20 Duy Tan University
V. Operators (cont.)
I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control
I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control
Example:
#include <iostream> Output
using namespace std; PI: 3.14159
#define PI 3.14159 a: 10
int main() {
const int a = 10;
cout << "PI: " << PI << endl;
cout << "a: " << a << endl;
return 0;
}
Selection statements:
if…else
switch-case
Loops:
while and do-while
for
Syntax:
if (<condition>) <statement>
[else if (<condition>) <statement>]
[else <statement>]
<condition> can be anything return a value
If the returned value is false or 0, the condition is
false
Other values are make the condition mean true
Examples: Output
int a = 5, b = 20; a <= b
if (a > b) 0 is false and 3 is true
cout << "a > b" << endl;
else
cout << "a <= b" << endl;
if (0)
cout << "0 is true" << endl;
else if (3)
cout << "0 is false and 3 is true" << endl;
Syntax:
switch (<expression>) {
case <value 1>: <statements>
break;
case <value 2>: <statements>
case <value 3>: <statements>
...
[default: <statements>]
}
<expression> must be an integer
Examples:
int x = 2; Output
switch (x) { x is 2 or 3
case 1:
cout << "x is 1" << endl;
break;
case 2:
case 3:
cout << "x is 2 or 3" << endl;
break;
default:
cout << "x is not found";
break;
38 Duy Tan University
do and while loops
Syntax:
while (<condition>) <statements>
• Check the <condition> before execution of the
<statements> in each iteration
do <statements> while (<condition>);
• Check the <condition> after execution of
the <statements> in each iteration
We can use continue and break in the loops
Examples:
int x = 0; Output
while (x < 3) { 0
cout << x << endl; 1
x++; 2
0
} 1
x = 0; 2
do {
cout << x << endl;
x++;
} while (x < 3);
Syntax:
for (<initialization>; <condition>; <increase>)
<statements>
Condition is checked before each iteration
Each expression can be ignored when unused
When <condition> ignored means it is true
We can use continue and break in the loops
Examples:
Output
int sum = 0;
for (int i = 0; i <= 5; i++) { 1
3
if (i % 2 == 0)
5
continue; sum: 9
cout << i << endl;
sum = sum + i;
}
cout << "sum: " << sum << endl;
I. Structure of a program
II. Primitive Types and Literals
III. Variables
IV. Basic Input/Output
V. Operators
VI. Constants
VII.Statements and Flow Control
6. Enter a charater:
If the character is lowercase, print the uppercase
If the character is uppercase, print the lowsercase
7. Enter an integer (>=0), print the integer by binary.
8. Enter an integer, print the number by binary in 16
bits.
9. Write inputMatrix(…) and displayMatrix(…) to
input and display an matrix 2D.
10. Write rotateMatrix(…) function to clockwise rotate
the matrix elements.