Programming Part 2
Programming Part 2
Author:Chibi Tinodaishe M
Cell : 0781081816
Algorithm
Is a step-by-step procedure for solving a problem.
it is a sequence of unambiguous instructions that can be carried out by a
computer.
Algorithms are used in all areas of computer science , including programming ,
data structures and algorithms
Example
Simple algorithm for finding the largest number in a list of numbers.
Pseudo-code
Is a way of describing an algorithm using a plain english language and some
programming concepts.
It is not meant to be executed by a computer, but rather to be a human
readable description of algorithm
Pseudocode is useful for planning and testing algorithms and for communicating
algorithms to others
Example
Pseudocode version of the algorithm for finding the largest number in a list of
numbers
Function find_largest_number(list)
Max_value=list[0]
For number in list :
If number > max_value :
Max_value=number
Return max_value
Flowchart
Is a visual representation of an algorithm
It is a diagrammatic way of showing the flow of logic of an algorithm.
It uses symbols and arrows to show the steps in the algorithm and the flow of
control between the steps .
Flowcharts are useful for understanding and communicating algorithms.
Example
Flowchart of the algorithm for finding the factorial of a given number.
START
READ N
I=1
Fact = 1
Is
I <= n
Print fact
Fact = fact X i
STOP
I=I+1
Comparison of algorithm , pseudocode and algorithm
1. The If Statement
2. The Select Case Statement
3. The Do While and Loop While loops
4. The For Loop
The IF Statement
− Every If statement must contain a then and an end if
− Example:
End If
1) >
2) <
3) =
4) >=
5) <=
6) <> (Not equal to)
Select Case Statement
The Select Case statement in Visual Basic is a control flow statement that allows you
to test a variable or expression against a list of values and execute different code
depending on the result.
How it works
The Select Case statement works by first evaluating the test expression.
If the test expression matches any of the values in the list of values, the code
that follows that Case statement is executed.
If the test expression does not match any of the values in the list of values, the
code that follows the Case Else statement is executed.
FORMAT
_expression
value1
code_to_execute_if_test_expression_matches_value1
value2
code_to_execute_if_test_expression_matches_value2
...
Else
code_to_execute_if_test_expression_does_not_match_any_of_the_other_values
EXAMPLE
Dim dayOfWeek As String
dayOfWeek = "Monday"
do {
// The body of the loop
} while (condition);
How it works
The Do While loop works by first executing the body of the loop. Then, it evaluates
the condition. If the condition is true, the body of the loop is executed again. If the
condition is false, the loop terminates.
Example
This loop will display a message box with the numbers 0 through 9.
A Loop While loop is a type of control flow statement that repeatedly executes a
block of code until a given boolean condition evaluates to false.
loop {
// The body of the loop
} while (condition);
How it works
The Loop While loop works by first evaluating the condition. If the condition is true,
the body of the loop is executed. Then, the condition is evaluated again. If the
condition is still true, the body of the loop is executed again. This process continues
until the condition evaluates to false, at which point the loop terminates.
Example
This loop will display a message box with the numbers 10 through
Simple Terms
A Do While loop will always execute the body of the loop at least once, even if
the condition is false.
A Loop While loop will only execute the body of the loop if the condition is true.
Format
The basic format of the Do While and Loop While loops is as follows:
Do While <condition>
// The body of the loop
End Do While
Do While and Loop While loops are both useful control flow statements that can be
used to repeat a block of code until a given condition is met. The main difference
between the two loops is that a Do While loop will always execute the body of the
loop at least once, even if the condition is false.
For Loop
A for loop is a type of control flow statement that executes a block of code a
specified number of times.
How it works
Then, it evaluates a condition. If the condition is true, the body of the loop is
executed. After the body of the loop is executed, the loop variable is
incremented by a specified amount.
The condition is then evaluated again. This process continues until the condition
evaluates to false, at which point the loop terminates.
Format
for I = 0 to 5
Name(I)
Next i
Simple terms
A for loop is like a counting loop. You specify a starting value for the loop
variable, an ending value, and an increment amount.
The loop will execute the body of the loop until the loop variable reaches the
ending value.
Conclusion
For loops are a powerful tool that can be used to repeat a block of code a specified
number of times. They are used in a wide variety of programming languages and
applications.