Syntax
Syntax are rules for defining the legal sequence of a symbolic element in
a language.
Programming as a language has its building blocks. These are reserved
words and keywords.
reserved words
These are word that have specific role in the context in which it occurs,so
it can not be used for other purpose. Example: IF,THEN, ELSE.
Keywords
This is a symbol in a programming language that have special meaning.
EXAMPLE
PRINT,IF
Pseudocode
Is one of the tools that can be used to DISPLAY preliminary plan which later can be
turn into a computer program.
Take note that pseudocode is note a standard language. It purpose is to describe algorithm
Algorithm
This is step by step method of solving a problem.
In this there will be use of diagrams to represent the algorithm
First problem
Lets create a pseudocode for a program that will input two numbers from the
keyboard and calculate there sum and print the result.
Start
Enter
numbers
Calculate
the sum
Print the
answer
end
• Indeed we have enter the numbers ,but will the number just hang in space?
there is a need for a method to store it.
• There must be a method of accepting data and storing it in computer memory.
• The computer also need to know the location of the stored number. So there is
need of a variable
What is a variable
This is memory location which computer uses to store data whenever the
program is running
This is computer memory which computer uses to store data when program
is running
Variable has three attributes
• The address in a memory where the data is stored
• The actual data itself
• The name of the variable the identifier
In our problem above the pseudocode can be written as
ACCEPT number1,number2,number3
Sum=number1+number2+number3
PRINT sum
end program
COMPILER
this is a program that translate high level language into absolute code.
The compiler needs to know type of data being used since it may have
different ways of dealing With integers,franctions,string and characters.
Types of variables
There are different types of variables
• Whole numbers-integers
• Fraction number=real or float
• Letters=characters
• Names=strings
• False or true/1 or 0=Boolean
Our pseudocode is now
Use variables: number1,number2,number3,sum OF TYPE int
DISPLAY “Accept number”;
ACCEPT number1.number2,number3
sum=number1+number2+number3
PRINT “the sum is “ sum
end program
Let us now look at the real code
#include<iostream>
using namespace std;
int number1,number2,number3,sum;
int main(){
cout<<"enter number 1"<<endl;
cin>>number1;
cout<<"enter number 2"<<endl;
cin>>number2;
cout<<"enter number 3"<<endl;
cin>>number3;
sum=number1+number2+number3;
cout<<"the sum is "<<sum<<endl;
return 0;
}
Problem to solve
DISPLAY a pseudocode to solve the following problem:
Input two decimal number and calculate and print the sum and product of the
numbers. Provide the text prompt for the input data and text description of the
output provided. Remember
CONSTANT
This is type of a variable that does not change
CONTROL STRUCTURE
Syntactic form in a language to express to express flow of control.
Common control structure are
IF…..THEN…ELSE WHILE…DO REPEAT…..UNTIL
CASE
SELECTION AS CONTROL STRUCTURE
There are many accusation where a program need to include range of
Alternative Actions.
There are different types of selections.
Start
Input
numbe
Number=0
Calculate
100/number
Print ans
Print
invalid
entry
End program
This is example of if statements
If statements use the logical operators to make sense.
The aim is to test if a certain condition is true or false
EXAMPLE
IF(A=1) THIS translate into if a is equal to one
NOTE: this is pseudocode thus why = is translated as is equal to.
But different programming languages translate this as assigned to.
>=this means greater than or equal to
<=less than or equal to
>greater than
<less than
PROBLEM
What things will change if we want to include division?
NOTE: there three main types of instructions used in programming
• Selection
• Sequence
• loops
What we have been doing so far is what is called sequence. The command are executed in
order
Now lets do selection
CONTROL STRUCTURE CONT
There are times when we offer a user with a bunch of choices.
For example if a user is offered to use these characters
• M for multiplication
• A for addition
• S for subtraction
The user should input three numbers
Upon each choice he program should perform valid functionality
To solve this we use IF THEN statement
Use variable: choice OF TYPE Character
answer,number1,number2 OF TYPE Integer
DISPLAY ”chose the following”
DISPLAY “m for multiply”
DISPLAY “a for add”
DISPLAY “s for subtract”
ACCEPT choice
DISPLAY ”input the first number”
ACCEPT number1
DISPLAY ”input the second number”
ACCEPT number2
IF choice=m
THEN answer=number1*number2
ENDIF
IF choice=a
THEN answer=number1+number2
ENDIF
IF choice=m
THEN answer=number1*number2
ENDIF
DISPLAY answer
End program
Using if then statement the statements can become complex to a computer
So there is a need of simplifying them.
To simplify the if then statement we use if….then….else
Binary choice on a given Boolean condition is indicated by the use of four
keywords:
IF, THEN, ELSE, and ENDIF. The general form is:
IF condition
THEN
sequence 1
ELSE
sequence 2
ENDIF
DISPLAY "Temperature of water?
"
ACCEPT Temp
IF Temp <= 0 THEN
DISPLAY "It's frozen"
IF Temp <= 12 THEN
DISPLAY "It's cold"
ELSE
IF Temp <= 25 THEN
DISPLAY "It's warm"
ELSE
IF Temp <= 75 THEN
DISPLAY "It's hot"
ELSE
IF Temp <= 100 THEN
DISPLAY "It's very hot"
ELSE
DISPLAY "It's burning"
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
Case statement(selection type)
Repeating if…then…else statement can sometimes be confusing
A cleaner and easy to follow is to use a method that covers that
specifically covers a set Alternative conditions that are needed.
In the pseudo code this will be called case statement
Use variables: age OF TYPE Integer
category OF TYPE Character
ACCEPT age
DO CASE of category
CASE category=u
PRINT “Insurance=not available”
CASE category=a
PRINT “Insurance=double”
CASE category=b
PRINT “Insurance=normal”
CASE category=m
PRINT “Insurance=medically dependent”
OTHERWISE PRINT “entry is invalid”