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

Partial Crash Course Booklet

The document provides an overview of programming concepts such as selection statements (IF and CASE), logical and relational operators, loops (FOR, WHILE, REPEAT), and arrays, including their syntax and usage. It includes practice questions for applying these concepts in pseudocode. Additionally, it covers file handling modes (READ, WRITE, APPEND) and provides pseudocode examples for file operations.
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)
24 views69 pages

Partial Crash Course Booklet

The document provides an overview of programming concepts such as selection statements (IF and CASE), logical and relational operators, loops (FOR, WHILE, REPEAT), and arrays, including their syntax and usage. It includes practice questions for applying these concepts in pseudocode. Additionally, it covers file handling modes (READ, WRITE, APPEND) and provides pseudocode examples for file operations.
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
You are on page 1/ 69

Crash Course Booklet

P2 Crash Course
Selection Statement
Selection statements are programming constructs that allow
the execution of certain blocks of code based on conditions.

IF ...........THEN..........ELSE..........ENDIF
CASE.......OF..........OTHERWISE.........ENDCASE

Syntax Of IF Statement
IF statements may or may not have an ELSE clause.
IF statements without an else clause are written as follows:

IF <condition> THEN
<statement(s)>
ENDIF

IF statements with an else clause are written as follows:

IF <condition> THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF
Logical Operators
AND OR
Both Condition Any one Condition
Should Be True Should Be True

Relational Operators
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to

The result of these operations is always of data type


BOOLEAN. In complex expressions it is advisable to use
parentheses
Syntax Of CASE Statement
CASE statements allow one out of several branches of code
to be executed, depending on the value of a
variable.

CASE statements are written as follows:

CASE OF <identifier>
<value 1> : <statement1>
<statement2>

<value 2> : <statement1>


<statement2>
ENDCASE

An OTHERWISE clause can be the last case:

CASE OF <identifier>
<value 1> : <statement1>
<statement2>

<value 2> : <statement1>


<statement2>

OTHERWISE : <statement1>
<statement2>
ENDCASE

Each value may be represented by a range, for example:

<value1> TO <value2> : <statement1>


<statement2>
Practice Question 1
Write a pseudocode of a program in which Input
three numbers and print the Largest Number
Practice Question 2
Write a pseudocode algorithm that asks the user to
enter a number and checks whether it is positive,
negative, or zero.
Practice Question 3
Write a pseudocode algorithm that prompts the
user to enter a password. If the password matches
"Open123", output "Access Granted"; otherwise,
output "Access Denied".
Practice Question 4
Write a pseudocode algorithm that asks the user to
enter a numerical grade (1-7) and outputs the
equivalent grade description:
7: "Excellent"
6: "Very Good"
5: "Good"
4: "Satisfactory"
3: "Pass"
2 or 1: "Fail"
Practice Question 5
Write a pseudocode algorithm that takes an integer as
input and determines whether the number is even, odd
Built in Function
ISNUM() And String Comparison

ISNUM() is basically a built in function that takes


string value as a parameter and return true if that
string value are Numbers
In pseudocode you can compare the strings for eg
“Z” > “A”

Character >= “A” and Character <= “Z”

Practice Question
There are Certain Conditions for a password to be
correct

1) First Digit Should Be Capital


2) 2nd, 3rd, 4th Digit Should be Numbers
3)Length Of the Password Should Be 9

Write a pseudocode of a program in which take


input the password and print "Approve” If these
Conditions are met and print "Re write Password"
If not

Note : First think how you are going to solve this


Problem. Imagine and write Correct Value and
Expected output this will help you in thinking
about the solution
Solution
Loops
Count-controlled (FOR) loops Post-condition (REPEAT) loops Pre-condition (WHILE) loops

FOR LOOP
FOR <identifier> ← <value1> TO <value2>
<statement(s)>
NEXT <identifier>

Practice Question
Input One Name and then Output “Good Morning”
23 Times concatenated with the Name
Repetition Known
The identifier must be an INTEGER variable.
No condition
Fixed Loops and will always execute
Values must evaluate to integers.
The loop runs from value1 to value2 inclusive.
Statements inside the loop execute for each assigned
value.
If value1 = value2, the loop runs once.
If value1 > value2, the loop does not execute.

