0% found this document useful (0 votes)
2 views51 pages

Lesson 02 - 02. C++ Language Basics

This document is a C++ programming course outline covering language basics, including program structure, primitive types, variables, input/output, operators, constants, and flow control statements. It provides examples and explanations for each topic, along with exercises for practice. The course is taught by Hieu Vo Dinh at Duy Tan University and is designed for a duration of 120 minutes.

Uploaded by

chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views51 pages

Lesson 02 - 02. C++ Language Basics

This document is a C++ programming course outline covering language basics, including program structure, primitive types, variables, input/output, operators, constants, and flow control statements. It provides examples and explanations for each topic, along with exercises for practice. The course is taught by Hieu Vo Dinh at Duy Tan University and is designed for a duration of 120 minutes.

Uploaded by

chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

02.

C++
Language Basics
Oritented Object Programming C++: Chapter 02

 Time: 120 Minutes


 Instructor: Hieu, Vo Dinh
 Email: [email protected]
 Phone: 0941 891 998
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

Duy Tan University


2
Contents

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

Duy Tan University


3
I. Structure of a program

#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”
int main() has to be “std::cout”

{
// 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

My first program in C++


*/
int main()
{
// Print something to the console
cout << "Hello world!";
return 0;
} 7 Duy Tan University
I. Structure of a program

#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
int main() the console

{
// 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

Duy Tan University


11
Integer types
Type names Short name Size Range (min size)
signed char char >=8 bits -128 to 127
unsigned char unsigned >=8 bits 0 to 255
char
signed short int short >=16 bits -32,768 to 32,767
unsigned short int unsigned >=16 bits 0 to 65,535
short
signed int int >=16 bits -32,768 to 32,767
unsigned int unsigned int >=16 bits 0 to 65,535
signed long int long >=32 bits -2^31 to 2^31 – 1
unsigned long int unsigned >=32 bits 0 to 2^32 - 1
long
signed long long long long >=64 bits -2^63 to 2^63 – 1
int
unsigned long
Size: char unsigned
≤ short ≤ int >=64 bits ≤
≤ long 0 to 2^64 -long
long 1
long int long long
Duy Tan University
12
Integer Literals (constants)

 int: (decimal, binary, octal, hex)


12, - 12 , 0b10, -0b10, 037, -037, 0xff ,-0xff
 unsigned int:
12U, 037U, 0b10U, 0xffU
 long:
12L, - 12L, 037L, -037L, 0xffL ,-0xffL
 unsigned long:
12UL, 037UL, 0xffUL
 Warning: unsigned arithmetic can be dangerous

Duy Tan University


13
Floating Point types

 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
Duy Tan University
14
Character types

 char
Almost always 8 bits
 Character literals:
'a', 'B', '1', '#'
'\n', '\t', '\b', '\?', '\"', '\\'
'\142'
'\x61'
 Others: char16_t, char32_t, wchar_t

Duy Tan University


15
Boolean type

 bool
Almost always 8 bits
 Boolean literals:
true
false
 We can use true as 1 and false as 0

Duy Tan University


16
III. Variables

 Likes C and Java:


type variableName;
type variableName = literal;
type variableName =
declaredVariable;
 Examples:
 int a;// Declare a to be an integer
 a = 99;
 int b = 10;// Declare and initialize b
to 10
 int c = b;// Declare and initialize c
from b
Duy Tan University
17
IV. Basic Input/Output

 Use standard output cout to print values on screen:


 cout << value;
 cout << value1 << value2 << ... <<
valueN;
 Examples:
Output
int a = 10;
10
double f = 1.1; 1.1
1+1=2
cout << a;// print 10 on screen
cout << '\n'; // new line
cout << f << endl;// 1.1 and new line
cout << "1 + 1 = " << (1 + 1) <<
endl; 18 Duy Tan University
IV. Basic Input/Output (cont.)

 Use standard input cin to get values from keyboard:


 cin >> variable;
 cin >> variable1 >> ... >> variableN;
 Examples:
int i; float f; char ch; Output
cin >> i;//gets an integer i 3from
stdin 3
cout << i << endl;//prints i 2.1
and new
p
line f: 2.1
cin >> f >> ch; ch: p
cout << "f: " << f << endl;
cout << "ch: " << ch
19 << endl; Duy Tan University
V. Operators

 Arithmetic operators:
+ - * / % ++ --
 Assignment operators:
= += -= *= /= %=
 Relational and Comparison operators:
== != > < >= <=
 Logical operators:
! && ||
 Conditional ternary operator:
 condition ? result1 : result2
Duy Tan University
20
V. Operators (cont.)

 Explicit type casting operator:


int i;
float f = 3.14;
i = (int)f;
i = int(f);
 sizeof operator: sizeof(value)
 int a = 12; cout << sizeof(int) <<
endl; // 4 bytes
 cout << sizeof(a) << endl; // 4 bytes
 cout << sizeof('a') << endl; // 1 byte
 cout << sizeof(12.0) << endl; // 8
bytes Duy Tan University
21
Contents

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

Duy Tan University


22
Practices by Your-Self in 5 minutes

Duy Tan University


23
Example 01

 What is the output?


#include <iostream>
using namespace std;
Output
int main() {
2
int i; 16
i = 01 + 01;
cout << i << endl;
i = 010 + 010;
cout << i << endl;
return 0;
} 24 Duy Tan University
Example 02

 What is the output?


#include <iostream>
using namespace std;
Output
int main()
1
{ 6
int a = 1, b = 5;
cout << a++ << endl;
cout << ++b << endl;
return 0;
}
Duy Tan University
25
Example 03

 What is the output?


#include <iostream>
using namespace std;
Output
int main()
12
{ 2
float f1 = 12, f2 = 2.3;
int a = f1, b = f2;
cout << a << endl;
cout << b << endl;
return 0;
} 26 Duy Tan University
Example 04

 What is the output?


#include <iostream>
using namespace std;
Output
int main() {
1.23457e+08
float f1 = 123456789; 123456789
int a = 123456789, b = f1;
123456792
cout << f1 << endl;
cout << a << endl;
cout << b << endl;
return 0;
} 27 Duy Tan University
Example 05

 What is the output?


#include <iostream>
using namespace std;
Output
int main()
-13035
{ 123456789
short a = 123456789;
int b = 123456789;
cout << a << endl;
cout << b << endl;
return 0;
} 28 Duy Tan University
Y2K bug

 A bug, that may have caused problems when dealing


with dates beyond December 31, 1999

Duy Tan University


29
Example 06

 What is the output?


#include <iostream>
using namespace std; Output
int main() { A
char ch1 = 'A', ch2 = 97; a
67
int a = ch1 + 2;
cout << ch1 << endl;
cout << ch2 << endl;
cout << a << endl;
return 0;
} 30 Duy Tan University
Contents

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

Duy Tan University


31
VI. Constants

 There are three ways to define constants:


Literals
• 1, -2, 1.5, 'a', "ABC", true, false
Preprocessor definitions (#define)
• #define PI 3.14159
• PI = 3.2;
Typed constant expressions
• const int a = 10;
• a = 5;
• const int b;

Duy Tan University


32
VI. Constants (cont.)

 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;
}

Duy Tan University


33
VII. Statements and flow control

 Selection statements:
if…else
switch-case
 Loops:
while and do-while
for

Duy Tan University


34
if statements

 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

Duy Tan University


35
if statements (cont.)

 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;

Duy Tan University


36
switch statements

 Syntax:
switch (<expression>) {
case <value 1>: <statements>
break;
case <value 2>: <statements>
case <value 3>: <statements>
...
[default: <statements>]
}
 <expression> must be an integer

Duy Tan University


37
switch statements (cont.)

 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

Duy Tan University


39
do and while loops (cont.)

 Examples:
int x = 0; Output
while (x < 3) { 0
cout << x << endl; 1
2
x++;
0
} 1
x = 0; 2
do {
cout << x << endl;
x++;
} while (x < 3);

Duy Tan University


40
for loops

 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

Duy Tan University


41
for loops (cont.)

 Examples:
Output
int sum = 0;
1
for (int i = 0; i <= 5; i++) {
3
if (i % 2 == 0) 5
continue; sum: 9
cout << i << endl;
sum = sum + i;
}
cout << "sum: " << sum << endl;

Duy Tan University


42
Example 01

 What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#1
if ((a == 2) && (++b > 5)) a: 2
cout << "#1" << endl; b: 6
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
Duy Tan University
43
Example 02

 What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#2
if ((a == 1) && (++b > 5)) a: 2
cout << "#1" << endl; b: 5
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
Duy Tan University
44
Example 03

 What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#1
if ((a == 2) || (++b > 5)) a: 2
cout << "#1" << endl; b: 5
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
Duy Tan University
45
Example 04

 What is the output?


#include <iostream>
using namespace std;
int main() { Output
int a = 2, b = 5;
#1
if ((a == 1) || (++b > 5)) a: 2
cout << "#1" << endl; b: 6
else
cout << "#2" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
Duy Tan University
46
Example 05

 What is the output?


#include <iostream>
using namespace std;
int k = 10; Output
int main() {
#1: 20
if (true) #2: 10
{
int k = 20;
cout << "#1: " << k << endl;
}
cout << "#2: " << k << endl;
return 0;
}
Duy Tan University
47
Summary

 The basic C++ program contains: header, main


function, comments, staments.
 Primitive types: char, unsigned char, short, unsigned
short, int, unsigned int, long, unsigned long, long
long, float, double.
 cin to input, cout to output
 if, if-else, else if…, switch…case
 for, while, do..while
 break, continues

Duy Tan University


48
Summary

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

Duy Tan University


49
Problems

1. Enter a month and a year, print number of days in


the month.
2. Write isPrime(…) function to check a number is a
prime or not.
3. Write input(…) function to enter n numbers to an
arrray, printLargestNumbers(…) to print k (k<=n)
largest number in the arrays.
4. Write add(…), insert(…) and remove(…) functions
to add, insert and remove an element of an array in a
position.
5. Write sort(…) function to sort elements in an array.
Duy Tan University
50
Problems (cont.)

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.

Duy Tan University


51

You might also like