0% found this document useful (0 votes)
37 views59 pages

Chapter 1 - Introduction To Programming

The document outlines the course structure for Computer Programming I and II at Admas University, covering topics such as programming paradigms, problem-solving techniques, and the basics of C++. It details various programming concepts including procedural, structured, and object-oriented programming, as well as the stages of program development and algorithm design. Additionally, it discusses the importance of learning programming for logical thinking and application development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views59 pages

Chapter 1 - Introduction To Programming

The document outlines the course structure for Computer Programming I and II at Admas University, covering topics such as programming paradigms, problem-solving techniques, and the basics of C++. It details various programming concepts including procedural, structured, and object-oriented programming, as well as the stages of program development and algorithm design. Additionally, it discusses the importance of learning programming for logical thinking and application development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Admas University

College of Computing and Informatics


Computer Science Department

Computer Programming I

Chapter 1: Introduction programming


Course Outline

 Chapter 1 : Introduction to Programming

 Introduction to computer programming

 Types of Computer Programming

 Procedural, Structured and object-oriented programming paradigms

 Problem solving, algorithm designing, and modeling logic

 Mechanics of creating a program and basics of C++ environment

 History of C and C++

Compiled by Daniel k ([Link].) 22 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Course Outline

 Chapter 2 : C++ Basics


 Structure of C++ Program; C++ IDE;

 Keywords, Identifiers, Inputs, Outputs, Comments

 Parts of a program; Data Types, Variables, and Constants

 Operators

 Assignment Operators;

 Compound Assignment Operators;

 Arithmetic Operators;

 Relational Operators;
 Increment and Decrement Operators
Compiled by Daniel k ([Link].) 33 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Course Outline

 Chapter 3 : Control Statements

 If statements: If…else, nested if

 Switch Statements: multiple cases, break, and default

 Looping: for, while, do, break, and continue; nested loops

Compiled by Daniel k ([Link].) 44 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Course Outline

 Chapter 4 : Array and String Manipulation

 Array Definition

 Array referencing

 One dimensional and multidimensional arrays

 Strings: Definition

 Accessing strings

Compiled by Daniel k ([Link].) 55 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Chapter 1:Outline

 Introduction to computer programming

 Programming paradigms

Procedural, Structured and object-oriented

 Types of computer programming

 Problem solving, algorithm designing, and modeling logic

 Software development life cycle

 History of C and C++

Compiled by Daniel k ([Link].) 66 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
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 : Sets of instructions that control a

computer’s processing of data. The instructions that tells the

computer what to do

Compiled by Daniel k ([Link].) 77 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Cont’d

 Computer programming

 is the process of writing, testing, debugging / troubleshooting, and

maintaining the source code of computer programs

 Computer programmer

 Computer programs (also know as source code) is often written by

professionals known as Computer Programmers

 Source code is written in one of programming languages

Compiled by Daniel k ([Link].) 88 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Why do we need to learn computer programming?

 To know how to the computer perform a task.

 To develop logical thinking

 To solve complex problem simple manner.

 To develop application

 To get financial income

 To improve logical and reasoning skill

Compiled by Daniel k ([Link].) 99 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) Chapter 1 : Introduction to Programming
Many Aspects of Programming

 Programming is problem solving

 Always trying to make computer do something useful — i.e., finding

an optimal travel route

 Programming is controlling

 Computer does exactly what you tell it to

 Programming is teaching

 Computer can only “learn” to do new things if you tell it how

Compiled by Daniel k ([Link].) 10 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 10 Chapter 1 : Introduction to Programming
Many Aspects of Programming

 Programming is creative

 Must find a good solution out of many possibilities

 Programming is modelling

 Describe salient (relevant) properties and behaviours of a system of

components (objects)

 Programming is abstraction

 Identify important features without getting lost in detail

Compiled by Daniel k ([Link].) 11 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 11 Chapter 1 : Introduction to Programming
What’s a Program

 Model of complex system

 Model: simplified representation of salient features of something,

either tangible or abstract

 System: collection of components that work closely together

 Sequences of instructions expressed in specific programming language:

 Syntax: grammatical rules for forming instructions

 Semantics: meaning/interpretation of instructions

