0% found this document useful (0 votes)
24 views68 pages

Unit1 - Structured Theory and Demo

Uploaded by

santhosh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views68 pages

Unit1 - Structured Theory and Demo

Uploaded by

santhosh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

SRM Institute of Science and Technology

Advanced Programming Practice-18CSC207J

Unit 1
Overview of the Unit

 Structured Programming Paradigm

 Procedural Programming Paradigm

 Object Oriented Programming Paradigm


Structured Programming Paradigm
Unit-I (15 Session)
Session 1-5 cover the following topics:-

 Structured Programming Paradigm


 Programming Language Theorem
 Böhm-Jacopini theorem
 Sequence, Selection,Decision,Iteration and Recursion
 Other Languages- : C/C++/Java /C# /Ruby (introduction of each language)
 Demo: Structured Programing in Python
 Lab 1: Structured Programming

Assignment : Comparative study of C/C++/Java /C# /Ruby


 TextBook: Shalom, Elad. A Review of Programming Paradigms Throughout the History: With a Suggestion Toward a Future Approach, Kindle
Edition
2.Procedural Programming Paradigm
Session 6 – 10 covers the following Topics:-
 Procedural Programming Paradigm
 Routines, Subroutines, Functions
 Using Functions in Python
 logical view, control flow of procedural programming in various aspects
 Other languages: Bliss, ChucK, Matlab
 Demo: creating routines and subroutines using functions in Python
 Lab 2: Procedural Programming
Assignment : Comparative study of : Bliss, ChucK, Matlab
TextBook: Shalom, Elad. A Review of Programming Paradigms Throughout the
History: With a Suggestion Toward a Future Approach, Kindle Edition
3. Object-Oriented Programming

Session 11-15 covers the following Topics:-


 Object Oriented Programming Paradigm
 Class, Objects, Instances, Methods
 Encapsulation, Data Abstraction
 Polymorphism, Inheritance
 Constructor, Destructor
 Example Languages: BETA, Cecil, Lava.
 Demo: OOP in Python
 Lab 3: Object Oriented Programming
 Assignment : Comparative Study of BETA, Cecil, Lava.
TextBook: Shalom, Elad. A Review of Programming Paradigms Throughout the History:
With a Suggestion Toward a Future Approach
What is Programming Paradigm?
 A programming paradigm is a fundamental
style of computer programming,
serving as a way of building the structure
and elements of computer programs.
 Various programming languages have different
capabilities and styles and they are
defined by their supportedprogramming
paradigms.
 Some programming languages follow only one
