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

basic operators & flowcharts

This is a document

Uploaded by

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

basic operators & flowcharts

This is a document

Uploaded by

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

CS1002 – Programming

Fundamentals
Lecture # 04
2
Introduction to Flowcharts
Name Symbol Use in flowchart

Oval Denotes the Start or end of the program

Parallelogram Used for input and output statements

Denotes a process to be carried out (e.g. addition, subtraction


Rectangle etc.)

Denotes a decision (or branch) to be made.


Diamond The program should continue along one of two routes. (e.g.
IF/THEN/ELSE)

On-page Connector Denotes the connection of flow chart element on same page.

Denotes the connection of flow chart elements across different


Off-page Connector
CS1002 - FALL 2023 pages
3
Introduction to Flowcharts
Name Symbol Use in flowchart

Flow lines Used to indicate the flow of logic by connecting


Arrows different symbols

Predefined Process /Function Used to represent a group of


statements performing one processing task.

Preprocessor
4
Mathematical Operators
5
Relational Operators
6
Logical Operators
7
The Flowchart
● Dictionary Definition: A schematic representation of a sequence of
operation, as in a manufacturing process or computer program
● Technical Definition: A graphical representation of the sequence of
operations in an information system or program
○ Program flowcharts: show the sequence of instructions in a single program or
subroutine
● Different symbols are used to draw each type of flowchart
8
The Flowchart
● A flowchart
○ Shows logic of an algorithm
○ Emphasizes individual steps and their interconnections

○ E.g. control flow from one action to another


9
Example 1 (Pseudo code)
Start
1. Start Processing
declare num1, num2,
2. declare num1, num2, result result
3. input num1 Input
Input num1
4. input num2
Input
5. results = num1 + num2 Input num2
Processing
6. Print results
result = num1 + num2
7. End
Print result Output

End
10
Class Task
● Draw flow chart for the following task
● Take 3 numbers from the user and print the average and sum of these three numbers
11
If selection structure
● Generic Format

A decision can be made on any


expression.
zero - false
true nonzero - true
condition Statement (s)
Example:
3 - 4 is true

false
12
if Selection Structure
Translation into Algorithm
If student’s grade is greater than or equal to 60
Print “Passed”

if ( marks >= 60 )
print "Passed";

● Diamond symbol (decision symbol)


○ Indicates decision is to be made
○ Contains an expression that can be true or false
■ Test condition, follow path

● if structure
○ Single-entry/single-exit
13
if Selection Structure
● Flowchart of pseudocode statement

A decision can be made on any


expression.
zero - false
true Print nonzero - true
Marks >= 60 “Passed”
Example:
3 - 4 is true

false
Start
14
Example 2: Flow chart declare M1, M2, M3,
M4, average
Pseudocode:
1.0 Start Input M1, M2,
M3, M4
2.0 Declare M1, M2, M3, M4, average
average = (M1 +
3.0 Input M1, M2, M3, M4 M2+M3+M4)/4
4.0 average = (M1+ M2+ M3+ M4) / 4
5.0 if (average > 50) then true average >=
60
5.1 Print “PASS”
false
6.0 endif
Print
7.0 End “PASS”

End
15
if/else Selection Structure
● General Structure

if condition is met then true false


condition
statement_1 (s)
else Statement_1 Statement_2
statement_2 (s) (s) (s)

endif
16
if/else Selection Structure
● if
○ Performs action if condition true
● if/else
○ Different actions if conditions true or false
Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”

if ( grade >= 50 )
Print "Passed";
else
Print "Failed";
17
if/else Selection Structure

false true
grade >= 50

print print
“Failed” “Passed”
Start

18
Example 3: Flow chart declare M1, M2, M3,
M4, average
Pseudocode:
1.0 Start Input M1, M2,
M3, M4
2.0 Declare M1, M2, M3, M4, average
3.0 Input M1, M2, M3, M4 average (M1 +
4.0 average = (M1+ M2+ M3+ M4) / 4 M2+M3+M4)/4
5.0 if (average >= 50) then
5.1 Print “PASS” true average >= false
6.0 else 50
6.1 Print “FAIL”
7.0 endif
Print “PASS” Print “FAIL”
8.0 End

End
19
Example 4
● Write a pseudocode and draw a flowchart to convert the length in feet to centimeter
● Algorithm:
○ Input the length in feet (Lft)
○ Calculate the length in cm (Lcm) by multiplying Lft with 30
○ Print length in cm (Lcm)
20
Example 5
● Draw a flowchart to calculate area of a rectangle
● The program should ask the user to input Length and Width and then display the
Area.
● Area = Length * Width
21
Example 6
Draw a flowchart that reads two values, determines the largest value and prints the largest value with an identifying message

Pseudocode
1.0 Declare VALUE1, VALUE2, MAX
2.0 Input VALUE1, VALUE2
3.0 if (VALUE1 > VALUE2) then
3.1 MAX = VALUE1
4.0 else
4.1 MAX = VALUE2
5.0 end if
6.0 Print “The largest value is”, MAX
22
Questions

You might also like