Compiled by Daniel k ([Link].) 12 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 12 Chapter 1 : Introduction to Programming
What’s a Program

 Instructions written (programmed/coded) by programmer

 Coded in a specific programming language

 Programs cannot be ambiguous

 All instructions together are called source code

 Executed by computer by carrying out individual instructions

Compiled by Daniel k ([Link].) 13 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 13 Chapter 1 : Introduction to Programming
What is programming language

 Artificial language that can be used to control the behavior of a computer


 Defined by
 Syntactic - describes possible combinations of symbols that form a
syntactically correct program
 Semantic - The meaning given to a combination of symbols
 Every programming languages have syntax and semantics
 Computers do exactly what they are told to do
 Available programming languages come in a variety of forms and types

Compiled by Daniel k ([Link].) 14 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 14 Chapter 1 : Introduction to Programming
What is programming language

 Programming languages can be divided in to three major categories:

I. low-level

II. Assembly

III. high-level languages

Compiled by Daniel k ([Link].) 15 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 15 Chapter 1 : Introduction to Programming
Machine language

 The most elementary computer language


 The first type of computer language invented, was machine language
 Also called Binary language
 Collection of binary digits or bits that the computer reads and interprets
 Machine language was machine dependent
 Different computers understand different sequences.
 Machine is short for computing machine (i.e., computer)
 Computer’s native language, sequence of zeroes and ones (binary)
 Different computers understand different sequences
 Hard for humans to understand: Example 01010001…

Compiled by Daniel k ([Link].) 16 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 16 Chapter 1 : Introduction to Programming
Assembly language

 Mnemonics for machine language


 instructions differ from machine to machine
 instructions translation also differ from machine to machine
 Assembly language program must be translated into machine language
 Machine language programming was simply too slow programmers
 Machine language programming was tedious for most programmers
 Low level: each instruction is minimal
 Still hard for humans to understand: Example : ADD d0,d2
 Assembly language programs are translated into machine language by a
program called an assembler.

Compiled by Daniel k ([Link].) 17 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 17 Chapter 1 : Introduction to Programming
High-level languages

 Designed to be easy for human beings to write and read programs


 Much easier to write a program than the low level language.
 Program written in high level is just like gibing instruction to person
 Closer to English easier to read and understand
 Low-level languages are closer to the language used by a computer,
 High-level languages are close to human languages.
 Example : FORTRAN, Pascal, BASIC, C, C++, Java, etc.
 High level: each instruction composed of many low-level instructions
 hypotenuse = [Link](opposite * opposite + adjacent * adjacent);

Compiled by Daniel k ([Link].) 18 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 18 Chapter 1 : Introduction to Programming
Programming paradigm

 Some program development approaches conceptually organize the


program around the code:
 Around what is happening
 known as Process oriented approach/ method
 Characterizes the program as serious linear steps.
 Code acting on data
 Other programming development conceptually organize the program
around data:
 Who is being affected
 Designed to manage increasing complexity.
 Ask not what your code does to your data structure
 But what your data structure can do for you
Compiled by Daniel k ([Link].) 19 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 19 Chapter 1 : Introduction to Programming
Programming paradigm

 Programming paradigm is a fundamental style to programming that


dictates how problems are structured and how solutions are expressed
in code
 A programming paradigm refers to the way or approach to classifying
programming languages based on their features, patterns, coding
styles, frameworks, etc., to solve a particular problem.
 A programming paradigm is a fundamental style of programming
 Unstructured Programming
 Procedural programming .
 Structured Programming
 Object Oriented Programming
Compiled by Daniel k ([Link].) 20 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 20 Chapter 1 : Introduction to Programming
Unstructured Programming

 Definition:

 The earliest style of programming

 Code is written as one continuous block

 Use goto statements to control flow.

 Characteristics

 No clear structure.

 Difficult to read and maintain.

 Common in early languages like BASIC and assembly.

 Example: Jumping around code using labels and goto.

Compiled by Daniel k ([Link].) 21 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 21 Chapter 1 : Introduction to Programming
Procedural programming

• Definition:

• A style based on the concept of procedure calls (functions or routines)

• Characteristics:

• Code is organized into procedures/functions.

• Emphasizes a step-by-step approach.

• Uses variables, loops, and conditionals.

• Common languages: C, Pascal.

