PL1 Lecture 1.3 Basic Concepts | PDF | Computer Program | Programming
0% found this document useful (0 votes)
18 views

PL1 Lecture 1.3 Basic Concepts

Uploaded by

mishibaislam666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

PL1 Lecture 1.3 Basic Concepts

Uploaded by

mishibaislam666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Basic programming

Course Title: Programming language 1

Dept. of Computer Science


Faculty of Science and Technology

Lecture No: 02 Week No: 1 Semester: Summer 23-24


Lecturer: Shahnaj Parvin; sparvin@aiub.edu
Lecture Outline

 List of topics,
 Pseudocode
 Variables
 Loops
 Decision
 Function
 debugging
3

How do you write a program?


• Decide what steps are needed to complete the task

• Write the steps in pseudocode (written in English) or as a


flowchart (graphic symbols)
• Translate into the programming language

• Try out the program and “debug” it (fix if necessary)


4

What is pseudocode?

• List of steps written in English

• Like the instructions for a recipe

• Must be in the right sequence


o Imagine saying “bake the cake” and then “mix it up”
5

Sample Pseudocode & Flowchart


• Task: add two numbers • Flowchart:
Start
• Pseudocode:
Read num1,
1. Start num2
2. Get two numbers
3. Add them
Sum= num1+num2
4. Print the answer
5. End
Print Sum

End
6

Another Sample: Calculating Age

 Pseudocode: Flowchart Start

Read birthYear
1. Start
2. Get year born Age= currentYear - birthYear
3. Calculate age
4. Print age Print Age

5. If age > 50 print OLD Yes


Print OLD Age>50?
6. End
No

End
7

Elements of a Program
• All programming languages have certain features in common.
For example:
Variables
Commands/Syntax (the way commands are structured)
Loops
Decisions
Functions

• Each programming language has a different set of rules about


these features.
8

Variables
• Variables are part of almost every program.

• A variable is a “place to put data” and is usually represented by


a letter or a word.
• Variable names cannot contain spaces.

• Some programming languages have very specific limits on


variable names.
9

Variables (cntd.)
• Here are some examples of data types:

Format
Data Type Size (bytes) Range
Specifier

int 4 -2,147,483,648 to 2,147,483,647 %d


long int 4 %ld
char 1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf

• For string data (sequence of character) type use %s


11

Variables (cntd.)
• Variables may be classified as global or local.

• A global variable is one that can be shared by all parts of a


program, including any functions or sub-programs.
• A local variable is one that is used only within a certain part of
the program, for example, only in one function or sub-program.
12

Loops

• A loop is a repetition of all or part of the commands in a


program.
• A loop often has a counter (a variable) and continues to repeat a
specified number of times.
• A loop may also continue until a certain condition is met (e.g.,
until the end of a file or until a number reaches a set limit).
13

Decisions
• A program often needs to decide whether something is true or false to see which way to
continue.

• Programs often use IF (or IF THEN or IF THEN ELSE) statements to show a decision.

• An IF statement always has a condition to check, often a comparison between a variable and a
number.

• The IF statement also must specify what to do if the condition/comparison is true.

• These instructions (for “true”) may come after the word THEN, or they may simply be listed.

• In an IF THEN statement, when the condition is false, the program simply ignores the THEN
commands and continues to the next line.

• In an IF THEN ELSE statement, commands are given for both the true and false conditions.
14

Functions
• In most programming languages, small sub-programs are used to perform
some of the tasks.
• These may be called functions, subroutines, handlers, or other such
terms.
• Functions often have names (e.g., getName or CALCTAX).

• A function generally gets information from the main program, performs


some task, and returns information back to the program.
• Functions follow the same rules of syntax, etc. as the main program.

• JavaScript code is primarily made of a series of functions.


15

Debugging

• To “debug” means to try a program, then fix any mistakes.

• Virtually no program works the first time you run it. There are just too
many places to make errors.

• When you are debugging a program, look for spelling and punctuation
errors.

• Fix one error at a time, then try the program again.


16

Self-Check 1
 A computer program is…
 A series of instructions to accomplish something
 A TV show
 Written in Egyptian hieroglyphics
 Can be written any way you want to
17

Self-Check 2
 To “compile” a program means to…
 Translate it into English
 Translate it into binary code
 Pile up the punch cards used for the program
 Run the program as it was written
18

Self-Check 3
 Pseudocode is…
 The program as it is written in a programming
language
 The results of a program that makes secret codes
 The logic of a program written in English
 The logic of a program shown in a chart
19

Self-Check 4
 The flowchart symbol to perform a calculation is…
20

Self-Check 5
 The flowchart symbol to show a decision is…
21

Self-Check 6
 Look at the flowchart section below. If the
variable X is 5, what will print (K or 1st)?

N Y
Print “K” X > 5? Print “1st”

K will be printed. The answer to the question “Is X greater than 5?”
is NO, since X is equal to (not greater than) 5.
22

Self-Check 7
 A function in a program is…
 Something from trigonometry, like COSINE
 A sub-program, usually performing one task
 A way to check the accuracy of a program (a “function
check”)
23

Self-Check 8
 A variable in a program is…
 A letter or word that represents a place to store data
 A decision made within a program
 A small sub-program used to find errors
24

Class Task
Try to write pseudo code and create a flowchart for a program that:

• Calculates the average of three grades and prints the average.

• The word GOOD should be printed only if the average is more than 80.

You might also like