Control Structures
Control Structures
Control Structures
• There are three basic types of logic, or flow of control, known as:
{
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
What Concept ?
A program segment to print out each character typed
at a keyboard until the character ‘q’ is entered.
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
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.
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