Now, it’s the time to give orders to the machine (system).
But the question is How?
Here, comes the Programming language to the rescue. First you have to write instructions to perform a specific task and then computer will execute them to give a meaningful output to you and this process is called Writing a Program.
So, in short, Program is a set of instructions given by the user to the Computer to perform a particular task. Suppose, you are writing commands to find the sum of two numbers or to find the area of the shapes etc then group of these commands will be called Program.
Control Flow of Program
Sequential Flow :
Whenever you start reading a book or chapter, you will go through line by line without skipping lines or words, this is what we call that you are following a sequence. Similarly, if computer reads/executes instructions line by line then it’s called Sequential Flow of Instructions/Program.
Selection Flow :
Whenever you read Newspaper, you skip most of the part or news. Many people read only the heading and skip rest of the news until they could not find information in which they are interested. Similarly, in selection flow computer executes instruction on the basis of certain conditions. For example, if you are giving an instruction that if a person is above 18 years, then he/she eligible for voting otherwise they are not. So, in that case computer follows selection flow between these two conditions.
Iteration Flow :
Iteration is something like Repetition, whenever you want that some instructions need to be executed multiple times, then you should use this flow. In this flow computer executes a set of instruction repeatedly as long as the condition will hold true.
Let’s try to understand one by one………
Sequential Flow :
As mentioned above, sequential flow is a flow where each instruction can be executed line by line. In sequence flow, computer can’t skip any instruction.
Sequential flow of Program
Programs Of Sequential Flow :
Let’s understand programs of sequential flow by creating programs.
Write a program to accept 2 numbers and find the sum.
Before start writing any code, just analyze the program. A program can work in 3 phases they are Input, Processing and Output. We consider these 3 phases and find what we need in above program.
For the sum of two number, these phases are :
- Input : accept two numbers from user as an input
- Processing : Put the formulae for addition of these two numbers.
- Output : Print the sum of these numbers as an output.
Let’s implement the above steps in program :
Program of Addition of 2 numbers
Lines 1, 5 and 8 are the comments of the programs, which are readable by the programmer and are not executed by the computer. It simplifies the working of instructions.
In Lines 2 and 3, you implemented first phase i.e. Input Phase where you accept numbers from the user. For accepting numbers from user, you use input() method. The message which you want to show to the user should be written inside input() method like :
“Enter first Number”,
“Enter Number”,
“Enter 1 number”
We know that input() method always accepts input in string format and you cannot perform any arithmetic operation on it. For example, if you enter 23 then input() method accept it as “23”. To get rid of this problem we use int() method before input(), so that string can be converted into number. If you want to convert it into decimal point then you should use float().
In line 6, you implement the second phase i.e. Processing phase, where you calculate the sum after implementing the formula.
In line 9, you implement the third phase i.e. Output phase, where you display the result to the user.
So, in short, always try to divide your program in 3 phases (as mentioned above) and then start writing the code. With the help of this technique it will be easy to write any kind of program.
Let’s practice more programs on the basis of above technique :
Write a program to find area and perimeter of rectangle.
After analyzing the program, on the basis of formulae i.e.
Area of rectangle = length*breadth,
Perimeter of rectangle = 2*(length+breadth),
We conclude that these program can be divided into the given phases like :
- Input : accept length and breadth of a rectangle from user as an input
- Processing : Put the formulae for calculating area and perimeter of rectangle.
- Output : Print the area and perimeter of rectangle as an output.
Let’s implement the above steps in program :
Program to find Area and Perimeter of Rectangle
Write a program to find simple interest.
After analysing the program, on the basis of formulae i.e.
Simple Interest = (Principal*rate of interest*time)/100
we conclude that these program can be divided into the given phases like :
- Input : accept principal, rate of interest and time from user as an input
- Processing : Put the formulae for calculating simple interest.
- Output : Print the simple interest as an output.
Let’s implement the above steps in program :
Program of Simple Interest