FOR <identifier> ← <value1> TO <value2> STEP <increment>


<statement(s)>
NEXT <identifier>

The increment must evaluate to an integer.


The identifier starts at value1 and increases by the
increment.
The loop runs until the identifier reaches or exceeds
value2.
If the identifier goes past value2, the loop ends.
The increment can be negative.
WHILE LOOP Pre Condition

WHILE <condition>
<statement(s)>
ENDWHILE

The condition must evaluate to a Boolean.


The condition is tested before executing the
statements.
Statements run only if the condition is TRUE.
After each execution, the condition is tested again.
The loop stops when the condition is FALSE.
If the condition is FALSE initially, the statements are
not executed
REPEAT LOOP Post Condition

REPEAT
<statement(s)>
UNTIL <condition>

The condition must evaluate to a Boolean.


The statements are executed at least once.
The condition is tested after the statements run.
If the condition is TRUE, the loop terminates.
If the condition is FALSE, the statements run again.
Practice Question 1
Input 500 Names and Print in the
format "Hello Name" and Prompt the user aswell
PROGRAMMING TECHNIQUES
1) SUM TECHNIQUE
SUM <-- 0 (Outside the loop)
SUM <-- Sum + Number (Inside the loop)

2) Counting TECHNIQUE
Count <-- 0 (Outside the loop)
Count <-- Count + 1 (Inside the loop)

Practice Question 2
Input 10 Numbers and Print the
sum of those 10 Numbers
Practice Question 3
Input 500 Numbers and print
how many numbers are Positive
Practice Question 3

Input 499 and print how many number are positive


, negative and zero with a suitable message
Flag Looping

Whenever we need to stop the loop or to make the


code efficient we use a flag and conditional loops

Flag <-- TRUE


WHILE Flag = True
INPUT Response
IF Response = "No" THEN
Flag <-- FALSE
ENDIF
ENDWHILE

Practice Question 4
Condition for a password to be correct is that first
digit should be capital

Write Pseudocode in which take input again and


again until the Password is correct and print
approve if it is correct and if it is wrong print re
write password and take input again
Practice Question 5
Take Numbers as input again and again until the user
types in 0 and find the average of those Numbers
Practice Question 6

Take numbers as input repeatedly until the user


types in -1. Count how many of those numbers are
even and display the count when the loop ends.
Hint : Use Built-in Function
Solution For Even Numbers
Solution For Odd Numbers
Array
Upper
6 "Pappan"
Bound
5 "Banto"
4 "Bano"

3 "Pappu"

2 "Ahmed"
Lower
Bound 1 "Taha"

Names

What Is Array ?
Arrays stores multiple values of same datatype and
the data is stored on position known as index

How to access Individual Element in an array

5 "Pappan"

4 "Banto"
Names[3] "Bano" 3 "Bano"
Names[0] "Taha"
2 "Pappu"
Names[4]
1 "Ahmed"
Names[5]
0 "Taha"

Names

How to Declare the Array


DECLARE NameOfArray : ARRAY [ LowerBound : UpperBound ] OF DataType
Practice Question 1
Find Specific Characters in a String Array: Given an array
userNames with 200 elements, each containing a username.
Write a program to output the first and last character of each
username. Additionally, count and print the number of
usernames that are less than 5 characters long.
Practice Question 2
Search and Modify in a Numeric Array: There is an array
accountBalances with 1000 elements representing various
bank account balances. Write a code to increase each
balance by 10% if it is below $1000. At the end, print the
total number of accounts that had balances below $1000.
Practice Question 3
Search for Elements Meeting a Condition: In an array
temperatureReadings with 365 elements, each representing
the daily maximum temperature of a year, write a program to
find and print all days where the temperature was above
30°C. Also, count how many such days were there.
Practice Question 4
Finding and Replacing in an Array of Sentences: You have an
array sentences with 150 elements, each a string containing
a word. Write a program to replace the word "happy" with
"joyful" in every element. Also, count how many elements
contained the word "happy".
Practice Question 5
Searching for Null or Empty Elements: An array
emailAddresses contains 300 elements with email
addresses. Some elements might be null or empty strings.
Write a program to print all valid email addresses and count
how many elements were null or empty.
2 Dimensional Array
A 2D array is a data structure that stores elements
in a grid or matrix of rows and columns. It consists
of multiple rows and columns, where each element
is uniquely identified by its row and column values.
2D arrays are commonly used in programming to
represent images, screens and game boards