• Example: Writing a function for sorting, then calling it multiple times with
different data.

Compiled by Daniel k ([Link].) 22 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 22 Chapter 1 : Introduction to Programming
Structured Programming

 Definition:
 A subset of procedural programming

 Enforces a logical structure and eliminates the use of goto.

 Characteristics:

 Uses control structures like if-else, for, while, and switch.

 Easier to read, debug, and maintain.

 Encourages top-down design ad code Reusablity

 Languages: C, Python (to some extent)


 Example:
 Breaking a task into smaller, manageable blocks using loops and
conditionals
Compiled by Daniel k ([Link].) 23 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 23 Chapter 1 : Introduction to Programming
Object-Oriented Programming (OOP)

 Definition:
 Based on the concept of "objects" —

 Instances of classes that encapsulate data and behavior.


 Characteristics:
 Concepts like encapsulation, inheritance, polymorphism, and abstraction

 Helps in modeling real-world entities.

 Data and operations are grouped together

 Common languages: Java, C++, Python, C#.


 Example:
 Defining a Car class with properties and methods,

 then creating multiple car objects.


Compiled by Daniel k ([Link].) 24 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 24 Chapter 1 : Introduction to Programming
Problem solving

 Problem solving, or breaking down the solution to a problem

into sequential steps is by far the most difficult part of computer

programming.

Q1. A farmer has some chickens and some Sheep. Together there

are 43 heads and 108 legs. How many chickens does the farmer

have? How many Sheep?

Compiled by Daniel k ([Link].) 25 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 25 Chapter 1 : Introduction to Programming
Problem solving Techniques

 Computer solves varieties of problems that can be expressed in a finite

number of steps

 A program solve a problem

 Solution to a problem should be reliable, maintainable, portable and efficient

 In computer programming two facts :

 Defining the problem and logical procedures to follow in solving it.

 Introducing the means by which programmers communicate those

procedures to the computer system so that it can be executed.

Compiled by Daniel k ([Link].) 26 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 26 Chapter 1 : Introduction to Programming
Control Structures

 To make a computer do anything, you have to write a program


 To write a program, tell the computer, step by step, exactly what to do
 The computer then "executes" the program
 Following each step mechanically, to accomplish the end goal.

 Programs could be written in terms of 3 structures:

 Sequence – which instruction should be done next?

 Selection – select between options

 Repetition – repeat an action while a given condition stays true

Compiled by Daniel k ([Link].) 27 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 27 Chapter 1 : Introduction to Programming
Stages of Program Development

 There are five stages of program development: namely


 Analysis
 Algorithm & Flow chart design
 Coding
 Implementation, and Maintenance.
Analysis
 Analysis stage requires a thorough understanding of the problem at hand
and analysis of the data and procedures needed to achieve the desired
result. In analysis stage, therefore, what we must do is work out what
must be done rather than how to do it.
 What input data are needed to the problem?
 What procedures needed to achieve the result?
 What outputs data are expected?
Compiled by Daniel k ([Link].) 28 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 28 Chapter 1 : Introduction to Programming
Algorithm

 Once the requirements of the program are defined, the next stage is
to design an algorithm to solve the problem
 An algorithm is a finite, step-by-step sequence of instructions that
describe how the data is to be processed to produce the desired
outputs
 The algorithm is implemented by a program
 An algorithm can be represented as
 Pseudocode
 Flowchart
 Structured chart
Compiled by Daniel k ([Link].) 29 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 29 Chapter 1 : Introduction to Programming
Pseudocode

 Artificial and informal language

 Helps programmers develop algorithms

 Save time later during construction & testing phase of a program's

Allows the designer to focus on the logic of the algorithm

 Without being distracted by details of language syntax

 Text-based" detail (algorithmic) design tool

 Pseudocode is not a rigorous notation,

 Since it is read by other people, not by the computer

Compiled by Daniel k ([Link].) 30 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 30 Chapter 1 : Introduction to Programming
Con't…

 Example:1
 Original Program Specification:
 Write a Pseudocode for a program that obtains two integer
numbers from the user. It will print out the sum of those numbers

1. Pseudocode
Step 1: START
Step 2: Read two numbers n1 and n2.
Step 3: Sum ← n1 + n2
Step 4: Print Sum
Step 5: STOP

