control flow structures. Flowcharts do not restrict us to disciplined control flow structures. Chapter Page - - . Composition A statement is a phrase that commands the computer to do an action.
We have already seen assignment statement. It is a simple statement, used to change the values of variables. Statements may be composed of other statements, leading to hierarchical structure of algorithms. Statements composed of other statements are known as compound statements.
Control flow statements are compound statements. They are used to alter the control flow of the process depending on the state of the process. There are three important control flow statements: • Sequential • Alternative • Iterative When a control flow statement is executed, the state of the process is tested, and depending on the result, a statement is selected for execution. .
. Sequential statement A sequential statement is composed of a sequence of statements. The statements in the sequence are executed one after another, in the same order as they are written in the algorithm, and the control flow is said to be sequential. Let S1 and S2 be statements.
A sequential statement composed of S1 and S2 is written as S1 S2 In order to execute the sequential statement, first do S1 and then do S2. The sequential statement given above can be represented in a flowchart as shown in in Figure . . The arrow from S1 to S2 indicates that S1 is executed, and after that, S2 is executed.
S1 S2 Figure . : Sequential control flow Let the input property be P, and the input-output relation be Q, for a problem. If statement S solves the problem, it is written as . -- P .
S . -- Q If we decompose the problem into two components, we need to compose S as a sequence of two statements S1 and S2 such that the input-output relation of S1, say R, is the input property of S2. . -- P .
. Let us solve the Farmer, Goat, Grass, and Wolf