Page 1 :
41, , Conditional and, Iterative Statements, , Wn We Carle’, , The if Statements of Python, Repetition of Tasks — A Necessity, The Range( ) Function, , Iteration / Looping Statements, , Introduction 4, Types of Statements in Python, , Statement Flow Control, Program Logic Development Tools, , INTRODUCTION, , cutes its statements from beginning to end. But not many programs, s in strict order from beginning to end. Programs, depending upon, the need, can choose to execute one of the available alternatives or even repeat a set of, statements. To perform their manipulative miracles, programs need tools for performing, repetitive actions and for making decisions. Python, of course, provides such tools by providing, statements to attain So. Such statements are called program control statements. This chapter, discusses such statements in details. Firstly, selection statement if and later iteration, statements for and while are discussed., , some jump statements of Pyt!, , Generally a program exe, execute all their statement, , This chapter also discusses hon which are break and continue.
Page 2 :
COMPUTER SCIENCE WITH PYTHON, 122, , OF STATEMENTS IN PYTHON, , , , 42, , the computer to perform any kind of action, be it dat,, 1 be it repeating actions. Statements form the smallest, thon statements can belong to one of the following, , Statements are the instructions given to, movements, and be it making decisions ©, executable unit within a Python program. Py, , three types =, © Empty Statement © Simple Statement (Single statement), , ¢ Compound Statement, , 1. Empty Statement, The simplest statement is the empty statement i.e., a statement which does nothing. In Python an, empty statement is pass statement. It takes the following form :, pass, Wherever Python encounters a pass statement, Python does PNOTE aad, , nothing and moves to next statement in the flow of control., The pass statement of Python is, , A pass statement is useful in those instances where the a do nothing statement ie,, syntax of the language requires the presence of a statement empty statement or null, but where the logic of the program does not. We will see it in operation statement., , loops and their bodies., , 2. Simple Statement, , Any single executable statement is a simple statement in Python. For example, following isa, simple statement in Python :, , name = input ( "Your name" ), Another example of simple statement is :, print (name) # print function called, , As you can make out that simple statements are single line statements, 3. Compound Statement, , A compound statement represents a group of statements executed as a unit. The compound, statements of Python are written in a specific pattern as shown bel ,, elow :, , <compound statement header> :, , <indented body containing multiple simple and/or compound statements?, , That is, a compound statement has :, , © a header line which begins with a keyword and i a, iS, , with a colon., in, A compound statement !, , © abody consist. of one or more Python statements, each Python has a header ending wi, indented * inside the header line. All statements he a colon (2) and “2 boy con, body are at the same level of indentation, tithe taining a sequence of stale, ments at the same level °, , You'll learn about some other compound statements, , while) in this chapter. (if, for, indentation., , Conventionally, Python uses four spaces to move to next level of indentation
Page 3 :
chapte, , 43., , , , , , ver 4, CONDITIONAL AND ITERATIVE STATEMENTS 123, , STATEMENT FLOW CONTROL, Ina program, Statements may be executed sequentially, selectively oT iteratively. Every, programming language provides constructs to support sequence, selection or iteration., , iscuss what i, Let us discus hat is meant by sequence, selection or iteration constructs., , The sequence construct means the statements are being Statement 1, executed sequentially. This represents the default flow of \, statement (see Fig. 4.1). 4, , Statement 2, Every Python program begins with the first statement of \, program. Each statement in turn is executed (sequence statement 3, construct). When the final statement of program is executed, \, , the program is done. Sequence refers to the normal flow of, , control in a program and is the simplest one., Figure 4.1 The sequence construct., , Selection, , The selection construct means the execution of statement(s) depending upon a condition-test. If, a condition evaluates to True, a course-of-action (a set of statements) is followed otherwise, another course-of-action (a different set of statements) if followed. This construct (selection, construct) is also called decision construct because it helps in making decision about which, set-of-statements is to be executed., , Following figure (Fig. 4.2) explains selection construct., , , , , , , , , { One course-of-action, a True eee, Condition? ————> | Statement 1 —> Statement 2, False {, | Statement 1 |, , Statement 2, , ae, , Another, course-of-action, , Figure 4.2 The selection construct., , You apply decision-making or selection in your real life so many times e.g,, if the traffic signal, light is red, then stop; if the traffic signal light is yellow then wait ; and if the signal light is, green then go., , You can think of many such real life examples of selection/decision-making.
Page 4 :
124, , , , (l oping), , The i i, ; iteration constructs mean repetition of a, Set-of-statements depending u, age % _ :, Condition-test. Til] the time a eClion is, , a, fae AS soon as the condition becomes, it se (or True), the Tepetition stops. The, , eration construct is also called looping, , construct., , The adjacent figure (Fig. 4.3) illustrates an, , iteration construct., , NY, , COMPUTER SCIENCE WITH py ye, , t, , Condition ?, , False, , = Thee, , , , , , , condi, | fue Ndition, Statement 1, | The loop body, Statement 2, , Figure 4.3 The iteration construct,, , The set-of-statements that are repeated again and again is called the body of : the loop,, condition on which the execution or exit of the loop depends is called the exit Condition or, , test-condition., , You can find many examples of iteration or looping around you. For instance, you often, See, , your mother cook chapatis or dosas or appams for you., , Let’s see, what she does for it :, , (i) put rolled chapati or dosa batter on flat pan or tawa, , (i) turn it 2-3 times, (iii) once done take it off., , Repeat the above process (steps (i), (ii), (iii)) for next chapati/dosa/appam. This is looping or, , iteration., , You can find numerous other examples of repetitive work in your real life e.g., washing clothes;, , colouring in colouring book etc. etc., , Every programming language must support these three, , types of constructs as the sequential program execution (the, default mode) is inadequate to the problems we must solve., , ant, por, B3, Ven. 1, , 1. What is a statement ? How many types, of statements are there in Python ?, , constructs., , Python also provides statements that support these, Coming sections discuss, statements — if that supports selection and for and while, that support iteration. Using these statements you can, , create Programs as per your need., , these Python, , 2 ie le Significance of a pass But before we talk about these statements, you should, know basic tools that will help you decide about the logic to, , 3. What is a compound statement ? Give solve a given problem i., the algorithm. For this, there are, , example of a compound statement., , multiple tools available. In the following section, we ate, , 4. What th 7 no, What are the three constructs that going to talk about three such tools — Flowcharts, decisial, , govern statement flow ?, , . What is the need for selection and, looping constructs ?, , trees and pseudocode.
Page 5 :
chop” ¢, a TIVE STATEMENTS, , 125, , 44 PROGRAM LOGIC DEVELOpiyen =, Ols, , you figure, . . Bh vari ‘ Out the algorithm for the solution of a given, An algorithm is a step-by- ous logic development tools. ee, , Ste], instructions) to solve a given oe (well-defined ss, m. For LGORITHM i i, |, , algorithm to find the Temainder of o; instance, the, &lven two An algorithm is a step-by-step, , J. Input first nu, numbers is ;, put fi mber, procedure (well-defined instructions), to solve a given problem., , 2. Input second number,, , 4. Display the result (the thir number), , An algorithm is a set of ord, a, , Consider another example ekentenre a steps (the subtasks) to solve a given problem., , first number is divisible by second aeilon — ~ using the same logic determine if the, r not., , 1, Input first number., 2. Input second number,, 3. Divide first number with second, ; number and store t i in thi, 4 Ciiesksitis tezinenises en store the remainder in third number., (a) If Yes, Display ‘the first number IS divisible by second number’ ., (b) If No, Display ‘the first number IS NOT divisible by second number’., , As you can see that the subtasks in above algorithms are shown in bold letters and there is a, proper order of carrying out these steps/subtasks. Algorithms are commonly written out with, tools like pseudocode, flow charts, or decision trees and tables. Let us talk about these tools one, , by one., , 4.4.1 Flowcharts, A flowchart is a graphical representation of an algorithm. A flowchart shows different subtasks, , with different symbols. Following figure (4.4) shows commonly used flowchart symbols., , > ows [ELT, , A flowchart is a graphical representation of steps an algorithm to solve a, , Subprocess é&) Starv/End e, given problem., [J Document Ej Dela, , Figure 4.4 Flowchart symbols., , , , Process, , , , , , , , , , , , , , , , ut/Output (1/0) operation (taking input and showing output)., ions like initializa, © Use Data symbol for Inp, © Use Process symbol for any, tion, calculation etc., , © Use Subprocess symbol to invoke a procedur, , type of computation and internal operati, , e written already.