Compiled by Daniel k ([Link].) 31 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 31 Chapter 1 : Introduction to Programming
Con't…

 Computation/Assignment  Conditional
 set the value of "variable" to if "condition"
:"arithmetic expression" or (subordinate) statement 1
"variable" equals etc ...
"expression" or else
"variable" = "expression" (subordinate) statement 2
 Input/Output etc ...
get "variable", "variable", ...  Iterative
display "variable", "variable", ... while "condition"
(subordinate) statement 1
etc ...

Compiled by Daniel k ([Link].) 32 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 32 Chapter 1 : Introduction to Programming
Con't…

 Example:2
 Original Program Specification:
 Develop an algorithm for a program that find largest number from
two numbers.
 Pseudocode
Step 1: START
Step 2: Read n1 and n2.
Step 3: If n1 > n2 go to step 5
Step 4: Big←n2,go to step 6
Step 5: Big ← n1
Step 6: Print Big
Step 7: STOP

Compiled by Daniel k ([Link].) 33 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 33 Chapter 1 : Introduction to Programming
Example

 Develop pseudocode match for a program to find largest number from


three numbers
 Pseudocode
Step 1: START
Step 2: Read n1, n2 and n3.
Step 3: If n1 > n2 and n1 > n3, go to step 6
Step 4: If n2 > n1 and n2 > n3, go to step 7
Step 5: Big ←n3, go to step 8
Step 6:Big ← n1 , go to step 8
Step 7:Big ← n2
Step 8: Print Big
Step 9: STOP.

Compiled by Daniel k ([Link].) 34 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 34 Chapter 1 : Introduction to Programming
Example

 Develop pseudocode match for a program find sum of N positive integer


numbers
 Pseudocode
Step 1: START
Step 2: Read N
Step 3: Sum ← 0,
Step 4: Count ← 0
Step 5: Read Num
Step 6: Sum ← Sum + Num
Step 7: count ← count +1
Step 8: If Count < N then goto step5
Step 9: Print Sum
Step 10: Stop
Compiled by Daniel k ([Link].) 35 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 35 Chapter 1 : Introduction to Programming
Flow Chart

 A graphic representation of an algorithm, often used in the design phase


of programming to work out the logical flow of a program

Compiled by Daniel k ([Link].) 36 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 36 Chapter 1 : Introduction to Programming
Example

A Flowchart to Add two numbers. Flowchart to find largest of two numbers.

Compiled by Daniel k ([Link].) 37 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 37 Chapter 1 : Introduction to Programming
Coding

 The flowchart is independent of programming language.

 At this stage we translate each steps described in the flowchart


(algorithm description) to an equivalent instruction of target
programming language

 Example if we want to write in FORTRAN program language, each


step will be described by an equivalent FORTRAN instruction
(Statement).

Compiled by Daniel k ([Link].) 38 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 38 Chapter 1 : Introduction to Programming
Implementation

 Once the program is written, the next step is to implement it.

 Program implementation involves three steps, namely, debugging (the


process of removing errors), testing (a check of correctness), and
documenting the program (to aid the maintenance of a program during
its life time).

 Every program contains bugs that can range from simple mistakes in the
language usage (syntax errors) up to complex flaws in the algorithm
(logic errors).

Compiled by Daniel k ([Link].) 39 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 39 Chapter 1 : Introduction to Programming
Maintenance

 There are many reasons why programs must be continually modified and
maintained, like changing conditions, new user needs, previously
undiscovered bugs (errors).

 Maintenance may involve all steps from requirements analysis to testing.

Compiled by Daniel k ([Link].) 40 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 40 Chapter 1 : Introduction to Programming
Translation and Execution

 Computer understands only the machine language.

 Program that is written low-level or high level language must be


translated to machine code so that the computer could process it.

 A translated machine code is called the object code or object program.

 Programs that translate a program called translators.

 There are three types of translators;

 Assembler,

 Interpreter, and

 Compiler.

Compiled by Daniel k ([Link].) 41 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 41 Chapter 1 : Introduction to Programming
Con't…

 Source code: High-level language instructions.


 Compiler: System software which translates high-level language
