You can learn about Flowcharts and Algorithms in Python Programs with Outputs helped you to understand the language better.

Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing

Sequential Processing

The Sequential (Sequence) construct means the statements are being executed sequentially. This is the default flow of statements.
Control structures allow you to change the sequence of instructions for execution. They also implement decisions and repetitions in programs. Decision-based (Conditional/Selection) Controls allow the computer to decide as to which statement is to be executed next depending upon a condition- test.

Iterative construct (Looping) allows the computer to execute a group of statements repeatedly till the condition is true. However, the power of programming languages lies in their ability to instruct a computer to perform the same basic operation again and again till the specific condition is satisfied. In real-life applications, sometimes, you will need to change the sequence of execution according to conditions. For example,
“If you pass the examination, then you will get a mobile”.
In this statement, the resultant action is taken if the condition is evaluated to true.

Decision-based Processing

Conditional/Selection logic, also known as decision logic, is used for making decisions. It is used for selecting the appropriate path out of the two or more alternative paths in the program logic. It is depicted as either an IF…THEN…ELSE or IF…..THEN structure. The flowcharts shown in Figures 2.3 and 2.4 illustrate the logic of these structures. Their corresponding pseudocode, i.e., a statement in the English language is also given in these figures.

Python Programming – Introduction to Programming chapter 2 img 3

The IF…THEN structure says that if the condition is true, then perform process 1; and if it is not true, then skip over process 1.

Python Programming – Introduction to Programming chapter 2 img 4

The IF…THEN…ELSE construct says that if the condition is true, then perform process 1, else (if the condition is not true) perform process 2. Thus, in this case, either process 1 or process 2 will be executed depending on whether the specified con¬dition is true or false. However, if you do not want to choose between the two processes, and you simply want to decide if a process is to be performed or not, then use IF…THEN structure only.
In both the structures, process 1 and process 2 can actually be one or more processes. They are not limited to a single process. END IF is used to indicate the end of the decision structures

Iterative (Looping) Processing

Many jobs that are required to be done with the help of a computer are repetitive in nature. For example, the calculation of the salary of different workers in a factory is done by the (No. of hours worked) x (wage rate). This calculation will be performed by an accountant for each worker every month. Such types of repeti¬tive calculations can easily be done using a program that has a loop built into the solution of the problem. The loop is defined as a block of processing steps repeated a certain number of times. An endless loop repeats infinitely and is always the result of an error. Figure 2.5 illustrates a flowchart depicting the concept of looping. It shows the flowchart for printing values 1, 2, 3…, 20.

In Step 5 of Figure 2.5, the current value of A is compared with 21. If the current value of A is less than 21, steps 3 and 4 are repeated. As soon as the current value of A becomes more than 21, the path corresponding to “NO” is followed, and the repeti¬tion process stops.

In a programming language, the ‘for’ loop is used for an iteration technique.

Terms Used in Looping

Initialization: It is the preparation required before entering a loop. In Figure 2.5, Step 2 initializes the value of A as 1. If there is any garbage in the memory location where the value of A is stored in the PC, that will be changed as 1.

Incrementation: It is the process wherein numer¬ical value is added to the variable each time one goes round the loop. Step 4 in Figure 2.5 shows the increment of A by 1.

The Loop Variable: It is an active variable in the loop. In Figure 2.5, A is the active variable.

Loop Exit Test: There must be some method of leaving the loop after it has revolved the requisite number of times. In Figure 2.5, Step 5 is the decision diamond, where the value of A is compared with 21. As soon as the condition becomes false, control comes out of the loop, and the process stops.

Python Programming – Introduction to Programming chapter 2 img 5

Example 1.
Draw a flowchart for adding marks in ten subjects obtained by a student in an examination. The output should print the percentage of marks of the student in the examination. The flowchart for this problem is shown in figure 2.6.
Solution:
Python Programming – Introduction to Programming chapter 2 img 6

