Chapter 1
Chapter 1
Introduction to programing
What is a programming?
• Computer
• an electronic device that accepts data,
• performs computations, and makes logical decisions according to
instructions that have been given to it
• then produces meaningful information in a form that is useful to the
user
• Computer programs
• Sequences of instructions expressed in specific programming language:
• the instructions that tells the computer what to do
• Is the process of creating computer software using a programming
Language.
• Computer programs (also know as source code)
• Is often written by professionals known as Computer Programmers
• example of programing languages are: Java, C, C++, python, PHP, Perl,
Ruby
• Computer programming
• is the process of writing the, testing it for errors (debugging)
INGREDIENTS:
1 cup butter
2 cups (370 grams)
granulated sugar
4 large eggs
3 cups (384 grams) cake INSTRUCTIONS:
flour • Step 1: Preheat oven to 350°. Grease and flour cake pan
• Step 2: Cream sugar and butter together in a mixing bowl. Add eggs, one at a
1 tablespoon (12 grams) time, beating briefly after each addition. Mix in vanilla.
baking powder
1 cup milk • Step 3: Combine flour and baking powder in a separate bowl. Add to the wet
ingredients and mix well. Add milk and stir until smooth.
2 teaspoons vanilla • Step 4: Pour the mixture into prepared pan.
• Step 5: Bake for 28 to 30 minutes.
• Step 6: Remove from the oven and cool completely.
• Step 7: Enjoy!
1.2. Reasons to study programming
a) MACHINE LANGUAGE:
• this is the only language computer actually understands (computer’s native
language)
• Machine dependent
• Cannot be run on another type of computer without significant alterations (different
computers understand different sequences)
• Also called Binary language(sequence of zeroes and ones )
• hard for humans to understand: 01010001…
… cont
b) ASSEMBLY LANGUAGE:
• Arising from the machine language difficulties,
• Writing programs in machine language is slow and tedious for
most programmers
• Here the sequence of 0’s and 1’s are replaced with symbols
• The language provided an opportunity to the programmers
to use English like words that were called Mnemonics
• Symbolic language(assembly) is machine dependent just
like machine language
• Assembler
• translate Assembly language to machine language
• still hard for humans to understand:
• ADD d0,d2
… cont
C) HIGH LEVEL LANGUAGE:
• High level language introduces the use of “almost English” language in writing
computer programs
• It is much easier to write a program than the low level language
• A program written in a high-level language is just like giving instructions to a
person in daily life.
• Examples are FORTRAN, COBOL, PASCAL, C, java, python etc.
• BASIC - Beginners All Purpose Symbolic Instruction Code
• COBOL - Common Business Oriented Language, widely used for business
data processing task
• FORTRAN - Formula Translation
• PASCAL - Named after French mathematician Blaise Pascal
• closer to English easier to read and understand:
• Python
• print("Hello, world!")
• The above computer program instructs the computer to print "Hello, World!" on the
computer screen.
Interpreting vs. Compiling Programs
• Each type of computer only “understands” its own
machine language (zeroes and ones)
• Thus we must translate from High-level language to
machine language
• a team of experts programs a translator, called a “compiler”
which translates entirety of a High-level language program to
an executable file in computer’s native machine language.
• An alternative to compiling your program is to
interpret your program
• each line of your program is translated into machine language
and immediately executed
… cont
Output unambiguity
6 1
Input Characteristics of Flexibility
5 Algorithm
2
Language
Finiteness
4 3 independent
Guidelines for Developing an Algorithm:
An algorithm will be enclosed by START (or BEGIN) and STOP (or
END)
To accept data from the user, generally used statements are INPUT,
READ, GET or OBTAIN
To display the result or any message, generally used statements are
PRINT, DISPLAY, or WRITE
Generally, COMPUTE or CALCULATE is used while describing
mathematical expressions, and based on the situation relevant
operators can be used.
Algorithm Examples
1. Algorithm to compute square of a number
• Step 1: Start
• Step 2: Read the Number
• Step 3: Compute square = Number * Number
• Step 4: Print square
• Step 5: Stop
int main() {
int sum = 0;
int i;
// Loop from 1 to 50 and add each
number to the sum
for (i = 1; i <= 50; i++) {
sum += i;
}
return 0;
} 50. Inside the loop, the current value
This program uses a for loop to iterate from 1 to
of i is added to the sum variable. After the loop completes, the final sum is printed to
the console.
Pseudocode
• Derived from pseudo and code is a compact and informal
high-level description of a computer algorithm that uses
the structural conventions of programming.
• It is easy for humans to read
• No single standard/ syntax for writing pseudocode
• There's no single standard for writing pseudocode, as long as it
clearly conveys the program's logic.
• It is not an executable program
• It's not a real programming language
• Writing it will save you time later during the construction
& testing phase of a program's development.
Rules used to write pseudocode
1. Write only one statement per line
Each statement in your pseudocode should express just one action for the
computer.
2. Capitalize initial keyword
We will use just a few keywords: Common keywords like "if,"
"else," "while," "for," "function," and "return" are used to represent
control flow and program structures
• INPUT/READ – indicates a user will be inputting something.
• OUTPUT/WRITE – indicates that an output will appear on the screen.
• WHILE – a loop (iteration that has a condition at the beginning)
• FOR – a counting loop (iteration)
• IF – THEN – ELSE – a decision (selection) in which a choice is made any
instructions that occur inside a selection or iteration are usually indented.
3. Indent to show hierarchy
• indentation refers to the practice of adding spaces or tabs at the beginning
of lines of code to visually represent the structure and hierarchy of the
program
• We will use a particular indentation pattern in each of the design
structures:
• Consistent indentation improves readability and visually shows the
program's structure.
Sequence: Statements that execute one after the other are typically
kept at the same level of indentation
Selection(if-else): indent the statements that fall inside the selection
structure, but not the keywords that form the selection
Looping(for, while): ident the statements that fall inside the loop,
but not the keywords that form the loop
Pseudocode examples
#include <iostream>
Pseudocode Example
using namespace std;
: Issue for driver license
BEGIN int main() {
OUTPUT "Enter your age" int age;