Professional Documents
Culture Documents
PROGRAMMING
What is p rog rammin g ?
VARIABLE CONSTANT
D ATA B E I N G S T O R E D I N M E M O RY S PA C E :
0 10 identifier Number 10
1 Red Colour Red
2 22.2 Price 22.2
3 3
4 4
5 5
6 6
IDENTIFIER
A name given to a variable, constant , data structure (e.g. array) or subroutine so that we can use
the data stored in it can be used later inside the code.
Example:
Assignment
Assignment: A type of programming statement that stores data in a variable and constant.
Rules for IDENTIFIERS
Casting: Converting data from one data type to another data type.
Example : Number ← int(“123”) #converting a string to an integer
Value ←String(22.4) # converting a number to an string
Conversion using Python
number = int(“123”)
value = str(22.4)
8.3 INPUT AND OUTPUT
INPUT OUTPUT
• The user entering data into the program, usually from a • Data that is displayed to the user usually on-screen.
keyboard.
• Used to give information to user.
• Used to take information from user.
• Example :
• Example:
Name ← “Alex”
INPUT Name
OUTPUT (“Hello”, Name)
OUTPUT (“Hello”, Name)
Using python:
Using python:
name = “Alex”
name= input("Enter your name")
print(‘Hello', name)
print(‘Hello', name)
Concatenation
• A symbol that performs mathematical function, i.e, ‘+’ adds two values.
Number1 ← 10
Number2 ← 5
Result1 ← Number1 + Number2
Result2 ← Number1 - Number2
Result3 ← Number1 * Number2
Result4 ← Number1 / Number2
Result5 ← DIV(33 , 7)
Result6 ← MOD(33 , 7)
MOD AND DIV
Addition operation
Subtraction operation
multiplication operation
division operation
DIV operation
MOD operation
OUTPUT
PARENTHESES
Count controlled
IF Statement, loop
Pre-Conditioned
CASE Statement loop
Post-Conditioned
loop
8.5 SEQUENCE
written.
program.
Example:
OUTPUT( “Enter your favorite color”)
INPUT Colour
OUTPUT( “Enter your name”)
INPUT name
OUTPUT(name, “ your favorite color is” ,
Colour)
8.6 SELECTION
The command IF is followed by a condition that is created using the logic operators. There are three stages of IF statements : IF
, ELSE and ELSEIF . Example:
INPUT NUM1
INPUT NUM2
What can be the relation between NUM1 and NUM2 ?
← 10
1. Num1
IF Num1 =10
THEN
OUTPUT(‘true’)
ENDIF
2. INPUT value_input
stored_value ← 100
IF value_input > stored_value
OUTPUT(‘ it is more than
100)
ENDIF
STRUCTURE OF IF..ELSE STATEMENT
INPUT Value_input
Stored_value ← 100
ELSEIF b> c
OUTPUT(b , “ is maximum”)
ELSE
OUTPUT(c , “ is maximum”)
ENDIF
SELECT CASE
• A SELECT CASE statement allows the program to take one variable or value, and then have lots of different
options depending what it is equal to.
• A case can have as many CASE statements as needed, but can only ever have a maximum of one default(this runs
if none of the comparisons is true) statement.
CASE follows the structure:
CASE OF variable
value1:
Statements that run if the CASE value1 is true
value2:
Statements that run if the CASE value1 is false, valeu2 is true.
Otherwise
Statements that run if none of the comparisons are true.
ENDCASE
Flowchart of CASE statement
B O O L E A N O P E R ATO R
There are three types of loops: Pre- condition loop While…endwhile loop
Ans:
i← 0
Total ← 0
FOR i ← 1 to 20
Total ← sum + i
NEXT i
OUTPUT Total
Example: Write a pseudocode
Take numbers as input and find positive and negative numbers
using Count-Controlled Loop.
ANS:
i← 0
FOR i ← 1 to 10
INPUT a
IF a >= 0
OUTPUT( a, “is Positive”)
ELSE
OUTPUT( a, “is Negative”)
ENDIF
NEXT i
Take 10 numbers as input and find odd and even numbers using
count-controlled loop.
ANS:
i← 0
FOR i ← 1 to 10
INPUT a
IF a MOD 2 = 0
OUTPUT (a, “ is even”)
ELSE
OUTPUT (a, “ is odd”)
ENDIF
NEXT i
PRE-CONDITIONED LOOP
WHILE condition DO
ENDWHILE
EXAMPLE: Outputting the numbers 1 to 10
• Number ← 1
WHILE Number < 11
OUTPUT(Number)
Number ← Number + 1
ENDWHILE
EXAMPLE
Write a pseudocode to find the total of this series using Pre-
Conditioned Loop:
1+2+3+4+……………20.
Ans:
i← 0
Total ← 0
WHILE i <=20
Total ← Total + i
i←i+1
ENDWHILE
OUTPUT Total
Take 10 numbers as input and find odd and even
numbers using Pre-Conditioned Loop.
REPEAT
UNTIL Condition
EXAMPLE
Write a pseudocode to find the total of this series using Post-Conditioned
loop:
1+2+3+4+……………20.
Ans:
i← 0
Total ← 0
REPEAT i <=20
Total ← Total + I
i←i+1
UNTIL i >= 20
OUTPUT Total
Take 10 numbers as input and find odd and even
numbers using Post-Conditioned Loop.
What is a string ?
• Any characters, numbers or symbols that are found within a double or single
quotation mark can be identified as a string. For example: “ Hello” , ‘123’,
“as@hhj”.
Example:
SUBSTRING(“Hello”, 0,1)
using python:
Substring = “Hello”[0:1]
Print(Substring)
Ending
Starting char
char
3. Upper and Lower: the characters(A-Z
and a-z) of a string can be converted from
upper case to lower case and vice-versa.
It follows the structure:
UPPER( string )
LOWER( string )
Example:
Using UPPER with a string :
UPPER(“Hello”)
Using LOWER with a string :
LOWER(“Hello”)
Using UPPER with a string stored in variable:
Word ← “hello”
Word ← UPPER( “hello”)
8 . 1 1 N E S T E D S T A T E M E N T S
An IF
A loop inside An IF
statement A loop within a
an IF statement statement in a
inside an IF loop
, or loop ,or
statement , or
Example1: Count how many numbers are more
than 10, and how many are equal to 10
MoreThan10 ← 0
Equalto10 ← 0
For X ← 0 to 5
OUTPUT(“Enter a number”)
INPUT Number
IF Number > 10
MoreThan10 ← MoreThan10 +1
ELSEIF Number = 10
Equalto10 ← Equalto10 +1
ENDIF
NEXT X
1. Procedure: A procedure
runs the code inside it and
does not return a value to
the program that called it.
The structure of a procedure is:
PROCEDURE identifier()
code to run inside the function
ENDPROCEDURE
2. FUNCTION
Function name
A procedure runs the code
inside it and returns a value
using the Return command to
the program that calls it. Returning product
as value
The structure of a procedure is:
FUNCTION identifier()
code to run inside the function
RETURN value
ENDFUNCTION
Returning value
Scope: The scope of a variable is the areas within a program where the variable can be accessed.
Main program
8.13 Library routines
For example, MOD and DIV (slide10) are library functions in some language.
Instead of “x”,
“total” is used as an
identifier and it
indicates that it
stores the total of
two numbers.
tes
Here, “#” indica
lls
a comment and te
to
the program not
execute the text
after the symbol
How an identifier can store multiple data of same datatypes?
0 1 2 3 4
• This array has 5 spaces that can be traced using their respected index numbers.
index 0 1 2 3 4
Data “red” "green" "blue" "yellow" "black"
• An array can have one dimension, two dimensions, three dimensions, four dimensions, and so on.
1-dimensional array
• This array has 5 spaces. The first data item is in position 0 and the data value is 10.
• Array use brackets after the identifier to indicate the index we want to access. For example , Array[0] is accessing the first
element in the array named Array.
• In array named Numbers,
Numbers[0] = 10
Numbers[1] = 5
Numbers[2] = 90
Numbers[3] = 26
Numbers[4] = 87
2-dimensional array
temperature
0 1 2 3
Temperature = [[11, 12, 5, 2], [15, 6,10 , 4], [10, 8, 12, 5], [12,15,8,6]]