what does a computer program look like: a general overview
TRANSCRIPT
What does a computer program look like: a general overview
Computer programming: solving a problem with a computer
• Goal of computer programming:
• We write a computer program to solve a problem or perform a task
Computer programming: solving a problem with a computer (cont.)
• In order to solve a problem, we need:
The basic fact of life is that without information, we cannot complete the task !!!
• An algorithm (detailed steps that solve the problem)
• Information !!!
Computer programming: solving a problem with a computer (cont.)
• Example: compute the sum of 2 numbers
Without the value of the 2 numbers, we cannot perform the addition operation !!!
• Algorithm: perform an addition
• Information: the value of the 2 numbers
Computer program = an algorithm + (lots of) information
• A computer program consists of:
• computer instructions that tells a computer precisely what to do to accomplish the job
• (Lots of) information to guide the algorithm to do the job
Computer program = an algorithm + (lots of) information (cont.)
• This fact is expressed in the title of a classical Computer Science book:
Computer program = an algorithm + (lots of) information (cont.)
• The title of this classical Computer Science book reflects the fact that to solve a problem (write a program) with a computer, you need:
• An algorithm, and
• Information (that is stored in data structure)
Statements: expressing the steps in an algorithm
• Statements
• Statement = an instruction written in a (High level) Programming Language
• A statement tells a computer in very precise terms what exactly it must do
Statements: expressing the steps in an algorithm (cont.)
• Example of statements
• Store the value 26 in the memory cell named "A"
• Add the number A to the number B
• If the number A ≥ 0 then do this otherwise do that
Variable: storing the information
• Variables
• A variable = a identifiable memory cell stores information that is necessary to solve a problem
• A variable behaves like a piece of paper that is identified by a unique name where we can record (a small amount of) information
Example:
Variable: storing the information (cont.)
• Data structure:
• Example
• A data structure = a collection of variables that store related information
• One variable stores the name of a person
• Another variable stores the phone number of a same person
• These 2 variables together would form a data structure
Data types
• There are different types of data:
• Numerical data, such as: age, salary, and so on.
• Textual data (or alpha-numerical data) such as books, news paper articles, webpages, and so on.
• Etc.
Data types (cont.)
• A programming language always provides a number of built-in data types to get your started
Common built-in data types:
The built-in data types are called primitive data types
• Single precision floating point numbers
• Double precision floating point numbers
• Integer numbers
• A character type
• A logical data type
Data types (cont.)
• In addition, a modern programming language allows the programmer to define his/her own data types
These are called user-defined types.
Variable definition
• In modern programming languages, we must "define" every variable in the computer program
• Effect of a variable definition:
• Each variable definition must use a unique name to identify the variable.
• A variable definition will tell the computer to create the variable that will be identified by that (unique) name
This is like telling the computer to:
1. get a new sheet of paper and
2. tag the new sheet of paper with the name of the variable
Starting point of the execution of a computer program
• Every computer program written in a high level programming language has a starting point of execution
• Each programming language has its own convention of where a program will start execution
Starting point of the execution of a computer program (cont.)
• Example starting point in some popular programming languages:
Programming language Starting point of a program Java main()C/C++ main()Python First line in the program
Execution of a computer program
• How a computer program is executed:
• The execution will start at the statement at the starting point of the execution
(In Java, this will be the first statement in main())
• After a statement is executed, the next statement executed depends on the flow of control
Execution of a computer program
• Flow of control:
• Normal flow of control is:
• There are control statements in a programming language that can change the normal flow of control
• After executing a statement, the next statement that will be executed is the statement following the last executed statement
A taste of "flow of control" (program execution): the Euler algorithm in Java
• Recall the Euclid Algorithm:
We saw the execution of the Euclid algorithm using 2 pieces of paper: http://192.168.1.3/~cheung/teaching/web/170/Syllabus/01/alg.html#exec
As long as one of the number is not zero (0) do
{
if ( number on A ≥ number on B )
replace the number on A by the value (A - B) otherwise replace the number on B by the value (B - A)
}
The Greatest Common Divisor (GCD) = B
A taste of "flow of control": the Euler algorithm in Java (cont.)
• We have also seen a Java program that implements the Euclid algorithm:
public class Euclid
{
public static void main(String args[])
{
int A; // Memory cell named "A"
int B; // Memory cell named "B" // These memory cells are like the 2 pieces of paper // we used above. They can store and recall a value
A = 28; // Write "28" on the piece of paper named "A"
B = 36; // Write "36" on the piece of paper named "B" // ================================ // This is the Euclid Algorithm: // ================================
while ( A != 0 && B != 0 )
{
if ( A >= B )
A = A - B; // Replace the number on A by (A-B)
else B = B - A; // Replace the number on B by (B-A)
}
System.out.println("GCD = " + B);
}
}
A taste of "flow of control": the Euler algorithm in Java (cont.)
• When the Java program is run on a computer, the computer will be doing pretty much the same thing as the execution on 2 pieces of paper that we saw here: http://192.168.1.3/~cheung/teaching/web/170/Syllabus/01/alg.html#exec
A taste of "flow of control": the Euler algorithm in Java (cont.)
• I will illustrate how a computer executes the Euclid algorithm with a series of diagram:
• The program execution of a Java program starts at beginning of main(...)
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "int A;" tells the computer to create a new variable name A
This is like getting a new sheet of paper and tag it with the name A
• Result:
• The flow of control goes to the next line
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "int B;" tells the computer to create a new variable name B
This is like getting a new sheet of paper and tag it with the name B
• Result:
• The flow of control goes to the next line
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "A=28;" tells the computer to put the number 28 in the variable A
• This is like writing "28" on the sheet of paper tagged with the name A
• Result:
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "B=36;" tells the computer to put the number 36 in the variable B
This is like writing "36" on the sheet of paper tagged with the name B
• Result:
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "while (A≠0 && B≠0)" tells the computer only continue inside the statements inside {...} if the condition is true.
The symbol && means "and".
• Since 28 (A) ≠ 0 and 36 (B) ≠ 0, the condition is true and the execution continues inside {...}
• Result:
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "if (A≥B)" tells the computer to: continue inside the first {...} if the condition is true and continue inside the second {...} if the condition is false
Since 28 (A) < 36 (B), the condition is false and the execution continues inside the second {...} (after the word else)
• Result:
A taste of "flow of control": the Euler algorithm in Java (cont.)
• Executing the line "B=B−A;" tells the computer to put the difference (36−28) in the variable B
This is like writing the difference (36−28) on the sheet of paper tagged with the name B
• Result:
A taste of "flow of control": the Euler algorithm in Java (cont.)
• When the execution reaches the line "}", the flow of control will go back to up to the while line and execute the steps all over again !!!
In other words:
A taste of "flow of control": the Euler algorithm in Java (cont.)
• The computer executing the Euclid algorithm written in the Java programming language is doing the exact same steps as what a human would do using 2 pieces of paper.
• A computer program is nothing more than a precise sequence of steps (that a human would use) to solve a problem that is expressed in a programming language
Conclusion:
A programming language is a language
• It is important to realize that a programming language is like any of the human languages
• Important facts about programming languages:
1. A programming language has syntax rules that govern the correctness of the "sentences"
2. The compiler (translator) is extremely inflexible on the syntax rules:
• If the compiler finds any syntax error in a program, it will not compile (translate) the entire program
A programming language is a language (cont.)
• Important advice:
(Don't worry, the syntax rules are very simple; still, you need to remember them well !)
• Remember the syntax rules of the programming language carefully