instructions into machine language or object code. The compiler
translates source code once and produces a complete machine
language program.
 Object code: Translated instructions ready for computer.
 An assembler is a software tool for translating low-level language
(assembly language) into machine language.

Compiled by Daniel k ([Link].) 42 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 42 Chapter 1 : Introduction to Programming
Basic Programming Tools

 The three basic building blocks, that is essential to develop a solution to a


problem are:

 Sequential executions instructions are performed one after the other.

 Branching operations where a decision is made

 Looping operations where a block of instructions is repeated.

 There are two types of loops.

 Conditional loop where we do not know how many times to repeat

 Counted loop where we do know how many times to repeat

Compiled by Daniel k ([Link].) 43 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 43 Chapter 1 : Introduction to Programming
Sequential executions

 Sequential instructions are executed one after the other.


Illustration: 1 - An algorithm and a flowchart to compute the area of a
rectangle whose length is ‘l’ and breadth is ‘b’.
Algorithm
Step 1: START
Step 2: Obtain (input) the length, call it l
Step 3: Obtain (input) the breadth, call it b
Step 4: Compute l*b, call it Area
Step 5: Display Area
Step 6: STOP

Compiled by Daniel k ([Link].) 44 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 44 Chapter 1 : Introduction to Programming
Con't…

 Illustration: 2- To allow for repeated calculation of the area of a


rectangle whose length is ‘l’ and breadth is ‘b’, rewrite the above
algorithm and flowchart. Allow different values of ‘l’ and ‘b’ before each
calculation of area
Algorithm Flowchart

Step 1: START START

Step 2: Read length, call it l


Read l,b

Step 3: Read breadth, call it b


Area ← l*b

Step 4: compute l*b, call it Area

Step 5: Display Area Display Area

Step 6: Go to step 2

Compiled by Daniel k ([Link].) 45 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 45 Chapter 1 : Introduction to Programming
Branching operations (Selection)

 With sequential instructions no possibility of skipping over one instruction.


 Branch is a point used to decide which set of instructions execute next.
 The question used to make the decision can only be yes or no.
 Depending on the answer, control flows in one direction or the other.
 Illustration: Construct an algorithm and flowchart to read two
numbers and determine which is large.
Algorithm Flowchart
Step 1: START
Step 2: Read two numbers A and B.
Step 3: If A > B then go to step 6
Step 4: Display B is largest.
Step 5: go to step 7
Step 6: Display A is largest
Step 7: STOP
Compiled by Daniel k ([Link].) 46 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 46 Chapter 1 : Introduction to Programming
Branching operations Selection

 Diamond symbol (decision symbol)


 Indicates decision is to be made
 Contains an expression that can be true or false
 Test the condition, follow appropriate path print
True
False Grade>=60
passed
Single Selection (if)
If grade >=60
print passed
Double Selection (if-else)
False True
If grade >=60 :
Grade>=60
Passed Failed ? passed
else:
failed

Compiled by Daniel k ([Link].) 47 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 47 Chapter 1 : Introduction to Programming
Selection (continued)

Light True
 Multiple Selection (switch) Red? Stop

False
If the light is ...
Light True Go
red -> stop Green?
False
green -> go Light True Slow
Yellow? Down
yellow -> slow down False

Nesting of Branching Operations


 There are many times when we need to choose between more than two
alternatives. One of the solutions to this is nesting of instructions.
 Illustration : Construct an algorithm and flowchart to see if a number ‘n’
is negative, positive, or zero
Compiled by Daniel k ([Link].) 48 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 48 Chapter 1 : Introduction to Programming
Con't…

 Algorithm Flowchart
Step 1: START
Step 2: Read in ‘n’
Step 3: Is n<0
Step 4: If yes, go to step 11
Step 5: Is n=0
Step 6: If yes, go to step 9
Step 7: Print “Positive”
Step 8: go to step 12
Step 9: Print “Zero”
Step 10: go to step 12
Step 11: Print “Negative”
Step 12: STOP

Compiled by Daniel k ([Link].) 49 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 49 Chapter 1 : Introduction to Programming
Loops

 Loops are the third major type of control structure that we need to examine.
There are 2 different types of loops, the counted loop and the conditional loop.

 The counted loop repeats a predetermined number of times

 The conditional loop repeats until a condition is satisfied.

 In the counted loop a counter keeps track of the loop executions. Once the
