0% found this document useful (0 votes)
21 views

Programming and Algorithms

Uploaded by

Temp Temp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Programming and Algorithms

Uploaded by

Temp Temp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8.

Programming and Algorithms

Programming and algorithms are fundamental components of


computer science and information technology. Understanding these concepts is crucial
for developing efficient software solutions and problem-solving skills.

8.1 Basic Concepts


8.1.1 Algorithm

 Definition: An algorithm is a step-by-step procedure or set of rules for solving a


specific problem or achieving a particular goal. Algorithms are the backbone of
programming, providing a clear and logical approach to problem-solving.
 Characteristics:

 Unambiguous: Each step of the algorithm must be clearly defined and


precise.
 Finite: Algorithms must terminate after a finite number of steps.
 Effective: Each step must be basic enough to be carried out, in principle, by
a person using only paper and pencil.

 Example: An algorithm to add two numbers:


1. Start
2. Input the first number (A)
3. Input the second number (B)
4. Calculate the sum (C = A + B)
5. Output the result (C)
6. End

8.1.2 Flowcharts

 Definition: A flowchart is a visual representation of an algorithm, using symbols


and arrows to illustrate the flow of control and the sequence of operations.
 Common Symbols:

 Oval: Represents the start and end of the flowchart.


https://zedicthub.blogspot.com/

1
 Rectangle: Indicates a process or instruction (e.g., calculation or
assignment).
 Diamond: Represents a decision point (e.g., if-else conditions).
 Parallelogram: Used for input and output operations (e.g., reading data or
displaying results).

 Example: A flowchart for the algorithm to add two numbers would include shapes
representing each step, connecting them with arrows to show the flow from start
to finish.

8.2 Programming Constructs

Programming constructs are fundamental building blocks used in programming


languages to create algorithms and control the flow of a program. The main constructs
include sequence, selection, and iteration.

8.2.1 Sequence

 Definition: Sequence refers to the execution of statements in a linear order, one


after the other. This is the simplest form of control structure, where each
statement is executed exactly once in the order they appear.
 Example

A=5
B = 10
C = A + B # C is now 15
print(C)

 Characteristics:

o Clear and straightforward execution.


o Each statement must be complete before moving to the next.

8.2.2 Selection

 Definition: Selection allows a program to make decisions based on certain


conditions. This control structure enables branching, where the program can
follow different paths depending on whether a condition is true or false.
 Common Forms:
o If Statement: Executes a block of code if a specified condition is true.
o If-Else Statement: Executes one block of code if the condition is true, and
another block if it is false.
https://zedicthub.blogspot.com/

2
 Example

age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

 Characteristics:

o Allows for flexible program behavior based on input conditions.


o Can be nested for multiple conditions.

8.2.3 Iteration

 Definition: Iteration refers to the repetition of a block of code until a specified


condition is met. This control structure allows programs to perform tasks multiple
times without rewriting code.
 Common Forms:
o For Loop: Repeats a block of code a specified number of times or over a
collection of items.
o While Loop: Repeats a block of code as long as a specified condition
remains true.
 Example

for i in range(5): # Executes the loop 5 times


print(i) # Outputs numbers 0 to 4

count = 0
while count < 5: # Executes as long as count is less than 5
print(count)
count += 1 # Increments count by 1

 Characteristics:

o Reduces code duplication by allowing repeated execution of the same


instructions.
https://zedicthub.blogspot.com/

3
o Can lead to infinite loops if the termination condition is not defined
correctly.

8.3 Summary

 An algorithm is a clear, step-by-step procedure for solving problems.


 Flowcharts provide a visual way to represent algorithms, enhancing
understanding and communication.
 Programming constructs—sequence, selection, and iteration—are fundamental
to controlling the flow of a program and creating effective algorithms.

https://zedicthub.blogspot.com/

You might also like