0% found this document useful (0 votes)
2 views7 pages

Chapter 5 - Develop Code

Chapter 5 discusses the development of code, explaining the difference between source code and object code, and the importance of algorithms in programming. It covers data types, variable initialization, type coercion, and three control constructs: sequence, selection, and repetition, providing examples in Python. The chapter emphasizes the need for proper variable declaration and initialization in different programming languages.

Uploaded by

Achintha Pradeep
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)
2 views7 pages

Chapter 5 - Develop Code

Chapter 5 discusses the development of code, explaining the difference between source code and object code, and the importance of algorithms in programming. It covers data types, variable initialization, type coercion, and three control constructs: sequence, selection, and repetition, providing examples in Python. The chapter emphasizes the need for proper variable declaration and initialization in different programming languages.

Uploaded by

Achintha Pradeep
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

Chapter 5

Develop Code

Source code is generally understood to mean programming statements that


are created by a programmer with a text editor or a visual programming
tool and then saved in a file.

Object code generally refers to the output, a compiled file, which is


produced when the Source Code is compiled with a compiler.

Source code is the fundamental component of a computer program that is


created by a programmer, often written in the form of functions,
descriptions, definitions, calls, methods and other operational statements.

Algorithms and Programs

As you learnt in Unit 1, an algorithm is a precise method of solving a


problem. It consists of a sequence of unambiguous, step by step
instructions. A program is an algorithm that has been converted in to
program code so that it can be executed by a computer.

Data Types

Algorithm use variables (named memory locations) to store values.


Variables have a variety of uses. When algorithms are converted in to
programs, the computer needs to be told what type of data is stored in each
variable. Every programming language has a number of built in data types.

1
When writing pseudocodes, you don’t have to specify data type of
variables. However, data types become more important once you start
programming in a high level language.

The method of declaring variables differs between programming languages.


Some languages, such as Python, automatically select the appropriate data
type for a variable based on the data assigned to it.

Other programming languages, such as Java and C#, require the data type
of variable to be declared before the variables can be used.

Example in Java:
String name = "John";

int age = 16

Variable Initialisation

Initialisation is the process of assigning an initial value to a variable.

When a variable is declared, the computer gives it a location in its memory.


Initially this location is empty, so before a variable can be used, it has to be
given a value.

You can put an initial value into a variable by:

• Initialising it when the program is run (Ex: SET total TO 0, in


pseudocodes)
• Reading a value from a keyboard or other device

In Python:
Total=0

Total = Total + admissionCharge

Type Coercion (Casting)

Sometimes data type of a variable gets changed during program execution.


This is known as type coercion.

2
This includes conversion from Number to String, String to Number, Boolean
to Number etc. when different types of operators are applied to the values.

For example, if an integer value and a real value are used in an arithmetic
operation, the result will always be a real.

In Python, type coercion is done automatically.

Example 1, the output from the following program is 3.25


x=1

y=2.25

z=x+y

print(z)

Example 2
x = 10

print("x is of type:",type(x))

y = 10.6

print("y is of type:",type(y))

z = x + y

print(z)

print("z is of type:",type(z))

Example 3
# initializing integer

s = '4'

# printing character converting to integer

c = ord(s)

print ("After converting character to integer : ")

3
print (c)

# printing integer converting to hexadecimal string

c = hex(56)

print ("After converting 56 to hexadecimal string : ")

print (c)

# printing integer converting to octal string

c = oct(56)

print ("After converting 56 to octal string : ")

print (c)

Output
After converting character to integer :

52

After converting 56 to hexadecimal string :

0x38

After converting 56 to octal string :

0o70

Three Control Constructs

1. Sequence
2. Selection
3. Repetition

Sequence
Ex : 1

# Taking kilometers input from the user

kilometers = float(input("Enter value in kilometers: "))

4
# conversion factor

conv_fac = 0.621371

# calculate miles

miles = kilometers * conv_fac

print("No of miles =",miles)

print('%0.3f kilometers is equal to %0.2f miles'


%(kilometers,miles))

Selection
Ex 1:

a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")

Ex 2:

a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

Ex 3:

a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")

Ex 4:

5
a = 33
b = 200
if not a > b:
print("a is NOT greater than b")

Repetition
Ex 1:

i = 1
while i < 6:
print(i)
i += 1

Ex 2:

i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)

Ex 3:

i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")

Ex 4:

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


for x in fruits:
print(x)

6
Ex 5:

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


for x in fruits:
if x == "banana":
break
print(x)

[Link]
t_life_cycle.htm

You might also like