0% found this document useful (0 votes)
12 views36 pages

Control Structures

Control structure

Uploaded by

alph.codes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views36 pages

Control Structures

Control structure

Uploaded by

alph.codes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 36

Control Structures

Control Structures
• There are three basic types of logic, or flow of control, known as:

• Sequence logic, or sequential flow


• Selection logic, or conditional flow
• Iteration logic, or repetitive flow
Sequential Logic (Sequential Flow)

Sequential logic as the name


suggests follows a serial or
sequential flow in which the flow
depends on the series of
instructions given to the
computer.
Selection Logic
(Conditional Flow)

Selection Logic simply involves a


number of conditions or
parameters which decides one out
of several written modules. The
structures which use these type of
logic are known as Conditional
Structures.
Iteration Logic (Repetitive Flow)

The Iteration logic employs a loop


which involves a repeat statement
followed by a module known as
the body of a loop.
Loop Statement
Loops

• In a programming language, a loop is a statement that contains


instructions that continually repeats until a certain condition is
reached.

• Loops help us remove the redundancy of code when a task has to be


repeated several times. With the use of loops, we can cut short those
hundred lines of code to a few.
Loops

Suppose you want to print the


text “Hello, World!” 10 times.
Rather than writing a print
statement 10 times, you can make
use of loops by indicating the
number of repetitions needed.
Loops
•Loop Types

•The three types of loops:


•for loop
•while loop
•do While loop
For Loops
• Loop statements will be
executed for each item of the
sequence.

for (initial value; condition; incrementation or


decrementation)

{
statements;
}
while loop
• It continually executes the
statements(code) as long as the
given condition is TRUE. It first
checks the condition and then
jumps into the instructions.

while (condition)
{
statements;
}
Do-While loop
• A do…while loop is similar to the
while loop except that the condition is do
always executed after the body of a {
loop. statements
• It is also called an exit-controlled loop. }
• In the do-while loop, the body of a while
loop is always executed at least once. (expression);
After the body is executed, then it
checks the condition.
Break Statement
• The break statement is used mainly in the switch statement. It is also useful for
immediately stopping a loop.
Continue Statement
• When you want to skip to the next iteration but remain in the loop,
you should use the continue statement.
Compound Logical Operators
• There are many occasions when we need to extend the conditions
that are to be tested. Often there are conditions to be linked.
Conditions linked with and only result in an action when all conditions
are true.

• For example, if a >b and a > c then display “a is the largest”. Conditions linked
with an or lead to an action when either or both are true.
• Example: To input a examination mark and test it for the award of a
grade. The mark is a whole number between 1 and 100. Grades are
awarded according to the following criteria:
>= 80 Distinction
>= 60 Merit
>= 40 Pass
< 40 fail
The pseudo-code is
Hint:
• Use variables: mark of type integer
• If mark >= 80 display “distinction”
• If mark >= 60 and mark < 80 display “merit”
• If mark >= 40 and mark < 60 display “pass”
• If mark < 40 display “fail”
Use variables: category, of type character
If – Else Display “input category (U/A/B/M)”
Accept category
If category = U
The case statement Display “insurance is not available”
Repeating the if … else Else
statements a number of times If category = A then
can be somewhat confusing.
Display “insurance is double”
An alternative method
provided in a number of Else
languages is to use a selector If category = B then
determined by the alternative Display “insurance is normal”
conditions that are needed. In
Else
our pseudo-code, this will
called a case statement. If category = M then
Display “insurance is medically dependent”
Else
Display “entry invalid”
Use variables: category, of type character

Using case statement Display “input category”


Accept category
DO case of category
CASE category = U
Display “insurance not available”
CASE category = A
Display “insurance is double”
CASE category = B
Display “insurance is normal”
CASE category = M
Display “insurance is medically dependent”
ELSE
Display “entry is invalid”
ENDCASE
A program segment to print out each character typed
at a keyboard until the character ‘q’ is entered.

What Concept ?
A program segment to print out each character typed
at a keyboard until the character ‘q’ is entered.

WHILE letter <> ‘q’


ACCEPT letter
DISPLAY “The character you typed is”, letter
ENDWHILE
Pseudo code that will output the square root of any
number input until the number input is zero.

What Concept?
Pseudo code that will output the square root of any
number input until the number input is zero.
Use variable: number of type real
DISPLAY “Type in a number or zero to stop”
ACCEPT number
WHILE number <> 0
Square = number * number
DISPLAY “The square of the number is”, square
DISPLAY “Type in a number or zero to stop”
ACCEPT number
ENDWHILE
Calculate the sum and average of a series of numbers.

