0% found this document useful (0 votes)
9 views23 pages

Algoritms 1-2 - JAVA - Introduction PART1

The document provides an introduction to programming concepts, focusing on algorithms, variable names, and control structures like if statements and loops. It includes examples of algorithms for basic arithmetic operations, conditional checks, and handling unknown input sizes. The content emphasizes the importance of clear variable naming and structured programming to simplify coding tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

Algoritms 1-2 - JAVA - Introduction PART1

The document provides an introduction to programming concepts, focusing on algorithms, variable names, and control structures like if statements and loops. It includes examples of algorithms for basic arithmetic operations, conditional checks, and handling unknown input sizes. The content emphasizes the importance of clear variable naming and structured programming to simplify coding tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

ITSP 121

Introduction to programming
Algorithms

• An algorithm is a step-by-step method for solving a specific


problem or doing a task.

• Writing the algorithm is always the first step of any


programming task.

• With a proper algorithm, the programming is simplified


tremendously.
Algorithms

• We use the keywords

– STOP to indicate the end of the algorithm

– READ to obtain input from the user

– WRITE to display output to the user


Variable names

• We use variable names to reference addresses in memory that will


be used in a program.

• Variables can store different types of data, i.e. integers, real


numbers, characters, text, etc.

• Always ensure that your variable names are as descriptive as


possible. For example LargestNumber instead of just Number, or
Name instead of Input1.

• Variable names cannot contain spaces, and cannot start with any
numbers or special characters (&,$,@,%, etc.).
Variable names

• Before starting an algorithm always ask yourself,

– what input is required

– what needs to happen to this input

– what output is expected.


Hello world

• The first program a first-time programmer will write


is famously known as “Hello world”.

NB:
• Bear in mind that java is a case sensitive.
• Every java line of code ends with a semi-colon ‘;’
Example 1

• Write an algorithm that will determine and display the


product of 2 numbers.

READ Number1
READ Number2
Total = Number1 * Number2
WRITE Total
STOP
Example 1

• Write an algorithm that will determine and display the


product of 2 numbers.
Example 2

• Write an algorithm that will determine and display the sum


of first 2 assignment marks.
READ AssignmentMark1
READ AssignmentMark2
Sum = AssignmentMark1 + AssignmentMark2
WRITE ‘The sum of the assignment marks are‘, Sum
STOP
Example 2

• Write an algorithm that will determine and display


the sum of first 2 assignment marks.
The if statement
• The if statement is used to determine whether a single
statement or group of statements should be executed or
not.
• Its structure is as follows:
If (Condition) Then
Statement
Statement
Else
Statement
Statement
EndIf
The following operators can be used in the Condition.
<,<=,>,>=,=,!=
Example 3

• Write an algorithm that will determine the sum of two


assignment marks. If this sum is above 20 it should
display high, otherwise it should display low.

READ AssignmentMark1
READ AssignmentMark2
Sum = AssignmentMark1 + AssignmentMark2
If (Sum > 20) Then
WRITE ‘High’
Else
WRITE ‘Low’
EndIf
STOP
Example 3

• Write an algorithm that will determine the sum of two


assignments. If this sum is above 20 it should display
high, otherwise it should display low.
Example 4

• Write an algorithm that will receive the colour of a ball as


input from the user. If the colour is yellow it should display
“yellow”, otherwise it should display “green”.
READ colour
If ( colour = ‘yellow’) Then
WRITE ‘Colour is yellow’
Else
WRITE ‘Colour is green’
EndIF
STOP
Example 4

• Write an algorithm that will display the colour of balls . If


the colour is yellow it should display “yellow”, otherwise it
should display “green”.
Repetition – the While loop
• The while loop is used when we know that a statement or
group of statements will have to be repeated for an
unknown number of times.
• The structure of the while loop is:
While (Condition) Do
Statement
Statement
Statement
EndWhile
• Very important: Remember that statements can be
combined as well. For example you might have an if
statement inside a while loop, or you can have a while loop
inside an if statement, etc.
Example 5
• Write an algorithm that will determine and display the total number of
assignments handed in (there are an unknown number of
assignments). Use the assignment mark as input. Assume that a
negative number will end the input of all assignments.
Example 5
• Write an algorithm that will determine the total number of
assignments handed in (there are an unknown number of
assignments). Use the assignment mark as input. Assume that a
negative number will end the input of all assignments.

READ AssignmentMark
Counter = 0
While (AssignmentMark >= 0) Do
Counter = Counter + 1
READ AssignmentMark
EndWhile
WRITE Counter
STOP
Example 6
• Write an algorithm that will determine and display the average mark
of the assignments handed in (there are an unknown number of
assignments). Assume that a negative number will end the input of all
assignments.
Example 6
• Write an algorithm that will determine and display the average mark
of the assignments handed in (there are an unknown number of
assignments). Assume that a negative number will end the input of all
assignments.
READ AssignmentMark
Total = 0
Counter = 0
While (AssignmentMark >= 0) Do
Total = Total + AssignmentMark
Counter = Counter + 1
READ AssignmentMark
EndWhile
Average = Total / Counter
WRITE Average
STOP
Exercise 7
• Write an algorithm that will determine the highest and lowest assignment mark
between an unknown number of assignments. Assume that a 0 will end the input of
assignments. The algorithm should also display the highest and lowest marks obtained
for the assignment at the end.
READ AssignmentMark
Highest = AssignmentMark
Lowest = AssignmentMark
While (AssignmentMark != 0) Do
If (AssignmentMark > Highest) Then
Highest = AssignmentMark
Else
If (AssignmentMark < Lowest) Then
Lowest = AssignmentMark
EndIf
EndIf
READ AssignmentMark
EndWhile
WRITE ‘The highest is ‘, Highest ,’ and the lowest is ‘, Lowest
STOP
Exercise 7
• Write an algorithm that will determine the highest and lowest assignment mark
between an unknown number of assignments. Assume that a 0 will end the input of
assignments. The algorithm should also display the highest and lowest marks obtained
for the assignment at the end.
Conclusion

• Any questions?

You might also like