paradigm [Smalltalk - object-oriented and
Haskell - functional] , while others
support multiple paradigms. [C++,
Java, C#, Scala, Python, Ruby…]
HISTORY !!
Gallery of Giant, Ancient Computers
John Mauchly and Dr Presper Eckert Jr looking at a
portion of ENIAC, which they co-designed in 1946.
(Credit: Hulton Archive/Getty Images)
The ENIAC, the first electronic computer, being
developed at the University of Pennsylvania, 1946.
(Credit: Apic/Getty Images)
Programmer Grace Hopper, who invented the first computer
language compiler (which transferred mathematical code into
machine code), also used gender stereotypes to encourage
women to enter the field
History of programming
languages
 Programming language is “a notation for
communicating to a computer what we
want it to do
 Machine language
 Assembly language
 FORTRAN and algebraic notation
 Structured abstractions and machine
independence
 Computation without the von Neumann
architecture
MACHINE LANGUAGE
 Instead of turning off the computer to
reconfigure its circuits, the operator could flip
switches to enter these codes, expressed in machine
language, into computer memory.
0010001000000100
0010010000000100
0001011001000010 16 bits - single
0011011000000011 machine
language
1111000000100101 instruction
0000000000000101
0000000000000110
0000000000000000

4 bits - Opcode 12 bits instruction code


ASSEMBLY LANGUAGE
 Assembly language relies on software tools to automate
some of the tasks of the programmer
 A program called an assembler translates the symbolic
assembly language code to binary machine code.
 Programmers also used a pair of new input
devices – a keypunch machine to type their assembly
language codes and a card reader to read
the resulting punched cards into memory for the
assembler.
ORIG x3000; DISADVANTAGE
LD R1, FIRST;
• Abstract
LD R2, SECOND;
mathematical idea
ADD R3, R2, R1;
ST R3, SUM;
• Computer hardware
HALT; architecture
FIRST .FILL #5 ;
SECOND .FILL #6 ;
SUM .BLKW #1 ;
FORTRAN AND ALGEBRAIC
NOTATION
 Unlike assembly language, high-level
languages (Java, Python, C, …)
support notations closer to the
abstractions, such as algebraic
expressions, used in mathematics and
science.
int first = 5;
int second = 6;
int sum = first + second;
 FORTRAN - FORmula TRANslation language
STRUCTURED ABSTRACTIONS AND MACHINE
INDEPENDENCE
 Higher level of abstraction and Portability
 ALGOL – Algorithm - the language
included notations for structured
control statements for sequencing,
loops, and selection. Finally, support for
procedures, including recursive
procedures, was provided.
 ALGOL and ALGOL compiler
 Pascal and Ada
COMPUTATION WITHOUT THE VON
NEUMANN ARCHITECTURE

 von Neumann architecture : consists of an area of


memory where both programs and data are stored
and a separate central processing unit that
sequentially executes instructions fetched from
memory.
 relied upon changes to the values of
variables
 single-processor model of computation Vs
multiple CPUs
 Programming languages neednot be based on any
particular model of hardware, but needonly support
LANGUAGE CATEGORIES
 Imperative
IMPERATIVE
 Functional OBJECT ORIENTED
VISUAL LANGUAGE
 Logic Eg: .NET
LOGIC PROGRAMMING
 Object-oriented.
MARKUP LANGUAGES SPECIAL-PURPOSE
HTML - Hyper Text Mark-up Language RPG - Report Program
JSTL - Java Server Pages Generator
Standard Tag Library (RPG),
XSTL - eXtensible Stylesheet APT - Automatically
Language Transformations Programmed Tools
SCRIPTING GPSS - General Purpose
LANGUAGE Simulation
Eg: Perl, System
JavaScript, Ruby
IMPORTANCE OF VARIOUS
PROGRAMMING CONCEPTS
 Increased capacity to express ideas
 does not directly support some features and
constructs
 Improved background for choosing appropriate
languages
 Increased ability to learnnew languages
 Better understanding of the significance of
implementation
 Better use of languages that are already
Structured Programming
 Structured programming is a paradigm that is based on
improving clarity and quality of programs by using
subroutines, block structures and loops (for and while) and
discouraging the use of goto statement.

1. History
2. Overview
3. Component
4. Representations in different languages(C, C++, Java,
Python)
1. History
The Böhm-Jacopini theorem, also called structured
program theorem, stated that working out a function is possible
by combining subprograms in only three manners:
 Executing one subprogram, and the other subprogram
(sequence) .
 Executing one of two subprograms according to the value of
a Boolean expression (selection) .
 Executing a subprogram until a Boolean expression is true
(iteration)
 Some of the languages that initially used structured
approach are ALGOL, Pascal, PL/I and Ada.

 By the end of the 20th century, concepts of structured


programming were widely applied so programming
languages that originally lacked structure now have it
(FORTRAN, COBOL and BASIC). Now, it is possible to
do structured programming in any programming language
(Java, C++, Python ...).
2. Overview

Structured programming was defined as a method used to minimize complexity


that uses:
1. Top-down analysis for problem solving
2. Modularization for program structure and organization
3. Structured code for the individual modules

The statements within a specific control structure are executed:


 Sequentially
 Conditionally
 Repetitively
ADVANTAGES OF STRUCTURED PROGRAMMING

 Programs are more easily and more


quickly written.
 Programs have greater reliability.
 Programs require lesstime to
debug and test.
 Programs are easier to maintain.
3.Component
 3.1 Structograms
 3.2 Subroutine
 3.3 Block
 3.4 Indentation
 3.5. Control structure – sequence, selection and iteration
Component
 Structograms or Nassi–Shneiderman -graphical
representation of structured programming.
 Structograms can be compared to flowcharts.
 Nassi–Shneiderman diagrams have no
representation for a goto statement.

Ex:-
 Structograms use the following diagrams:
1.process blocks - Process blocks represent the simplest
actions and don’t require analysis. Actions are performed
block by block.
2.Branching blocks
Branching blocks are of two types – True/False or
Yes/No block and multiple branching block.
3.Testing loops
Testing loops allow the program to repeat one or
many processes until a condition is fulfilled. There are
two types of testing loops – test first and test last
blocks – and the order in which the steps are
performed is what makes them different.
3.2.Subroutine

 Procedure,
 Function
 Routine
 Method
 Subprogram.
 Callable unit- Generic term.
Block
 Two types of blocks can be
distinguished based on their location
within a program:
 block associated with a procedure
 in-line block
Control structure – sequence, selection ,iteration and recursion.
(example for Control structure)

Recursion:
Recursion"; a statement is executed by repeatedly calling itself
until termination conditions are met. While similar in practice to
iterative loops, recursive loops may be more computationally
efficient, and are implemented differently as a cascading stack .

Graphical representation of the three basic patterns — sequence, selection, and


repetition
Note:

What is meant by structured language?


 C is called a structured programming language because to solve a large problem, C
programming language divides the problem into smaller modules called functions or procedures each of
which handles a particular responsibility. The program which solves the entire problem is a collection of
such functions

Examples of Structured Programming language are C, C+, C++, C#, Java, PERL, Ruby, PHP, ALGOL,
Pascal, PL/I and Ada

What is unstructured programming language?


 An unstructured program is a procedural program – the statements are executed in sequence as written. But
this type of programming uses the goto statement. A goto statement allows control to be passed to any other
place in the program. ... This means that it is often difficult to understand the logic of such a program.

Examples of unstructured Programming language are JOSS, FOCAL, MUMPS, TELCOMP, COBOL
Control Structure - DECISION MAKING (PYTHON )

 Decision making statements in programming languages decides the direction of flow of


program execution. Decision making statements available in python are:
if statement :
It is used to decide whether a certain statement or block of statements will be executed
or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if condition:
# Statements to execute if
# condition is true
Example : OUTPUT
i = 10 I am Not in if
if (i > 15):
print ("10 is less than 15")
print ("I am Not in if”)
 if..else statements:
We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Example :
i = 20;
if (i < 15):
print ("i is smaller than 15")
print ("i'm in if Block")
else:
print ("i is greater than 15")
print ("i'm in else Block")
print ("i'm not in if and not in else Block")
 NESTED IF STATEMENTS
Python allows us to nest if statements within if statements. i.e,
we can place an if statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
EXAMPLE : NESTED IF ELSE
i = 10
if (i == 10):
if (i < 15):
print ("i is smaller than 15")
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")
IF-ELIF-ELSE LADDER
 Here, a user can decide among multiple options. The if statements
are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is
executed, and the rest of the ladder is bypassed. If none of the
conditions is true, then Syntax:-
Syntax:-
if (condition):
statement
elif (condition):
statement
else:
 statement the final else statement will be executed.
EXAMPLE
i = 20
if (i == 10):
print ("i is 10")
elif (i == 15): OUTPUT
i is 20
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")
CONDITION SYNTAX
SIMPLE IF if test expression:
statement(s)

IF….ELSE if test expression:


Body of if
else:
Body of else
IF...ELIF...ELSE if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
NESTED IF if test expression:
if test expression:
Body of if
else:
Body of else
else:
Body of else
Conditional Expression
A conditional expression evaluates an expression based on a condition.
Conditional expression is expressed using if and else combined with expression
Syntax:
expression if Boolean-expression else expression
Example:
Biggest of two numbers OUTPUT
The biggest number is 23
num1 = 23
num2 = 15
big = num1 if num1 > num2 else num2
print ( “ The biggest number is “ , big )
Even or odd OUTPUT
print ( “ num is even “ if num % 2 == 0 else “ num is odd “) Num is even
Iteration – Loops
 Python has two primitive loop commands:
 while loops
 for loops
The while Loop

 With the while loop we can execute a set of statements as long as


a condition is true
Example
OUTPUT
Print i as long as i is less than 6: 1
i=1 2
while i < 6: 3
4
print(i)
5
i += 1

Note: remember to increment i, or else the loop will continue forever.


The break Statement

With the break statement we can stop the loop even if the
while condition is true:
Example - Exit the loop when i is 3:
OUTPUT
i=1 1
2
while i < 6: 3
print(i)
if i == 3:
break
i += 1
THE CONTINUE STATEMENT

With the continue statement we can stop the current iteration,


and continue with the next:
Example
Continue to the next iteration if i is 3:
OUTPUT
i=0
1
while i < 6: 2
i += 1 4
if i == 3: 5
continue 6
print(i)
The else Statement
 With the else statement we can run a block of code once when the
condition no longer is true:
 Example
 Print a message once the condition is false:
OUTPUT
 i=1 1
while i < 6: 2
print(i) 3
i += 1 4
else: 5
i is no longer lesser
print("i is no longer lesser than 6") than 6
For Loops

 A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string)
Example
Print each fruit in a fruit list: OUTPUT
apple
fruits = ["apple", "banana", "cherry"] banana
cherry
for x in fruits:
print(x)

The for loop does not require an indexing variable to set beforehand
Looping Through a String

 Even strings are iterable objects, they contain a


sequence of characters:
OUTPU
Example T
b
 Loop through the letters in the word "banana": a
 for x in "banana": n
a
print(x) n
a
THE BREAK STATEMENT
With the break statement we can stop the loop before it has looped
through all the items:
Example
OUTPUT
Exit the loop when x is "banana": apple
banana

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
if x == "banana":
break
THE CONTINUE STATEMENT
With the continue statement we can stop the current
iteration of the loop, and continue with the next:
Example
Do not print banana: OUTPUT
apple
cherry

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
continue
The range() Function

 To loop through a set of code a specified number of times, we can use


the range() function,
 The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
 Example
 Using the range() function: OUTPUT
 for x in range(6): 0
print(x) 1
 Note that range(6) is not the values of 0 to 6, but the values 0 to 5. 2
3
4
5
 The range() function defaults to 0 as a starting value, however it is possible to specify
the starting value by adding a parameter: range(2, 6), which means values from 2 to 6
(but not including 6):
 Example OUTPUT
2
Using the start parameter: 3
 for x in range(2, 6): 4
print(x) 5

 The range() function defaults to increment the sequence by 1, however it is possible to


specify the increment value by adding a third parameter: range(2, 30, 3):
 Example
 Increment the sequence with 3 (default is 1):
for x in range(2, 30, 3):
OUTPUT
print(x)
2
5
8
11
14
17
20
23
26
29
Else in For Loop

 The else keyword in a for loop specifies a block of code to


be executed when the loop is finished:
 Example
 Print all numbers from 0 to 5, and print a message when the
OUTPUT
loop has ended: 0
for x in range(6): 1
2
print(x) 3
4
else: 5
print("Finally finished!") Finally finished!
Nested Loops
A nested loop is a loop inside a loop.
 The "inner loop" will be executed one time for each iteration of the "outer loop":
 Example OUTPUT
 Print each adjective for every fruit: red apple
red banana
 adj = ["red", "big", "tasty"]
red cherry
fruits = ["apple", "banana", "cherry"]
big apple
big banana
for x in adj:
for y in fruits: big cherry
print(x, y) tasty apple
tasty banana
tasty cherry

eference : https://www.w3schools.com/python/python_for_loops.asp

You might also like