Computer Science P2
Computer Science P2
Q : write an
algorithm for baking a cake .
*
get all the
ingredients -
• mix 3
tablespoons of sugar into it .
• beat 10 min .
in
•
if cake is still .
•
raw
•
then microwave for 5 more min .
Q write
: an
Algorithm for adding
3 numbers
together .
7 >
4 5
Pseudocode
{
start
"
Hello World
"
Print
End
•
start
"
.
Print
"
Enter first number :
Input numb
"
.
Input num2
.
Sum 8- numb + mum 2
Print Your is !
"
. sum , sum .
End
|
/ 3 5 8
I
3
+ +
£ < =
- -
✗ * ⇐
÷ 1
ab
^
AND
OR
( ) ( )
NOT
Assignment
start
numb ← 10
|n¥
2 33
mum ←
iiiz 's!!
sum ← num1+num2 + too
print sum
End
Conditional statement
•
if É
then
end
if
Q: Tell
if pass or fail input , marks from user
.
Start
"
Please
"
Print enter marks
your
:
marks
Input
if (marks >= 75 )
then
"
Print passed with good marks!
"
You
else if ( marks > 50) =
then "
Print " fair
you passed
with mairks!
else
You Failed !
"
"
Print
end if
End nestedifstaenent
Case statement
marks
case
of
"
90 : output ''
A *
"
output
"
270 : C
"
760 : output
"
D
"
3 50 : output KE
"
< 50 : Output of
Otherwise
"
Output
"
Enter valid marks
End case
LOOPS
• FOR
-
loop ← runs
for a
specific
amount time
of .
Repeat -
Until loop ←
based on condition
For c ← 1 TO 10
EI
f. output c
c- 2
End for
123456789102
while marks↳#- ←
pre
Éntw marks
condition
End while
I
Repeat
: ,
¥1 MarkI Post
condition
Datatypes
Integers :O , 1
,
10 , 986 ,
-192
Real : 0.1 ,
9.36 ,
- I -2 1.0
,
Char : A ! O
g g
String
" "
" " "
!* # Hello
"
Haifa 2
:
, ,
operation on them as
specified by
tee user .
Start
declare numb
,
num 2 ,
ans as real
declare operator as char
" "
numb
Input
Input mum 2
"
Output
"
Please enter operator :
Input operator
If
' '
operator = +
' '
Elséff Operator
-
'
ElseIf
'
operator = *
Else "
output please
"
enter valid operator
" "
End
case
of operator
' '
t ; ans = numb 1- mum 2
C- '
: ans = numb - num2
' '
* : ans = numd * Mumm
/
'
'
/ : ans = numb mum 2
end case
Flow charts
start / End
Input / Output
process
condition
.
Q Take two numbers from the user
and add them , show the result .
Start
Real mum
1) num2,5hm
Inputnumb,num2
1
Output sum
1
End
Q : Tell
if a standard is pass or
fail
his / her marks
depending on .
Start
int M
V
.
input M
.
15
" "
§
yes
✓
/Outputiiyoupassed
<
Eid
start
for e= I to 10
" "
Output *
End for
End
start
<
repeat "TEtimes
^ ¥-7.0
Y
Outpolling
,
End
}
d
Is
NO
Age 218 > Output"Adult
"
omñ→]
Yes
⑤
Input PercentageMarks
÷
P.M < 0
OR
FM 7 1 00
xes→y
"/
Output Invalid Masks
"
?
ND
IS
f- p.ms
?
49
" "
Output Fail
µ es
⇐I£
-
"
Pass
"
End
Q :
keep
until
taking inputs from
valid
the user
he enters marks ?
start
✓
✗input marks /
r-e☒i
t
Is
marks =
80
Input marks
•
While - end while
•
Repeat until
Repeat mark -_
48
Input marks
Until
(maxks > 0 AND marks < 100
)
Fori=3-o5-
2=5
output i
3,#
End
for
☐"
Pseudocode Syntax
Variable declaration
Assignment statement
a ← btc
eg :
Displaying message
"
output
"
any message
"
Output
"
"
Output
"
Hello world !
Output a
Hello world !
"
Output
"
,
a
Taking Input
< variable name >
-
input -
eg :
Input numb
Selection statements
IF THEN
. .
- -
ELSE
END IF
} these statements
IF Block are
condition is true
in
executed
if
otherwise
they are skipped
IF marks 50 THEN
eg : =
passed
"
Output
"
You
ELSE
ELSE
" "
END IF END IF
IF < condition > THEN
ELSE
END IF
END IF