In Figure 2.6, the first symbol is a terminal symbol. It shows that this is the starting point or beginning of the flowchart. The second symbol is an Input/Output (I/O) symbol that is labeled specifically to show that this step is to read input data. This step will input the roll number, name, and marks obtained by the student from an input device into the main storage of the computer system. The third symbol is a processing symbol which is suitably- labeled to indicate that at this step, the computer will add the marks obtained by the student in various subjects and then store the sum in a memory location which has been given the name TOTAL. The fourth symbol is again a processing symbol. The label inside it clearly indicates that the percentage of marks obtained by a student is calculated at this

stage by dividing TOTAL by 10 and multiplying the result by 100. The result is stored in a memory location that has been given the name PERCENTAGE. The fifth symbol is an input/output symbol and is labeled as PRINT OUTPUT.

Example 2.
Draw a flowchart for calculating the average and the percentage marks of 50 students. Each student appeared in ten subjects. The flowchart should show the counting of the number of students who appeared in the examination, and the calculation should stop when the number of students reaches 50. (See Figure 2.7).
Solution:
Python Programming – Introduction to Programming chapter 2 img 7

in the same examination, the process of calculation and printing the percentage of marks obtained by each student will basically remain the same. (The process of reading the input data; adding the marks of all subjects; calculating the percentage and then writing the output data). This is to be repeated for all 50 students. In this situation where the same logical steps can be repeated, the flow line symbols are used in a flowchart to indicate the repetitive nature of the logic in the form of a loop.

Figure 2.7 shows a flowchart that uses a deci¬sion step to terminate the algorithm. In this flowchart, another variable COUNT has been introduced which is initialized to zero outside the loop process and is incremented by 1 after processing the data for each student. Thus, the value of COUNT will always be equal to the number of students whose data has already been processed. At the decision step, the value of COUNT is compared with 50 which is the total number of students who have appeared for the examination. The steps within the loop process are repeated until the value of COUNT becomes equal to 50. As soon as the value of COUNT becomes equal to 50, the instruction at the decision step causes the control to come out of the loop and the processing stops because a terminal symbol labeled STOP is encountered.

How to Write a General Flowchart?

The flowchart shown in Figure 2.8 is a general flowchart for calculating the percentage of marks of any number of students appearing in the examination. A good program should be general in nature. For example, in this case, you should write a program that need not be modified every time the total num¬ber of students appearing in an examination changes.

To overcome these drawbacks, another method can be adapted to control the loop. In this method, the end of input data is marked by a trailer record, that is, the last data record in the input is followed by a record whose main purpose is to indicate that the end of the input data has been reached. Suppose the first 7 characters of the input record of a student represent his roll number (ROLL NO). Since 0000000 is never used as a roll number, a value of 0000000 as the first 7 characters can be used to represent the

trailer record. As each input record is processed, the ROLL NO can be compared with 0000000 to determine if the processing is complete. The logic of this process is shown in the flowchart of Figure 2.8.

Python Programming – Introduction to Programming chapter 2 img 8

What is a Trailer Record?

The concept of a trailer record centers around the notion of selecting a field (a particular item of data) in the input record which will be used to indicate the end of data and then selecting a trailer value, also known as a sentinel value, which will never occur as normal data value for that field. The roll number 0000000 is a trailer record in Figure 2.8.

Example 3.
Draw a flowchart to generate the first 13 members of the Lucas series:
1, 3, 4, 7,11,18, 29, (See Figure 2.9).

Example 4.
Draw a flowchart that reads a year and determines whether it is a leap year or not (See Figure 2.10).

Example 5.
Draw a flowchart that swaps two numbers without using a temporary variable (See Figure 2.11).

Python Programming – Introduction to Programming chapter 2 img 9

Python Programming – Introduction to Programming chapter 2 img 10
Python Programming – Introduction to Programming chapter 2 img 11

Example 6.
Draw a flowchart to read any number. If the number is greater than 10, it displays its square and cube (See figure 2.12.).

Python Programming – Introduction to Programming chapter 2 img 12

Leave a Reply

Your email address will not be published. Required fields are marked *