CSE 021 - Lec03 - Programming Basics - 09.11.21
CSE 021 - Lec03 - Programming Basics - 09.11.21
Programming
CSE 021 Fall 2021
1
Lecture Topics
• Programs and Programming Languages
• How a Program Works
• Program Translation
• Compilers and Interpreters
• Machine Language, Assembly, and High-Level Languages
• Programming Process
• Syntax and Semantics
• What is a Program Made of?
• Program Debugging
• A Simple C Program: Printing a Line of Text
2
Programs and Programming
Languages
• What is a Program?
• A set of instructions a computer follows in order to perform a task.
3
How a Program Works
• CPU designed to perform simple operations on pieces of
data
• Examples: reading data, adding, subtracting, multiplying, and
dividing numbers
• Understands instructions written in machine language and included
in its instruction set
• Each brand of CPU has its own instruction set
• To carry out meaningful calculation, CPU must perform
many operations
4
How a Program Works (Cont.)
• Program must be copied from secondary memory to RAM
each time CPU executes it.
5
How a Program Works (Cont.)
6
How a Program Works (Cont.)
7
Program Translation
• A central processing unit (CPU) is designed to interpret and
execute a specific set of instructions represented in binary
form (i.e., 1s and 0s) called machine code.
• Only programs in machine code can be executed by a CPU.
8
Program Translation (Cont.)
• Writing programs at this “low level” is tedious and error-prone.
• Therefore, most programs are written in a “high-level”
programming language such as Python, C++, Java.
• Since the instructions of such programs are not in machine code
that a CPU can execute, a translator program must be used.
9
Compilers and Interpreters
• Compiler: software that translates high-level language program
into separate machine language program
• Machine language program can be executed at any time
• Used by C++ language
• Interpreter: software that translates and executes instructions in
high-level language program
• Used by Python language
• Interprets one instruction at a time
• No separate machine language program
10
Compiler
11
Interpreter
12
Interpreters
13
From Machine Language to
Assembly Language
• Impractical for people to write in machine language
• Assembly language: uses short words (mnemonics) for
instructions instead of binary numbers
• Easier for programmers to work with
mnemonic
• Assembler: translates assembly language to machine
language for execution by CPU
14
Assembly Language
15
High-Level Languages
• Low-level language: close in nature to machine language
• Example: Assembly language
16
Programming Process
1. Clearly define what the program is to do.
2. Visualize the program running on the computer.
3. Design a flow-chart
4. Check the flowchart for logical errors.
5. Write a pseudocode version of the code.
6. Check the pseudocode for errors.
7. Write the actual program and compile it.
8. Correct any errors found during compilation.
9. Repeat step 7 & 8 as many times as necessary.
17
Programming Process
10.Run the program with test data for input.
11.Correct any errors found while running the program.
12.Repeat step 10 & 11 as many times as necessary.
13.Validate the results of the program.
18
Designing a Program
• Programs must be designed before they are written
• Program development cycle:
• Design the program
• Write the code
• Correct syntax errors
• Test the program
• Correct logic errors
19
Designing a Program
• Design is the most important part of the program
development cycle
• Understand the task that the program is to perform
• Work with customer to get a sense what the program is supposed
to do
• Ask questions about program details
• Create one or more software requirements
• Determine the steps that must be taken to perform the task
• Break down required task into a series of steps
• Create an algorithm, listing logical steps that must be taken
20
Integrated Development
Environment (IDE)
• An IDE (Integrated Development Environment) is a bundled
set of software tools for program development. This
typically includes:
• an editor for creating and modifying programs
• a translator for executing programs, and
• a program debugger for taking control of the execution of a
program to aid in finding program errors
21
Syntax, Semantics and Program
Translation
• Programming languages (called “artificial languages”) are
languages just as “natural languages” such as English and
Mandarin (Chinese).
• Syntax and semantics are important concepts that apply to
all languages.
22
Syntax
• The syntax of a language is a set of characters and the
acceptable sequences (arrangements) of those characters.
• set of rules to be followed when writing program
• English, for example, includes the letters of the alphabet,
punctuation, and properly spelled words and properly
punctuated sentences.
• The following is a syntactically correct sentence in English:
• “Hello there, how are you?”
• The following, however, is not syntactically correct:
• “Hello there, hao are you?”
• The sequence of letters “hao” is not a word in the English language
23
Semantics
• The semantics of a language is the meaning associated with
each syntactically correct sequence of characters.
• Consider the following sentence:
• “Colorless green ideas sleep furiously.”
• This sentence is syntactically correct, but has no meaning.
Thus, it is semantically incorrect.
• Every language has its own syntax and semantics.
24
What is a Program Made of?
• There are certain elements that are common to all
programming languages?
• Statements
• Keywords
• Operators
• Punctuations
• Comments
• Programmer-defined symbols (variable/method/class names)
25
Keywords, Operators, and
Punctuations
• Statement: individual instruction used in high-level language
• Keywords: predefined words used to write program in high-level
language
• Each keyword has specific meaning
• Example in C: if, else, int, while
• Operators: perform operations on data
• Example: math operators to perform arithmetic
• Punctuations: sometimes called separators basically are used to
implement the grammatical and structure of a syntax
• Example: periods, commas, semicolons
26
Comments
• Comments: notes of explanation within a program
• Ignored by interpreters and compilers
• Intended for a person reading the program’s code
• Single line vs. multi-line comments
• In C and C++: Begin with a // characters or /*…*/
27
Variables
• Variable: name that represents a value stored in the
computer memory
• Used to access and manipulate data stored in memory
• A variable references the value it represents
• Assignment statement: used to create a variable and make
it reference data
• General format is variable = expression
• Example: age = 29
• Assignment operator: the equal sign (=)
• Variable receiving value must be on left side
28
Variable Naming Rules
• Rules for naming variables :
• Variable name cannot be a one of the languages keywords
• Variable name cannot contain spaces
• First character must be a letter or an underscore
• After first character may use letters, digits, or underscores
• Variable names are case sensitive
• Variable name should reflect its use
29
Program Debugging:
Syntax Errors vs. Semantic Errors
• Program debugging is the process of finding and correcting errors
(“bugs”) in a computer program. Programming errors are
inevitable during program development.
30
Program Debugging:
Syntax Errors vs. Semantic Errors
• In contrast, semantic errors (generally called logic errors ) are errors in program
logic.
• Such errors cannot be automatically detected, since translators cannot understand
the intent of a given computation.
• Therefore, programs are not terminated when containing logic errors, but give
incorrect results.
• For example, if a program computed the average of three numbers as follows,
(num1 + num2 + num3) / 2.0
• translator would have no means of determining that the divisor should be 3 and not 2.
• Computers do not understand what a program is meant to do, they only follow the
instructions given. It is up to the programmer to detect such errors.
• Program debugging is not a trivial task, and constitutes much of the time of program
development.
31
The First Actual Computer “Bug”
In 1947, engineers working on the Mark II
computer at Harvard University found a moth
stuck in one of the components, causing the
machine to malfunction. They taped the insect in
their logbook and labeled it “first actual case of
bug being found.”
The term program “bug” (and “debugging”) has become a standard part of
the language of computer programmers. The log book, complete with the
attached bug, is on display at the Smithsonian Institution in Washington, D.C.
32
A Simple C Program: Printing a Line of Text
1. // A first program in C.
2. #include <stdio.h>
3.
4. // function main begins program execution
5. int main(void) {
6. printf("Welcome to C!\n");
7. } // end function main Welcome to C!
33
Comments
• Begin with //
• Insert comments to document programs and improve
program readability
• Comments do not cause the computer to perform actions
• Help other people read and understand your program
• Can also use /*…*/ multi-line comments
• Everything between /* and */ is a comment
34
A Simple C Program: Printing a Line of Text
1. // A first program in C.
2. #include <stdio.h>
3.
4. // function main begins program execution
5. int main(void) {
6. printf("Welcome to C!\n");
7. } // end function main Welcome to C!
35
#include Preprocessor Directive
• #include <stdio.h>
• C preprocessor directive
• Preprocessor handles lines beginning with # before
compilation
• This directive includes the contents of the standard
input/output header (<stdio.h>)
• Contains information the compiler uses to ensure that you
correctly use standard input/output library functions such as
printf
36
A Simple C Program: Printing a Line of Text
1. // A first program in C.
2. #include <stdio.h>
3.
4. // function main begins program execution
5. int main(void) {
6. printf("Welcome to C!\n");
7. } // end function main Welcome to C!
37
Blank Lines and White Space
• Blank lines, space characters and tab characters make
programs easier to read
• Together, these are known as white space
• Generally ignored by the compiler
38
A Simple C Program: Printing a Line of Text
1. // A first program in C.
2. #include <stdio.h>
3.
4. // function main begins program execution
5. int main(void) {
6. printf("Welcome to C!\n");
7. } // end function main Welcome to C!
39
The Main Function
• Begins execution of every C program
• Parentheses after main indicate that main is a function
• C programs consist of functions, one of which must be main
• Precede every function by a comment stating its purpose
• Functions can return information
• The keyword int to the left of main indicates that main
“returns” an integer (whole number) value
40
The Main Function (Cont.)
• Functions can receive information
• void in parentheses means main does not receive any information
• A left brace, {, begins each function’s body
• A corresponding right brace, }, ends each function’s body
• A program terminates upon reaching main’s closing right
brace
• The braces form a block
41
A Simple C Program: Printing a Line of Text
1. // A first program in C.
2. #include <stdio.h>
3.
4. // function main begins program execution
5. int main(void) {
6. printf("Welcome to C!\n");
7. } // end function main Welcome to C!
42
Output Statement
• printf("Welcome to C!\n");
• “f” in printf stands for “formatted”
• Performs an action—displays the string in the quotes
• A string is also called a character string, a message or a literal
• The entire line is called a statement
• Every statement ends with a semicolon statement
terminator “;”
• Characters usually print as they appear between
• Notice the characters \n were not displayed.
43
Escape Sequences
• In a string, backslash (\) is an escape character
• Compiler combines a backslash with the next character to
form an escape sequence
• \n means newline
• When printf encounters a newline in a string, it positions
the output cursor to the beginning of the next line
44
Escape Sequences (Cont.)
Escape
Description
Sequence
\n Moves the cursor to the beginning of the next line.
\t Moves the cursor to the next horizontal tab stop.
Produces a sound or visible alert without changing the current
\a
cursor position.
Because the backslash has special meaning in a string, \\ is
\\
required to insert a backslash character in a string.
Because strings are enclosed in double quotes, \” is required
\”
to insert a double-quote character in a string.
45
Linker and Executables
• When compiling a printf statement, the compiler merely
provides space in the object program for a “call” to the
function
• The compiler does not know where the library functions
are—the linker does
• When the linker runs, it locates the library functions and
inserts the proper calls to these functions in the object
program
• Now the object program is complete and ready to execute
• The linked program is called an executable
46
Indentation Conventions
• Indent the entire body of each function one level of
indentation within the braces that define the function’s
body
• Emphasizes a program’s functional structure and helps make
them easier to read
• Set an indentation convention and uniformly apply that
convention
• Style guides often recommend using spaces rather than tabs
47
End of Lecture!
Thanks for your Attention!
48