Module - Unit-1 Intro To Programming
Module - Unit-1 Intro To Programming
UNIT
1 Introduction to
Programming
1
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
LESSON 1:
Basic Programming Concepts
OBJECTIVES:
At the end of the lesson, students will be able to:
DURATION: 2 hours
2
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Basic Programming Concepts
Introduction
Think about some various ways that people use computers. Each student uses
a computer to do some different tasks in school, such as typing a document, searching
for articles, sending an email, and participating or giving contributions in an online
class. At work, individuals use PCs to analyze data, make presentations, conduct
business transactions, communicate with colleagues and customers, control
machines in manufacturing facilities, and do numerous different things. At home,
individuals use PCs for various tasks such as paying bills, shopping online,
Figure 1. Microsoft Word and Adobe Photoshop. An example of Word Processing and Image Editing Program
Figure 1. shows screens from two usually used computer programs, Microsoft Word ,
and Adobe Photoshop. Microsoft Word is a word processing program that lets you
create a document, edit a document, and print documents with a personal computer.
Adobe Photoshop is an image editing or editor computer program that will enable you
to work with vivid images, such as photos taken with your mobile phone and digital
camera.
Computer Programming
The programming stages include design, or deciding what the program should
accomplish, and coding, which refers to the use of a programming language to express
the program logic in computer-readable form.
What we take to arranged is that the message with a human being is different than
communicating with any computer. A person has prior knowledge and life experience;
they know where to find the tea; they know that the milk is stored in a fridge or where
it is kept.
Like many natural languages, we use to communicate with each other. There
are many languages that a computer programmer can use to speak with a computer.
Each programming language has its own set of words and rules, called the
syntax of that language. If you are working on writing a computer program, you have
to follow the syntax of the language you are using; otherwise, you would not be
understood.
4
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Machine Language
Source: https://dfarq.homeip.net/
Example:
A computer programmer must have knowledge about the architecture of the particular
machine before programming in machine language. Developing programs using
machine language is a tedious job. It is challenging to recall the arrangement of binary
numbers for different computer architecture. Therefore, it is not much in practice.
Low-Level Language
High-Level Language
High-level languages, such as *COBOL, VISUAL BASIC, JAVA, etc. are some
of the popular languages used by the programmers. It is named high-level languages
to distinguish them from the machine and symbolic languages. It is also called
compiler languages since these languages require a unique program called a
compiler, which translates programs written in a particular language into a machine
6
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
language. When these languages are used, a compilation run is required before data
can be processed. The unique program written in a high-level language is called the
source program, and its translation in machine language is named an object program.
Language Description
Ada was developed in the 1970s,
Ada primarily for applications used by the
U.S. Department of Defense. This
computer language is named honor of
the “Countess Ada Lovelace" it is a
meaningful and vital historical symbol in
computing.
Beginners All-purpose Symbolic
Instruction Code is a general purpose-
BASIC language initially an outline designed in
the early 1960s to be straightforward
adequate for beginners to gain
knowledge. Nowadays, there are many
different versions of BASIC.
FORmulaTRANslator was the first high-
FORTRAN level programming language. It was
originally developed in the 1950s to carry
out complex arithmetic or mathematical
calculations.
Common Business-Oriented Language
COBOL was developed in the 1950s and was
designed for business applications.
Pascal was created in 1970 and was
Pascal initially designed for teaching computer
programming. The language was named
under the mathematician, physicist, and
philosopher Blaise Pascal.
C and C++ (it is pronounced as a "c plus
C and C++ plus"), and it is a powerful, general-
purpose computer language developed
at the Bell Laboratories. The C language
was developed in 1972, and the C++
language was developed in 1983.
Pronounced “c sharp. “This language
C# was created by Microsoft around 2000
for developing applications based on the
Microsoft.NET platform.
Java was created and developed by the
Java Sun Microsystems in the early 1990s. It
can develop programs that run on a
7
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Assembly Language
Now we could say that high-level languages are a lot easier to understand over
low-level languages. Let us take a look at the example below:
Let us say we want the value of a memory cell to be equal to number fifteen
(15). In a high-level language, we could simply accomplish this by writing: int x = 15;
where:
int (based from the dictionary word integer is a reserved word used to identify
the data type of a number equal (=) sign ( a generally accepted mathematical symbol)
is used to denote an equation process, 15 is simply the decimal value of the number
8
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
MOV is an instruction telling the processor to equate the value of the computer
memory (base memory – Bx) to OOFh in hexadecimal or IIII in binary (decimal
equivalent of 15)
Mnemonics are shortened English words that are used to stipulate a computer
instruction. Each special education in a binary code has a specific mnemonic. They are called
architecture-dependent, and there is a list to separate mnemonics for different computer
architectures.
▪ It takes more time and effort to write the code for the same.
▪ It is very complex and challenging to understand.
▪ The syntax is difficult to remember.
▪ It has a lack of portability computer programs among different computer
architectures.
▪ It needs more size or memory of the computer to run the extended programs
written in Assembly Language.
9
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
As we learned in the previous section, the computer understands the program only if
it is coded in its machine language. In this segment, we explain the procedure for
turning a program written in a high-level language into machine language. Because
the computer can understand only machine language instructions, programs that are
written in a high-level language must be translated into machine language. Once a
plan has been reported in a high-level language, the programmer will use a compiler
or an interpreter to do the translation.
On the other hand, an interpreter to translate the high-level instructions into machine-
understandable instructions (binary code) at runtime. A computer programmer cannot
run the computer program on a machine except if the right interpreter is available.
INTERPRETER COMPILER
Scans the whole computer program and
Translates the program one statement
translates it as an entire into machine
at a time.
code.
Interpreters typically take a small
Compilers typically take a massive
amount of time to examine the source
amount of period to analyze the source
code. However, the general
code. But the available execution time is
implementation time is comparatively
reasonably faster than interpreters.
slower than compilers.
It generates the middle object code,
None-intermediate object code is
which further needs linking. Thus, it
generated, thus a memory efficient.
requires more memory.
10
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
It is the job of the computer programmer to write and test the program. There
are four steps in this process:
(1) writing and editing the program,
(2) compiling the program,
(3) linking the program, and
(4) executing the program.
This is the process of developing and modifying source code - the name given to
the program instructions you write. Some C++ compilers come with a precise editor
that can deliver a lot of support in managing your computer programs. An editor often
provides a complete environment for writing, collecting, developing, and testing your
programs. This is sometimes called an integrated development environment, or
IDE.
You can also use some other editors to create your source files, but they must store
the code as plaintext without any extra formatting data embedded in it. Overall, if you
have a compiler system used and with an editor indicate, it will give a lot of features of
attributes that make it easier to write and organize your source code programs. There
will usually be automatic facilities for placing out the program text correctly, and color
highlighting for main essential language elements, which not only makes your code
more readable, also provides a clear indicator when you make errors when inputting
in such words.
On a Personal Computer, you could use one of the numerous free software and share
software computer programming editors. These will provide a lot of help in ensuring
your code is correct with syntax highlighting and auto indenting of your code. Try not
to utilize a word processor, for example, Microsoft Word, as these are not appropriate
for delivering a program code in light of the extra formatting information they store
beside the text. Purchasing one of the professionally developed programming
development environments which support C++, such as Borland or Microsoft, in which
case you will have pervasive editing capabilities. Before parting with your cash,
though, it is a good idea to manage that the level of C++ that is supported is
approximate to the current C++ standard.
11
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
✔ Compiling Programs
The compiler converts your source code into machine language and distinguish and
reports the errors in the compilation process. The input to this phase is the file you
produce during your editing, usually referred to as a source file.
✔ Linking Programs
The linker gives numerous modules generated by the compiler from source
code such as files, adds required code modules from program libraries supplied as
part of the C++ programming, and bonds everything into an executable whole. The
linker can also report and detect errors, for example, if part of your program is missing
or a nonexistent library component is referenced.
Program libraries extend and support the C++ language by providing routines
to carry out operations that are not part of the language. For instance, libraries
comprise procedures that support processes such as performing input and output,
calculating a mathematical square root, can compare two-character strings, or acquire
date and time information.
A failure during the linking phase means that you have to go back and edit your
source code once again. Success, on the other hand, will produce an executable file.
In a Microsoft Windows environment, this executable file will have a .exe extension.
Several IDEs also take Build options, which compile and link your computer program
in one step. This possibility will usually be found inside an IDE, in the Compile menu,
or a menu of its own.
✔ Executing Programs
The execution phase is where you run your program, having completed all the
last process successfully. This phase can also generate a wide variety of errors that
12
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
can contain producing the wrong output. In all cases, it is back to the editing process
to check your source code.
In IDEs, you will find a suitable menu command that lets you run or execute
your compiled computer program. This Run or Execute selection may have a menu
of its own, or you may find it under the Compile menu option.
In Windows, you can run the.exe file for your program as you would any other
executable. The procedures of editing a program, compiling a program, linking a
program, and executing a program are essentially the same for developing computer
programs in any environment and with any compiled language.
Programming Paradigm
Procedural Programming
Procedural programming is instinctive in the sense that it is very like how a program
work. If you want a computer to do a certain task, you should provide incrementally
procedures on how to do it. No surprise that most of the early computer programming
languages are all technical. Examples of procedural programming languages include
Fortran, COBOL, and C, which have been throughout since the 1960s and 70s.
Object-Oriented Programming
13
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
PROCEDURAL ORIENTED
OBJECT ORIENTED PROGRAMMING
PROGRAMMING
In procedural programming, the In object-oriented programming, the
computer program is divided into a little program is divided into small parts
part called the functions. called objects.
Procedural programming follows a top- Object-oriented programming follows a
down approach. bottom-up approach.
Object-oriented programming has
There is no access specifier in
access specifiers like private, public,
procedural programming.
protected, etc.
Adding new data and functions is not
Adding new data and functions is easy.
easy.
Procedural programming does not have
Object-oriented programming provides
any proper way of hiding data, so it is
data hiding, so it is more secure.
less secure.
In procedural programming, overloading Overloading is possible in object-
is not possible. oriented programming.
In procedural programming, the function In object-oriented programming, data is
is much essential than data. more important than function.
Procedural programming is based on an Object-oriented programming is based
unreal world. on the real world.
Examples: C, FORTRAN, Pascal,
Examples: C++, Java, Python, C#, etc.
Basic, etc.
14
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
LESSON 2:
1. remainder
Program Development
OBJECTIVES:
At the end of the lesson, students will be able to:
DURATION: 3 hours
15
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
The process of developing a program in C++ may not be visible if you have
never written a computer program before. However, it is very similar to various
situations in life in which at the start, it just is not clear how you are going to attain your
objective. Generally, you start with a rough idea of what you want to attain, but you
need to interpret this into an additional exact specification of what you want.
Once you have reached this more precise specification, you can work out a
series of steps to direct your final objective. It is like the idea of building a house; you
need to know what kind of place you want, how large it is going to be, what types of
materials are, and where you want to make it. This kind of detailed planning is
necessary when you want to compose or write a program.
Also, the first step is to get a clear idea of what you want to do.
For example:
▪ To start building your house, you should consider what facilities it ought
to provide: like how many bedrooms, or how many bathrooms, or how
big it is going to be, etc.
▪ All these things affect the house cost in terms of materials and the work
involved in building it. Mostly, it originates down to a compromise that
best sees your requirements within the limitations of the budget allotted,
the workforce, and the time that is accessible for you to finish the project.
It is the same as developing a program of any size. Even for a generally exact
issue, you need to know what kind of input to expect, how the information is to be
prepared, what sort of output is required, and how it will look. The data could be
entered with the keyboard, but it might also involve data from a disk file, or information
achieves over a telephone line or a network.
The output could be displayed on the screen or printed; perhaps it might involve
updating a data file on a disk.
16
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Note:
For more composite programs, you will need to look at many more
features of what the computer program will do. A direct definition of the problem
that your program is going to solve is an essential part of understanding the
resources and efforts that are going to be needed for the creation of a finished
product. Since these details also drive you to found whether the project is
possible.
For example:
▪ To get the house built, you will need detailed plans. These plans
empower the development workers to do their job, and the plans
describe in detail how the house will go together, the materials to use,
the dimensions, and so on.
▪ You will need the establishment dug before the walls are built, so the
plan must involve the segmenting work into manageable units to be
performed in a logical sequence.
It is the same as a program. You will need to specify what the computer program
does by separating it into a set of well-defined and manageable chunks that are
reasonably self-contained. You will also need to detail how these chunks connect, as
well as what information each piece will need when it executes. This will empower you
to build the logic of each block comparatively independently from the rest of the
program.
If you treat a massive program as one colossal process that you try to code as
a single chunk, the chances are that you will never get it to work. Three tools will help
us with this task:
The structure chart is used to design the whole program. Pseudocode and
flowcharts, on the other hand, are used to develop the individual parts of the program.
These parts are known as modules in pseudocode or functions in the C++ language.
The hierarchy chart (also known as the structure chart) displays the connection
among several modules. Its name originates from its general use in viewing the
17
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Hierarchy charts are generated by the computer programmer to aid the document in
a program. They carry the large image of the modules (or functions) used in a program.
Figure 2. Hierarchy or Structure chart for a program that has a five function
Source: https://press.rebus.community/programmingfundamentals/
18
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Implementation
With the detailed design of a house, the work can start. Each group of
construction workers will start completing their parts of the project at the right time.
Each phase will need to be examined to check that it has been done correctly before
the following phase begins. Except these checks could simply result in the entire
house collapsing.
Naturally, if a program is enormous, you will write the computer source code
one element at a time. As one part is completed, you can now proceed with writing the
next. Each part of the code will be based on the detailed design specifications; it must
verify that each piece performs the intended task, as much as you can, before
proceeding. It will enable you to gradually progress to a fully functioning program that
does everything you originally intended.
In this stage, we see whether the code written in the previous step is solving
the specified problem or not. That means we test the program, whether it is solving
the problem for several input information values or not. We also test whether it could
come up with the desired output or not.
The house is complete, but there are still a lot of things that need to be tested:
the water and electrical outlet, the water pump, the heating, and so on. Any of these
areas can have problems that need to be fixed. This is occasionally a reiterative
process, in which issues with one feature of the house can be the reason for effects
going wrong somewhere else.
The mechanism with a program is similar. Each of your program modules, the
pieces that make up your program, will need to be tested individually. When they do
not work correctly, there is a need to debug them. Debugging is the process of finding
and correcting errors in your program. When finding the errors in a program, it involves
tracing where the information went and how it was processed
The term bug is used to denote any error in a program. Its term came from a discovery
that a computer program error was caused by an insect shorting part of the circuit in
the computer.
By inspecting the code of a simple program, you can often find an error by
merely inspecting the code. The process of debugging a computer usually includes
adding extra program code to create output that will empower you to check what the
arrangement of events is and what intermediate values are produced in a computer
program. With a huge program, you will also need to test the computer program
modules in combination. Although the discrete modules may work, there is no
assurance that they will work together! The jargon for this stage of program
development is integration testing.
19
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
There are two types of testing: Blackbox and Whitebox. The system test
engineer and the user do Blackbox testing. Whitebox testing is the responsibility of the
programmer.
Blackbox Testing
Blackbox Testing gets its name from the concept of testing the program
without knowing what it is inside- without knowing how It works. In other words,
the program is like a black box that we cannot see into.
The plan will then be used when the system is tested. We should ask to
see this test plan before we write our program. The test engineer’s goal will help
us make sure we fully understand the requirements and help us create our test
plan.
Whitebox Testing
Whereas Blackbox testing assumes that the tester knows nothing about
the program, Whitebox testing assumes that the tester knows everything about
the program. In this case, the program is like a glass in which everything inside
is visible.
1) Verify that every line of code has been executed at least once.
Fortunately, there are programming tools on the market today that
will do this for us.
3) For every condition that has a range, make sure the tests include the
first and last items in the content, as well as things below the first and
above the last-the most common mistakes in array range tests occur
at the extreme of the range.
4) If error conditions are being checked, make sure all error logic is
tested. This may require us to make temporary modifications to our
program to force the errors.
20
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Program Documentation
When the program is finished and thoroughly tested, the documentation on the
program is included for its distribution. Documentation usually contains the necessary
information about the program requirements – the operating system and hardware
requirements needed for the program to run. It also includes technical data such as
who created the program, whom to contact in case of a problem with the program, and
instructions on the use and maintenance of the program.
Maintenance
At this stage, the computer program is actively used by the users. If any
improvements are found in this stage, all the steps are to be repeated to make the
enhancements. That means, in this stage, the solution (program) is used by the end-
user. If the user experiences any issue or needs any improvement, then we need to
repeat all the stages from the beginning, so that the encountered problem is answered,
or enhancement is added.
This final stage in programming is maintaining or updating the program. This is the
phase where the programmer must keep the program running smoothly and updated
with the developments and changes in the field where it is used
21
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
LESSON 3:
Logic Formulation
OBJECTIVES:
At the end of the lesson, students will be able to:
DURATION: 5 hours
22
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
Computers are logical machines. They follow instructions down to the letter
(follow steps as instructed than as intended). Although computers perform accurate
calculations or computations, an error in a formula or a value may cause difficulty
detecting errors. Computers cannot think and, therefore, cannot correct wrong
instructions. To be achieved, a program must be carefully designed before they are
created or written.
Program Logic Formulation deals with this problem. It provides the tools needed
to design the right solution by helping you define and identify the problem, its
requirements, and the required instructions.
The two most used tools will be of great help for you to formulate a better
solution. These are the flowchart and algorithm.
Algorithm
Problem:
The price of one kilo of an apple is 50 pesos. If the customer buys three or more
kilos of apples, 5 pesos will be discounted for every kilo. Determine the total cost of
apples purchased.
Algorithm:
The above example simply breaks down the problem into sub-problems that
need to be solved before coming up with the required answer. First, you must know
how many apples the customer is buying before you can make any computations.
23
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
Next, you then compute the cost depending on the number of apples purchased and
then use a formula that would calculate the appropriate value. Finally, you then come
up with the answer through the calculations you made. You cannot give a solution to
the problem immediately by saying outright how much the apples cost without first
knowing how many apples are being purchased, how much is the cost of apples if
there were discounts, and performing the calculations based on them .
Flowchart
▪ Variable - The memory cells used for storing a program’s input data and its
computational results are called variables. It is the representation of an idea
that is changing.
For Example:
int a, b, c;
▪ Constant - Named item is used to specify a name for a memory cell that will
contain a value that does not change (fixed amount).
For Example:
long width = 5;
Note:
V ariables that hold a number must be initialized by equating it to zero, and variables
that will keep word(s) may be initialized by equating it to space and enclosing it in double-
quotes (name" ")
24
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Types of Flowcharts
Flowcharting Symbols
25
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
26
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
Flowchart Development
Structured flowcharts are designed using the four program structures, namely:
a) Sequential - Steps are carried out in a firmly sequential manner; each stage is
performed exactly once.
These structures will be our guide to producing the correct structured flowchart
necessary to prepare the right program.
Sequential
In this structure, steps are performed in a strictly sequential manner and are
executed once. This is the simplest method of control. It is easy to develop and
understand. The following problems in flowcharting illustrate simple logic flow.
Problem 1
Design an algorithm and flowchart that would determine the product of two numbers.
Algorithm
START
1: Define the variables.
(Let A be the first A=0
B=0
number and B as the C=0
second number, C for a
product).
Input
A,B
2: Initialize the values to be used:
A = 0, B= 0, C= 0
C = A* B
3: Input values for A and B.
4: Calculate the product of the two
Output C
numbers. C = A*B. 27
IT 103: COMPUTER PROGRAMMING 1
5: Output the product.
END
UNIT 1: Basic Programming Concepts
Problem 2
Design an algorithm and a flowchart that will compute the sum, difference, product,
and quotient of these values and print them out.
Algorithm
1. Define the variables.
(Let X, be the first number and Y as the
second number, SUM for sum, DIFF for
difference PROD for a product, and QUO for
quotient).
2. Initialize the variables to be used.
3. Input values for X and Y.
4. Calculate the sum, difference, product, and
quotient of the two values read.
5. Output the computed values (sum, average,
product, and quotient).
START
Input
X, Y
END
28
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Selection
Problem 3
Design an algorithm and a flowchart that will determine if the grade of a student is
"passed" or failed," considered 75 as the passing grade.
Algorithm
1. Define the variables.
(Let G be the grade of a student)
2. Initialize the variables to be used.
3. Input student grade.
4. Test if G is greater than or less than 75.
5. If G is greater than or equal to 75, print "Passed," and if it is less than 75, print
"Failed."
Start
G =0;
Input
G
True
If Print
G >=75
“Passed”
False
Print
“Failed”
End
29
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Problem 4
A company plans to give a year-end bonus to each of its employees. Draw a
flowchart, which will compute the bonus of an employee. Consider the following
criteria: If the monthly salary of the employee is less than 10,000.00 pesos, the bonus
is fifty percent of the wage; for employees with wages greater than or equal to
10,000.00 pesos, the bonus is equivalent to the monthly salary. Print out the
corresponding bonus of the employee.
Algorithm
1. Define the variables to be used
(Let S be the salary, B for bonus of the employee)
2. Initialize the variables to be used.
3. Input salary.
4. Test the value of the salary. If S is less than 10,000, then the bonus of the
employee is fifty percent of the salary. If the salary is greater than or equal to
10,000, the bonus is equivalent to their monthly salary.
5. Output the corresponding bonus of the employees.
START
S = 0; B = 0
INPUT
True
IF
S < 10,000 B = S *0 .50 A
False
B=S
Output B
END
30
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Problem 5:
Design an algorithm and a flowchart that will examine the value of the temperature.
Then display the following messages depending on the value assigned to temp.
Temperature Message
Less than 0 ICE
Between 0 and 100 WATER
Exceeds 100 STEAM
Algorithm
1. Define the variables. (Let temp be variable for the temperature)
2. Initialize the variables to be used.
3. Input the value of temp.
4. Test if the value of temp is less than 0, between 0 and 100, or exceeds 100.
5. If the temp is less than zero, print "ICE," and if the number is greater than or equal
to zero and less than or equal to 100, print "WATER". Else the number is greater
than 100, print "STEAM."
START
temp = 0
input
temp
True
IF PRINT A
temp< 0 “ICE”
False
True
IF
temp>=0 and PRINT
temp < = 100N “WATER” A
False
PRINT
“STEAM”
END
31
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Repetition
A Loop is used when it is desired to make the same calculation on more than
one set of data. It consists of repeating a program, or a selection of programs, and
substituting new data for each repetition. Computer program loops play an important
part in data processing since they provide efficient means of processing several sets
of data with an identical program segment.
Problem6
Design a flowchart that will print “Hello “twenty times.
Solution 1
Algorithm:
1. Define the variables to be used. (Let x be the counter)
2. Initialize the counter x = 1.
3. Test the value of x. if the value of x < = 20 Print “Hello”, else end of program.
4. Increment x (x=x+1).
5. Repeat step 3.
START
X=1
False
X <= 20
END
True
X=X+1
32
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Solution 2
Algorithm:
1. Define the variables to be used. (Let x be the counter)
2. Initialize the x = 0.
3. Print “Hello”
a. Increment x Increment x (x=x+1).
4. Test the value of x. if the value of x < = 20 repeat step 3, else end of program.
.
START
X=0
X=X+1
True
False
X <= 20
END
Modular Structure
Most computer programs perform a task that is huge enough to be broken down
into various subtasks. For this reason, computer programmers usually break down
their programs into modules. A module is a set of statements that exist within a
program to perform a specific task. Instead of writing a massive program as one long
sequence of statements, numerous small modules can be written; each carries out a
specific part of the task. These modules can then be executed in the want order to
perform the overall task.
The approach is sometimes called divide and conquer because an enormous
task is divided into several smaller tasks that are easily performed. This approach is
represented by the structure chart. A structure chart, also is known as a hierarchy
chart, shows the functional flow through the different modules in a program. For
33
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
example, let us write separate modules for the kitchen, bathroom(s), bedrooms, family
room, and living room in designing the flooring of a house, as illustrated below.
Flooring
Every modern programming language lets you create modules that are
commonly called procedures, subroutines, subprograms, methods, and functions.
✔ Simpler Code
✔ Code Reuse
✔ Better Testing
The testing and debugging of the program get less complex when every
programming task is contained in a module. Developers can test every module
in a program independently to determine whether it correctly performs its
operation. This makes it simpler to segregate and fix errors.
34
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
✔ Faster Development
Top-Down Design
▪ The overall task that the program is to perform is divided into a series of
subtasks.
▪ Each of the subtasks is evaluated to determine whether it can be further broken
down into more subtasks. This step is repetitive until no more subtasks can be
recognized.
▪ Once all of the subtasks have been identified, code will be written.
Calling a Module
A module definition specifies what the module does and must be called to
execute. When a module is called, the control jumps to that module and then executes
the statement in the body of the module. When the end of the module is reached, the
control jumps back to the part of the program that called the module, and the program
resumes execution at that point.
A module call is shown in a flowchart with a rectangle with vertical bars on each
side. The name of the module that is being called is written on the symbol.
Message( )
35
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
start
Display “I have a
message for you.”
Message()
return
Display “That’s
all.”
end
36
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming
Multimedia ConceptsConcepts
Flowchart Tracing
To determine the correctness of logic flow within any given flowcharting, it will
usually require some tracing and testing. Tracing, it will provide a clearer
understanding of the processing steps involved. This will usually require the use of
actual test data values. The first step is to have a list of variables to hold different
values during the processing operation. We begin tracing the flowchart with the first
instruction and continue by following the appropriate flow lines and flow direction
indicators.
1. START
A=0;B=0;
C=0;D=0;
A=B+1
END Output:42
37
IT 103: COMPUTER PROGRAMMING 1