Create an Array with 5 rows and 4 Columns


1 2 3 4
1

Declaration Of 2D Array
ROWS COLUMN

DECLARE NameOfArray : ARRAY [ L.B : U.B, L.B : U.B ] OF DataType

DECLARE Numbers : ARRAY [ 1 : 5 , 1 : 4 ] OF INTEGERS


Practice Question
Declare an array of datatype String (Names) of 545
rows and 250 columns

DECLARE Names : ARRAY [ 1 : 545 , 1 : 250 ] OF INTEGERS

Concept Of Nested Loops

Accessing The Individual Element


1 2 3 4
Number[1,1] 21 1 21
Number[3,3] 5
2
Number[5,2] 78
3 5

5 78
Practice Question 1
Construct An Array with the name StNames which have 200
rows and 500 columns and Initialize each Elements
Practice Question 2
Construct An Array with the name Number which have 150
rows and 238 columns and Initialize each Elements
Practice Question 3
There is an array StNames with 125 rows and 235 columns
which contains the name of Students but there are certain
Elements which are empty. Count how many elements are
empty.
Practice Question 4
There is an array StNames with 125 rows and 235 columns
which contains the name of Students. Search if there is
“Bano“. Output Found if there and Not Found if not there
Practice Question 5
There is an array Numbers with 125 rows and 235 columns
which contains the profit earned each day. Calculate the
sum of the profits.
Practice Question 6
There is an array Job with 129 rows and 289 columns which
contains salaries of Employees. Increase the salary of each
Employee by 12%.
Practice Question 7
There is an array Student with 129 rows and 2 columns. First
Columns Contains the Name of Students, The second
Column contains the Number of Days they were Present in
string format. Output the percentage of attendance of Bano
considering she was suppose to attend 180 Days.
Practice Question 8

2. Output “Burned Out” if the resultant Image is Burn Out


Practice Question 9
Files
The purpose of file is to store data permenantly

Three Modes In File

READ MODE WRITE MODE APPEND MODE


Read mode Write mode Append
is used is used mode is
when you when you used when
only want to want to you want to
read data write data write data
from to a new to an
existing file file existing file

Write Mode
Writing to a text file means Creating a text file.
All the previous data would be deleted and a file
will be written from scratch
Pseudocode For Writing

OPENFILE "FileName.txt" FOR WRITE


WRITEFILE "FileName.txt" , "String"/Variable
CLOSEFILE "FileName.txt"

Append Mode
Sometimes we want to add data to an existing file
rather than creating a new file.
Append Mode adds the new data to an existing file

Pseudocode For Append

OPENFILE "FileName.txt" FOR APPEND


WRITEFILE "FileName.txt" , "String"/Variable
CLOSEFILE "FileName.txt"
Read Mode
Whenever you want to search something from an
existing file we use Read Mode

Pseudocode For Read


OPENFILE "FileName.txt" FOR READ
READFILE "FileName.txt" , Variable
CLOSEFILE "FileName.txt"

READS A LINE AND STORED ALL THE DATA IN VARIABLE


Practice Question 1
Create a new file with the name “Stname.txt” and store all
the data which is in the array named “Student” and contains
456 Elements
Practice Question 2
Create a new file with the name “Stname.txt” and store the
data which is in the array named “Student” and contains 456
Elements but there are certain Elements which are Empty so
avoid them.
Practice Question 3
There is an existing file with the name “Stname.txt” and add
more data in it from the array named “Student” which
contains 456 Elements
Practice Question 4
Write pseudocode for a program that reads from an Array
called "Transactions" which has 2000 elements. Filter out
any transactions that are flagged as "invalid" or "rejected"
and store the valid transactions in a existing file named
"ValidTransactions.txt".
Practice Question 5
There is a file named “Profit.txt” which contains daily profit.
Calculate the total profit and Output with a suitable message
Practice Question 6

Take a Student ID as input from the user. For each element in


the array that matches the Student ID given by the user

add the value of the array Element to the existing file


“Logfile.txt”
assign an empty string to the Array element.

Count the number of lines added to the file


Output Count

You might also like