0% found this document useful (0 votes)
28 views9 pages

Programming Part 2

This document discusses algorithms, pseudocode, flowcharts, and control structures used in programming. It provides examples and comparisons of algorithms, pseudocode, and flowcharts. It also explains loops and control structures like if statements, select case statements, do while loops, loop while loops, and for loops. Key points covered include the definition of algorithms, pseudocode, and flowcharts. Examples are given for finding the largest number in a list and calculating a factorial. The use and format of if statements, select case statements, do while loops, loop while loops, and for loops are described.

Uploaded by

shammahzuze05
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)
28 views9 pages

Programming Part 2

This document discusses algorithms, pseudocode, flowcharts, and control structures used in programming. It provides examples and comparisons of algorithms, pseudocode, and flowcharts. It also explains loops and control structures like if statements, select case statements, do while loops, loop while loops, and for loops. Key points covered include the definition of algorithms, pseudocode, and flowcharts. Examples are given for finding the largest number in a list and calculating a factorial. The use and format of if statements, select case statements, do while loops, loop while loops, and for loops are described.

Uploaded by

shammahzuze05
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/ 9

PROGRAMMING PART 2

[These are programming simple notes.]

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.

1. Set the maximum value to the first number in the list


2. Iterate over the list, comparing each number to the maximum value.
3. If a number is greater than the maximum value, set the maximum value to that
number.
4. Return the maximum value

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

Characteristic Algorithm Pseudocode Flowchart


Level of abstraction Most` abstract Less abstract Least
abstract
Form of expression Natural language Natural language and Symbols and arrows
programming
concepts
Purpose To solve a problem To describe an To visualize an
algorithm in a algorithm
human readable way

Loops and control structures

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:

If Num1 < 2 Then


MsgBox.show(“The value is less than 2”)

End If

If Num1 <> 4 then


MsgBox.Show(“The value is not 4”)
End If

− The relational operators that can be used in Visual Basic are

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"

Select Case dayOfWeek


Case "Monday"
MsgBox("Today is Monday.")
Case "Tuesday"
MsgBox("Today is Tuesday.")
Case "Wednesday"
MsgBox("Today is Wednesday.")
Case "Thursday"
MsgBox("Today is Thursday.")
Case "Friday"
MsgBox("Today is Friday.")
Case "Saturday"
MsgBox("Today is Saturday.")
Case "Sunday"
MsgBox("Today is Sunday.")
Case Else
MsgBox("Invalid day of the week.")
End Select
Do While and Loop While loops

A Do While loop is a type of control flow statement that executes a


block of code at least once, and then repeatedly executes the block, or
not, depending on a given boolean condition at the end of the block.

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

The following example shows a simple Do While loop:

Dim counter As Integer = 0

Do While counter < 10


MsgBox(counter)
counter = counter + 1
Loop

This loop will display a message box with the numbers 0 through 9.

Loop While Loops

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

The following example shows a simple Loop While loop:

Dim counter As Integer = 10

Loop While counter > 0


MsgBox(counter)
counter = counter - 1
End Loop

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

Loop While <condition>


// The body of the loop
End Loop
Conclusion

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

 A for loop works by first initializing a loop variable.

 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

The basic format of a for loop is as follows:

for (initialization; condition; increment) {


// The body of the loop
}

 initialization: Initializes the loop variable.


 condition: The condition that is evaluated to determine whether or not to
execute the body of the loop.
 increment: Increments the loop variable by a specified amount.
Example

The following example shows a simple for loop:

for I = 0 to 5
Name(I)
Next i

This loop will print the numbers 0 through 9 to the console.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy