Lecture+1
Lecture+1
Programming
DR/ Mohamed Abdo Kassem
1
1. Machine Languages, Assembly
Languages, and High-level Languages
• Machine language
• Only language computer directly understands
• Defined by hardware design
• Machine-dependent
• Generally consist of strings of numbers
• Ultimately 0s and 1s
• Instruct computers to perform elementary operations
• One at a time
• Cumbersome for humans
• Example:
+1300042774 0100 1101 0111 1101 0001 0100 0001 0110
+1400593419 0101 0011 0111 1011 0101 1100 0000 1011
+1200274027 0100 0111 1000 1010 1011 1010 0110 1011
2
2. Assembly language
• English-like abbreviations representing
elementary computer operations
• Clearer to humans
• Incomprehensible to computers
• Translator programs (assemblers)
• Convert to machine language
• Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
3
3. High-level languages
e.g. C, BASIC, FORTRAN, Java, Pascal, Ada, Perl
• Use common mathematical notations
• Single statements accomplish substantial tasks
• Assembly language requires many instructions to accomplish
simple tasks
• Translator programs (compilers)
• Convert to machine language
• Interpreter programs
• Directly execute high-level language programs
4
Basics of a Typical C++ Environment
5
Basics of a Typical C++ Environment
Program created in the editor
Edit Editor foo.cpp and stored on disk.
(vi, emacs)
Preprocessor strips out comments,
Preprocessor expands macros
Compile (and Link)
(CC , g++)
Compiler
foo.o Compiler creates object code and
stores it on disk (.o file)
C++ Library Linker foo Linker links the object code with the
(a.out) libraries, stores in a.out on disk.
Loader
Loader puts program
Run
..
in memory (RAM)
(load and execute) ..
..
./foo, ./a.out
8
A Simple Program:
Printing a Line of Text
• Standard output stream object
• std::cout
• “Connected” to screen
• <<
• Stream insertion operator
• Value to right (right operand) inserted into output stream
• Namespace
• std:: specifies using name that belongs to “namespace”
std
• std:: removed through use of using statements
• Escape characters
• \
• Indicates “special” character output
9
A Simple Program:
Printing a Line of Text
Escape Sequence Description
10
11
Another Simple Program:
Adding Two Integers
• Variables
• Location in memory where value can be stored
• Common data types
• int - integer numbers
• char - characters
• double - floating point numbers
• Declare variables with name and data type before use
int integer1;
int integer2;
int sum;
• Can declare several variables of same type in one declaration
• Comma-separated list
int integer1, integer2, sum;
12
Another Simple Program:
Adding Two Integers
• Variables
• Variable names
• Valid identifier
• Series of characters (letters, digits, underscores)
• Cannot begin with digit
• Case sensitive
13
Another Simple Program:
Adding Two Integers
• Input stream object
• >> (stream extraction operator)
• Used with std::cin
• Waits for user to input value, then press Enter (Return) key
• Stores value in variable to right of operator
• Converts value to variable data type
• = (assignment operator)
• Assigns value to variable
• Binary operator (two operands)
• Example:
sum = variable1 + variable2;
14
// Addition program.
#include <iostream>
int main()
{
int integer1;
int integer2;
int sum;
std::cout << "Enter first integer\n";
std::cin >> integer1;
std::cout << "Enter second integer\n";
std::cin >> integer2;
sum = integer1 + integer2;
std::cout << "Sum is " << sum << std::endl;
return 0;
}
15
// Addition program.
#include <iostream>
// function main begins program execution
int main()
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
int sum; // variable in which sum will be stored
std::cout << "Enter first integer\n"; // prompt
std::cin >> integer1; // read an integer
std::cout << "Enter second integer\n"; // prompt
std::cin >> integer2; // read an integer
sum = integer1 + integer2; // assign result to sum
std::cout << "Sum is " << sum << std::endl; // print sum
return 0; // indicate that program ended successfully
} // end function main
16
Enter first integer
45
Enter second integer
72
Sum is 117
17
Memory Concepts
• Variable names
• Correspond to actual locations in computer's memory
• Every variable has name, type, size and value
• When new value placed into variable,
overwrites previous value
• Reading variables from memory is nondestructive
18
Memory Concepts integer1 12
int integer1, integer2, sum; integer2 0
– declare three variables
sum -3
– starting values are arbitrary
integer1 45
std::cin >> integer1;
– Assume user entered 45 integer2 0
sum -3
integer1 45
std::cin >> integer2;
integer2 72
– Assume user entered 72
sum -3
* Multiplication
/ Division
Integer division truncates remainder
7 / 5 evaluates to 1
%Modulus operator returns remainder
7 % 5 evaluates to 2
20
Arithmetic
• Rules of operator precedence
• Operators in parentheses evaluated first
• Nested/embedded parentheses
• Operators in innermost pair first
• Multiplication, division, modulus applied next
• Operators applied from left to right
• Addition,
Operator(s) subtraction applied
Operation(s) last (precedence)
Order of evaluation
21
Decision Making: Equality and
Relational Operators
• if structure
• Make decision based on truth or falsity of condition
• If condition met, body executed
• Else, body not executed
• Equality and relational operators
• Equality operators
• Same level of precedence
• Relational operators
• Same level of precedence
• Associate left to right
22
Decision Making: Equality and
Relational Operators
Sta nd a rd a lg e b ra ic C++ e q ua lity Exa m p le Me a ning o f
e q ua lity o p e ra to r o r o r re la tio na l o f C++ C++ c o nd itio n
re la tio na l o p e ra to r o p e ra to r c o nd itio n
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
23
Decision Making: Equality and
Relational Operators
• using statements
• Eliminate use of std:: prefix
• Write cout instead of std::cout
24
#include <iostream>
using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
int main()
{
int num1; // first number to be read from user
int num2; // second number to be read from user
cout << "Enter two integers, and I will tell you\n"
<< "the relationships they satisfy: ";
cin >> num1 >> num2; // read two integers
if ( num1 == num2 )
cout << num1 << " is equal to " << num2 << endl;
if ( num1 != num2 )
cout << num1 << " is not equal to " << num2 << endl;
if ( num1 < num2 )
cout << num1 << " is less than " << num2 << endl;
if ( num1 > num2 )
cout << num1 << " is greater than " << num2 << endl;
if ( num1 <= num2 )
cout << num1 << " is less than or equal to "
<< num2 << endl;
if ( num1 >= num2 )
cout << num1 << " is greater than or equal to "
<< num2 << endl;
return 0; // indicate that program ended successfully
} // end function main
25
Enter two integers, and I will tell you
the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
26
LOCAL and Global VARIABLES
27
28
29
30
31
32
33
34
35
36
38
39
40
41