What Concept?
Calculate the sum and average of a series of numbers.
Use variables: n, count of the type integer Sum, number, average of the type real

DISPLAY “How many numbers do you want to input”


ACCEPT count
SUM = 0
FOR (n = 1, n <= count, n + 1)
DISPLAY “Input the number from your list”
ACCEPT number
SUM = sum + number
ENDFOR
Average = sum / count
DISPLAY “The sum of the numbers is “, sum
DISPLAY “Average of the numbers is “, average
Pseudocode and the corresponding flowchart for finding the
sum of n numbers.

What Concept?
Pseudocode and the corresponding flowchart for finding the
sum of n numbers.
Start
Sum = 0
Display “Input value n”
Input n
For(i = 1, i<=n, i++)
Input a value
Sum = sum + value
ENDFOR
Output sum
Stop
• In this example, we have used ‘i’ to allow us to count the numbers, which we get for the addition.
• We compare ‘i’ with n to check whether we have exhausted the numbers or not in order to stop
the computation of the sum (or to stop the iteration structure).
• In such a case, ‘i’ is referred to as a counter.
Flow charts
• Flowcharting is a tool developed in the computer industry, for
showing the steps involved in a process.

• A flowchart is a diagram made up of boxes, diamonds and other


shapes, connected by arrows
• each shape represents a step in the process, and the arrows show the order
in which they occur.
• Flowcharting combines symbols and flowlines, to show figuratively the
operation of an algorithm.
Flow charts
• Flow charts visualize the results of decisions, showing what will
happen in a program, and also when, for example an if statement, is
required to make a decision.
• Flow charts can be used to show iteration (repeating something).
• Flowcharts are written with program flow from the top of a page to
the bottom.
• Each command is placed in a box of the appropriate shape, and
arrows are used to direct program flow.
Flow charts

Notations
General Rules for flowcharting
• All boxes of the flowchart are connected with Arrows. (Not lines)
• Flowchart symbols have an entry point on the top of the symbol with no other entry points. The exit point
for all flowchart symbols is on the bottom except for the Decision symbol.
• The Decision symbol has two exit points; these can be on the sides or the bottom and one side.
• Generally a flowchart will flow from top to bottom. However, an upward flow can be shown as long as it
does not exceed 3 symbols.
• Connectors are used to connect breaks in the flowchart. Examples are:
• From one page to another page.
• From the bottom of the page to the top of the same page.
• An upward flow of more than 3 symbols
• Subroutines and Interrupt programs have their own and independent flowcharts.
• All flow charts start with a Terminal or Predefined Process (for interrupt programs or subroutines) symbol.
• All flowcharts end with a terminal or a contentious loop.
Draw a flowchart for a program that calculates
and print the area and the perimeter of a
rectangle.
• Input
• Length
• width
• Processing
• Area = length*width
• Perimeter = 2*( length + width)
• Output
• Area
• Perimeter
Draw a flowchart for a program that calculates
and print the area and the perimeter of a
rectangle.
• Input
• Length
• width
• Processing
• Area = length*width
• Perimeter = 2*( length + width)
• Output
• Area
• Perimeter
Write a Program to Print the Sum of two
integer Numbers
Pseudocode
1. BEGIN /*Start*/
2. INPUT N1 /* read the first number and save in the variable ( N1 )*/
3. INPUT N2 /* read the second number and save in the variable ( N2 )*/
ADD * Add both numbers and save the result in the variable *
4. (Sum) --> Sum = N1 + N2
5. OUTPUT Sum /*Print the variable ( Sum )*/
6. END /*Stop*/
Define a pseudocode and draw the corresponding flowchart
for adding the test scores as given below: 26, 49, 98, 87, 62,
75
Define a pseudocode and draw the corresponding flowchart
for adding the test scores as given below: 26, 49, 98, 87, 62,
75
Pseudocode:
1. Start
2. Sum = 0
3. INPUT value
4. Sum = Sum + value
5. GOTO Step3
6. OUTPUT Sum
7. Stop
Define a pseudocode and draw the corresponding flowchart
for adding the test scores as given below: 26, 49, 98, 87, 62,
75
Pseudocode:
1. Start
2. Sum = 0
3. INPUT value
4. Sum = Sum + value
5. GOTO Step3
6. OUTPUT Sum
7. Stop

You might also like