Sei sulla pagina 1di 40

Introduction to

Programming
LOGIC & PROBLEM SOLVING
Logic & Problem solving
• Enables the learner to approach real world problems in a
systematic manner, which is an essential skill for a software
engineer.
Types of Problems
Examples of Computational Problems
Exercise
1. What comes in your mind when you hear the word problem?
List down the different words that you have come across.
2. Then try to search the problems which you can completely
solve with computer.
Classification of computational problems
Exercise
• Make a list of different examples of computational problems
like
• Find out who was the topper of your class ( arrange in ascending
or descending order)
• Calculate the average score of your class
Distributed Example
Sequential/Event Based Example
Exercise: Lifecycle

Identify the different steps involved in automating


the billing activity in a Retail Store.
Problem Solving Life Cycle
Need of Algorithms
• It should be able to interpret each instruction in the algorithm
and carry it out.
• It should be able to read a text containing of a set of
characters.
• It should be able to add and divide.
• It should be able to store (or remember) computed values.
• It should be able to write (or output) the answers obtained.
Algorithms
• Representation of computation
• Following notations are used for representing algorithms:-
• Flowchart: graphical representation
• Pseudo code: look like English statements with additional
qualities.
What is pseudocode?
• List of steps written in English
• Like the instructions for a recipe
• Must be in the right sequence
• Imagine saying “bake the cake” and then “mix it up”
Sample Pseudocode
• Task: add two numbers
• Pseudocode:
• Start
• Get two numbers
• Add them
• Print the answer
• End
What does a flowchart look like?
• The pseudocode from the previous slide would look like this as
a flowchart:

Start
Print answer
Get 2 numbers
End
Add them
What are those funny symbols?

• START/END

• INPUT/OUTPUT

• PROCESS

• DECISION
What are those funny symbols?

• START/END
• Used at the beginning
and end of each
flowchart.
What are those funny symbols?

• INPUT/OUTPUT
• Shows when
information/data comes
into a program or is
printed out.
What are those funny symbols?

• PROCESS
• Used to show
calculations, storing of
data in variables, and
other “processes” that
take place within a
program.
What are those funny symbols?

• DECISION
• Used to show that the
program must decide Y
whether something X>7?
(usually a comparison
N
between numbers) is true
or false. YES and NO (or
T/F) branches are usually
shown.
Another Sample:
Calculating Age
• Pseudocode:
• Start
• Get year born
• Calculate age
• Print age
• If age > 50 print OLD
• End
Another Sample: Start
Calculating Age
• Flowchart Get yr
• Start
• Get year born Calc age
• Calculate age
• Print age Print age
• If age > 50 print OLD
• End OLD Y Age>50?

N
End
Steps to develop an Algorithm
Example
• Write an algorithm to find the average marks scored by a
student. Also check whether the student has passed or failed.
For a student to pass, the average marks secured should be
65.
What does the computer understand?

Computer only understands machine


language instructions.
What is programming?
• Series of instructions to a computer to accomplish a task
• Instructions must be written in a way the computer can
understand
• Programming languages are used to write programs
What is programming?
• Some programming languages (like Java or C++) require the
code to be compiled (translated to binary) before it can be
started.
• Others (like JavaScript) are interpreted, meaning that each
command is translated separately when the program is
started.
Compiled versus interpreted
languages
• Compiled versus interpreted:
• A design-stage decision
• Performance is slower with interpreted languages

• Both compiled and interpreted languages have their strengths and


weaknesses
• No simple answer as to which is better -- it depends on the
application. Within a single application, we might decide to use
several programming languages.
Advantages of compiled
languages
• Assembler, COBOL, PL/I, C/C++ are translated by running the source
code through a compiler
• This results in very efficient code that can be executed any number
of times
• Often, the overhead for the translation is incurred just once, when
the source is compiled; thereafter, it need only be loaded and
executed
• Compiled programs will be more efficient and performing
• Interpreted languages are often parsed, interpreted, and executed
each time that the program is run, increasing the cost of running the
program
Classification
Translation from HLL to ML
Advantages of interpreted
languages
• An interpretive language is relatively easy to code, test, and change
• Good for one-time solutions
• Good for developing application prototypes
• Ad hoc versus fixed requests
• Time saver for command strings that are entered repeatedly
Paradigm of programming languages
• Procedural Programming Languages, 
• Structured Programming languages, and 
• Object oriented Languages.

• Key Difference:
• A procedural programming language consists of a set of
procedure calls and a set of code for each procedure.
• A structural programming language emphasizes on separating a
program’s data from its functionality.
• object oriented languages are based on entities known as
objects.
Procedural Programming
Languages
• These languages code programs in such a way that the
program executes statement by statement, reading and
modifying a shared memory. 
• statements are organized into procedures.
• These procedures are also known as subroutines or functions. 
• It is written in a step by step manner, thus it is very easy to
follow the smaller programs.

• Example: PASCAL, COBOL,FORTRAN


Structured Programming
languages
• These are based on the top down methodology in which a
system is further divided into compositional subsystem.
• It focuses on dividing the problem into sub parts and hence
simplifies the problem solving.
• It organizes large instructions by breaking them into separate
and smaller section of modules, sub routines and
subprograms.
• Control structures are used to determine the exact order in
which the set of instructions are to be executed.

• Example: C , Ada
Object oriented Languages: 
• Object oriented program languages are based on the ‘objects’
rather than ‘actions’.
• This language gives more preference to data rather than logic.
An object can be defined as the pairing of a piece of data with
the operations that can be performed on it.
• It is one of the latest and powerful types.
• Objects are capable of storing information and they can also
interact with other objects.
Summary

Potrebbero piacerti anche