INTRODUCTION to Computational Thinking and Programming
INTRODUCTION to Computational Thinking and Programming
START / END
PROCESS
DECISION / Condition
DATA (Input/Output)
DOCUMENT
Start
Eg. of
FLOWCHART to
Input the first number (f) determine if the
first number is
divisible by the
Input the second number (s) second number.
Remainder = f%s
YES
Display “ the first no. is
divisible by the second no.”
STOP
PSEUDOCODE
Pseudocode is an informal way of describing the steps of a
program’s solution without using any strict programming language
syntax or underlying technology considerations.
It uses simple English and highlights the sequence of instructions.
Eg. Write the pseudocode to determine is the first number is divisible
by the second number or not.
Ans.
Input first number in variable firstnum.
Input second number in variable secondtnum.
Remainder= firstnum modulus secondnum
If the remainder is 0
display ‘the first number is divisible by the second number’.
else
display ‘the first number IS NOT divisible by the second number’.
DECISION TREES
Decision trees are a way of presenting rules in a
hierarchical and sequential structure, where based
on the hierarchy of rules, certain outcomes are
predicted. Eg.
MARKS DIVISION
>=60 First
50-60 Second
40-50 Third
<40 Failed
Eg. Decision
tree
Marks more than or
equal to 60
NO YES
Error prevents the program from compiling Fixing exception is tougher than fixing errors.
and running correctly. Exceptions are written in try and except
block.
Errors in a program can be fixed by making
corrections in the code. Some Built-in Exceptions are:-
1. EOF Error
Types of Errors:- 2. IOError
1) Compile time Errors 3. NameError
• Syntax Error 4. IndexError
5. ImportError
2) Runtime Errors 6. TypeError
3) Logical Errors / Semantics Error (In few 7. ValueError
of the programming Languages) 8. ZeroDivisionError
9. OverflowError
10. KeyError
Types of Error
Compile Time Error- Occurs during compile time. When a
program compiles, its source code is checked for whether it follows
the programming language’s rules or not.
Its types are:-
1. Syntax Errors : it occurs when a grammatical rule of the Programming
language is violated
Eg.
if a=>b # Instead of ==(relational operator) =>
print(a) #has been used, which is syntactically wrong.
#Moreover, : has not been given after the if condition.
Logical Error:
It occurs due to wrong logic of a program. We don’t encounter any error
during compile-time or run-time, but still program does not provide the correct
result. This is done by the programmer’s mistake.
Eg.
Trying to print the multiplication table , but instead of a*i, you have written a+i ,
which shows a wrong result by adding the nos. rather than multiplying.
Some Built – in Description
Exceptions
EOFError Raised when one of the built-in functions (input()) hits an end-of-file condition
(EOF) without reading any data.
IOError Raised when an I/O operation (such as a print(), the built-in open() function or
a method of a file object) fails for an I/O-related reason, e.g., “file not found” or
“disk full”.
NameError Raised when an identifier name is not found.
IndexError Raised when a sequence subscript or index is out of range, e.g., from a string of
length 4 if you try to read a value of index like 4 or more i.e., string[4], string[5],
string[-5] etc. will raise exception as legal indexes for a string of length 4 are 0,
1, 2, 3 and -1, -2, -3, -4 only.
ImportError Raised when an import statement fails to find the module definition or when a
from_import fails to find a name that is to be imported.
ZeroDivisionError Raised when the second argument of a division or modulo operation is zero.
OverflowError Raised when the result of an arithmetic operation is too large to be represented.
KeyError Raised when a mapping (dictionary) key is not found in the set of
existing keys.
HOW TO DEBUG A PROGRAM
• Debugging involves correction of code so that the cause of errors is
removed.
• Testing is done to verify correct behavior i.e., with some sample
values (test cases) whose output is already known, the code is
tested – if it produces the anticipated output, code is correct and if
it does not, there is some error. Once you know the errors, you can
debug your program.