counter reaches a predetermined number, the loop terminates. Number of
loop executions is set before the loop begins and cannot be changed while the
loop is running.

 The conditional loop has no predetermined stopping point. Rather, each time
through the loop the program performs a test to determine when to stop. Also
the quantity being used for the test can change while the loop executes.
Compiled by Daniel k ([Link].) 50 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 50 Chapter 1 : Introduction to Programming
Con't…

 Variable used to control the loop referred as Loop Control Variable (LCV).

 Flowchart symbol for loop is hexagon. Inside the loop is the start and stop values
for LCV. Also a step value is included from which the computer decides how
many times to execute the loop. Inside the loop is the body which can consist of
any number of instructions. When the loop finishes, the control transfers to the
first statement outside the loop.

 With counted loops, it’s a must to know in advance how many times the loop
will execute. But if this information is not available and yet the problem demands
a loop means, we can make use of conditional loop, where the machine will
check every time through the loop to see whether it should be repeated or not.

 In conditional loop, programmer must change the Loop control variable.


Compiled by Daniel k ([Link].) 51 Chapter 3- Structures
Computer Programming II ( CoSc1012 ) 51 Chapter 1 : Introduction to Programming
Example

 An algorithm and flowchart to print out the numbers 1 to 100 and their
squares.
Algorithm
Start 1: START
Start 2: Loop (LCV start=1;stop=100,step=1)
Step 3: Print LCV, LCV2 value
Step 4: End loop Flowchart
Step 5: STOP

Compiled by Daniel k ([Link].) 52 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 52 Chapter 1 : Introduction to Programming
Repetition

 While
True while it’s still clumpy
 Do-while
Stir the mixture
Mixture
Stir
Clumpy?
False

Must I eat
Veggies?
ask parents if must eat
vegetables
Parent say True
“Yes”? while parents say “Yes”
False

Compiled by Daniel k ([Link].) 53 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 53 Chapter 1 : Introduction to Programming
Repetition (continued)

 For

Teaching a baby to count


from 1 to 10:
Counter = 1
counter = 1
Add 1 if counter <= 10:
to counter
increment counter
True
Counter
≤ 10? Print print counter number
counter

False

Compiled by Daniel k ([Link].) 54 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 54 Chapter 1 : Introduction to Programming
Individual Assignment

 Construct an algorithm and a flowchart for the following:


1. To print the numbers from 1 to 100.
2. To store and print odd numbers lessthan 200.
3. To find the sum of first n even numbers.
4. To print out a list of first n even numbers and their squares.
5. To find the sum of n positive numbers
6. To print the numbers upto 100 in reverse order
7. To find the sum of n natural numbers and average.
8. To find the largest of 3 numbers
9. To calculate the compound interest (C.l = P × (1 + r/100)n – P)
10. To find the cube of first n natural numbers (e.g.: 1, 8, 27, …., n3)
11. To read a number and find its factorial (3 ! = 3 × 2 × 1)
12. To obtains two integer numbers print out the sum and average
13. To obtain two number and display in reverse order

Compiled by Daniel k ([Link].) 55 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 55 Chapter 1 : Introduction to Programming
Structure Chart

 The structure chart is an important technique that helps the

analyst design the program for the new system.

 There are three components in the structured chart: modules,

connections between modules, and communication between

modules.

Compiled by Daniel k ([Link].) 56 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 56 Chapter 1 : Introduction to Programming
Structure Chart Example

A Simple Sequence Problem


• We want to develop an algorithm (a step by step process) for a
program to calculate the sum and average of two numbers entered
by the user of the program.

Compiled by Daniel k ([Link].) 57 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 57 Chapter 1 : Introduction to Programming
Cont…

 The structure chart shows the relationship among the various


components of the program.
 The top level is a general description of the problem to be solved
where the lower levels show all the detail or specifics of the
solution.
 NOTE: The second level of a structure chart attempts to break the
solution into three basic parts – an input module, a processing module
and an output module.

Compiled by Daniel k ([Link].) 58 Chapter 3- Structures


Computer Programming II ( CoSc1012 ) 58 Chapter 1 : Introduction to Programming

You might also like