Basic Concepts in
Programming
Fundamentals of Programming
Bùi Duy Đăng
bddang@[Link]
Plan for today
• Review
• Basic concepts in programming
• Exercises
FundamentalsOfProgram 2
Review
Applications More abstract
Programming languages
Software
Operating systems
Architecture
Computer
components Hardware
Circuits
Transitors
Less abstract
FundamentalsOfProgram 3
Compiler and Intepreter
• Pros and Cons
• Which languages support compilers?
• Which languages support intepreter?
FundamentalsOfProgram 4
Bugs/debugs
• Syntax and semantic errors
• When does compile-time error occur?
• When does run-time error occur?
FundamentalsOfProgram 5
Programming language
• A language to communicate with a computer
• Low-level programming language
• Machine code, Assembly
• High-level programming language
• Procedural: Fortran, Pascal, C
• Functional: Lisp, Haskell, F#
• Object oriented: C++, Java, C#
• Others
• Scripting languages: JavaScript, Python
• AI prompts: ask AI to generate source code
FundamentalsOfProgram 6
Algorithm
A finite sequence of steps to solve a problem
• Given a year, check whether it is a leap year or not?
• Given a number, check whether it is a prime or not?
FundamentalsOfProgram 7
Basic concepts in
programming
FundamentalsOfProgram 8
Programming environment
• A platform of tools supporting programming tasks
• Source code editor
• Edit text, syntax highlight, code completion
• Compiler
• Translate source code to machine code
• Terminal
• Command-line tool to run program and shell commands
• Debugger
• Run program step-by-step
• Monitor computer memory ??
FundamentalsOfProgram 9
Programming enviroment
• IDE – Integrated Development Environment
• “All-in-one” tools
• User friendly, configuration-free, add-in features
• Cons:
• Heavy memory and storage usage
• Limit configuration skill
• Require license
• Track user data
FundamentalsOfProgram 10
Programming enviroment
Tools Windows MacOS Linux
Editor Notepad++ TextMate Vim, Emacs
VS Code (*) VS Code (*) VS Code (*)
Sublime Text (*) Sublime Text (*) Sublime Text (*)
Compiler MSVC (*)
GCC (+) Clang GCC
Clang (+) GCC Clang
Debugger MSVC (*) LLDB GDB
GDB GDB LLDB
IDE Visual Studio (*) Xcode (*)
Code::Blocks Code::Blocks Code::Blocks
CodeLite CodeLite CodeLite
(*): Proprietary software and track user data.
(+): Run on Linux simulated environment like MinGW64, WSL.
FundamentalsOfProgram 11
Comments
FundamentalsOfProgram 12
Comments
• Comments are code lines to refer comments of programmers
• All languages support
• It is very necessary for programmers
• Review
• Others read
• Reading a piece of comments instead of reading whole code
• For example: 1, 2
FundamentalsOfProgram 13
Comments in C++
• Line comments
// this is a comment until the end line
• Block comments
/* this is the first comment in a block
this comment will be stop until seeing */
#if 0
#endif
FundamentalsOfProgram 14
Variables
FundamentalsOfProgram 15
Variables
• Variables are names/labels to label data in memory
• Used to store values
• Variable names are case-sensitive,
• Capital and lowercase letters are different (e.g., ”hello” and “Hello”)
• Hungarian, Camel, snake style
• Contains only alphabetic letters, underscores (“_”) or numbers
• MUST not start with a number
• Cannot be keywords in computer (e.g., if, for)
Meaningful variable names are essential
FundamentalsOfProgram 16
Constant Variables
• Constant variables are names/labels to label data in memory
• Used to store unchangable values
• In C++,
• Names of constant variables are often written in uppercase
(e.g., PI, MAX, CURRENTYEAR)
FundamentalsOfProgram 17
Data types
FundamentalsOfProgram 18
Data Types
• Computer stores data digitally in memories
• A classification of data which tells the compiler or interpreter how the
programmer intends to use the data
• Different data types:
• Simple, built-in data types (e.g., integer, double)
• User-defined (e.g., struct, class)
• Others (e.g., pointer in C++)
FundamentalsOfProgram 19
Boolean
• Applied to ”things” that have two values
• ON/OFF, open/close, true/false
• It contains true or false
• In C++, denoted as bool, in a indirected way
• false: value 0
• true: not value 0
• For example:
• 0 (false), 1 (true), -1 (true), 2 (true)
• 1 < 2 (false), 3 > 1 (true)
FundamentalsOfProgram 20
Character
• Indicate each character value in computer
• In C/C++,
Name: char
Range: 256 characters in ASCII Table
Integers can be used to represent
• Unicode, UTF-8
FundamentalsOfProgram 21
FundamentalsOfProgram 22
Integer
• Indicate negative, zero, and non-negative numbers in mathematics
in C++
• Signed integers
• Unsigned integers
FundamentalsOfProgram 23
Real Numbers
• Contains integers, rational, and irrational numbers in mathematics
• In C++, using floating-point to store
• float: 4 bytes, precision up to 7 digits
• double: 8 bytes, precision up to 15 digits
FundamentalsOfProgram 24
Constant Variables
• Memory location whose content cannot change during execution.
• Syntax
const DataType VariableName
• For example
const double PI = 3.14;
const int NO_OF_STUDENTS = 60;
FundamentalsOfProgram 25
Arithmetic Operators
• Pre-defined in programming languages for data types
• In C++,
Which operaters used
• Addition +
for integer and/or real
• Subtraction – numbers?
• Multiplication *
• Division /
• Modulus %
• Unary, binary, and ternary operators
FundamentalsOfProgram 26
Unary Operators
• Only have one operand in a expression
• In C++,
• ++ (increment, one value), -- (decrement, one value)
• Put before operand
E.g., ++x or --x: do increment/decrement first.
• Put after operand
E.g., x++ or x--: do increment/decrement after.
x = 10; y = x++; // y = 10 and x = 11
x = 10; y = ++x; // x = 11 and y = 11
FundamentalsOfProgram 27
Bitwise Operators
• Used for bit of operands (integers)
• In C++,
• and &, or |, xor ^, not ~
• shift right >>, shift left <<
• Compound assignment operators: &=, |=, ^=, ~=, >>=, <<=
FundamentalsOfProgram 28
Relational Operators
• Check the relationship between two operands
• Return true or false
• In C++,
• ==, !=, >, <, >=, <=
• For example:
s1 = (1 == 2); s3 = (1 > 2); s5 = (1 < 2);
s2 = (1 != 2); s4 = (1 >= 2); s6 = (1 <= 2);
FundamentalsOfProgram 29
Logical Operators
• Check whether an expression is true or false
• In C++,
and &&,
or ||,
not !
• For example
s1 = (1 > 2) && (3 > 4);
s2 = (1 > 2) || (3 > 4);
s3 = !(1 > 2);
FundamentalsOfProgram 30
C++ Operator Precedence
Source: [Link]
FundamentalsOfProgram 31
C++ Operator Precedence
FundamentalsOfProgram 32
Variable Declaration
DataType Name;
For example,
int a;
double x, y, z;
FundamentalsOfProgram 33
Assignments operators
Variable = expression;
Expressions can be values, variables, or complex expressions
• In C++,
int x = 0; // initialization, equivalent to int x(0);
float y; // the value of y?
y = 1.5;
y = x;
y = (3 + x) * 4;
FundamentalsOfProgram 34
Statements
• Instructs the computer to do a specific task
• In C++, compiler ignores space (blank, tab, enter) in a statement
• Ends with a semicolon (;)
• For example
a=292;
a = 292;
a
=
292;
FundamentalsOfProgram 35
Statements
• Single statement
• Compound statement: a group of statements enclosed in curly
brace { and }
For example:
a = 2912; // Single statement
{ // Compound statement
a = 2912; b = 1706;
}
FundamentalsOfProgram 36
Output statement
• In C++,
#include <iostream> // declare iostream library
// using namespace std; // comment
int main(){
// std::cout << expression;
// this is an output statement example
std::cout << “hello world!”;
return 0;
} FundamentalsOfProgram 37
Output stament
• Escape sequence
cout << “hello world!\n” // Equivalent to
cout << “hello world!” << endl; // endline
FundamentalsOfProgram 38
Input statement
• In C++,
std::cin >> variable ;
#include <iostream>
// using namespace std;
...
int yearBorn; // input the year born,
int currentYear = 2023;
std::cin >> yearBorn; // suppose a valid year
std::cout << “your age is “ << currentYear –
yearBorn << endl; FundamentalsOfProgram 39
Some useful functions
• Math functions
• #include <cmath> // math library
• pow(x, y)
• acos(x), asin(x), atan(x), cos(x), sin(x), ...
• exp(x), log(x), log10(x)
• sqrt(x)
• ceil(x), floor(x)
• abs(x), fabs(x)
FundamentalsOfProgram 40
Exercises
1) Print out some characters in ASCII Table in console and list
some characters you interested.
2) Print out some information and escapse sequence
E.g.,: Student_name\n
3) Given a number year, calculate its age
4) Convert speed value to pace value and vise versa
5) Convert metter value to mile value and vise versa
FundamentalsOfProgram 41
Exercises
6) Given a year, check if is a leap year.
7) Given scores of three courses and its coefficients. Calculate the
average score.
8) Given an everage score, display rank of this score, such as
excellent (9-10), very good (8-9), good (7-8), fair (5-7), poor (<5)
9) Given three everage scores of students, display ranking of three
students. For example: Input: 8 9 10. Output: 1 2 3
10) Given a number n, calculate 1 + 2 + … + n
FundamentalsOfProgram 42