Notes Chapter 4 Conditional and Iterative Statements

Introduction

  • Generally a program executes from starting point to end point.
  • Some program does not execute in order.
  • As per the requirement, execution order of the program can be changed and it is also possible to execute a program repeatedly.
  • Python provides control structures to manage the order of execution of a program, which are if-else, for, while and jump statements like break, continue.

Types of statements

In Python, statements are of 3 types-

» Empty Statements
              • pass

» Simple Statements (Single Statement)
              • name=input (“Enter your Name “)
              • print(name) etc.

» Compound Statements

Notes Chapter 4 Conditional and Iterative Statements

              •Here, Header line starts with the keyword and ends at colon (:).
              •The body consists of more than one simple Python statements or compound statements.

Statement Flow Control

In a program, statements executes in sequential manner or in selective manner or in iterative manner.

Notes Chapter 4 Conditional and Iterative Statements

Program Logic Development Tool

A program has following development stages-

  1. Identification of the problem
  2. Analysis of problem
  3. Writing Algorithm or Designing Flowchart
  4. Writing Code
  5. Testing and Debugging
  6. Implementation
  7. Maintenance

Algorithm

A process or set of rules to be followed in problem-solving operations is an algorithm.

For ex-
Algorithm to add two numbers is as under-

  1. Input First Number
  2. Input Second Number
  3. Add First Number with Second Number and store into Third number.
  4. Display Third number

Flowcharts

A flowchart is a graphical representation of an algorithm, workflow or process. The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows.

For ex- flowchart to calculate simple interest is as under-

Notes Chapter 4 Conditional and Iterative Statements

if Statement

In Python, if statement is used to select statement for processing. If execution of a statement is to be done on the basis of a condition, if statement is to be used. Its syntax is-

Notes Chapter 4 Conditional and Iterative Statements

like –

Notes Chapter 4 Conditional and Iterative Statements

if-else Statement

If out of two statements, it is required to select one statement for processing on the basis of a condition, if-else statement is to be used. Its syntax is-

Notes Chapter 4 Conditional and Iterative Statements

like –

Notes Chapter 4 Conditional and Iterative Statements

if-elif Statements

If out of multiple statements, it is required to select one statement for processing on the basis of a condition, if-elif statement is to be used. Its syntax is-

Notes Chapter 4 Conditional and Iterative Statements

like –

Notes Chapter 4 Conditional and Iterative Statements

Nested If -else

Notes Chapter 4 Conditional and Iterative Statements

Loop/ Repetition/ Iteration

These control structures are used for repeated execution of statement(s) on the basis of a condition. Loop has 3 main components-
1. Start (initialization of loop)
2. Step (moving forward in loop )
3. Stop (ending of loop)

Notes Chapter 4 Conditional and Iterative Statements

Python has following loops-
        –for loop
        –while loop

range () Function

In Python, an important function is range( ). its syntax is-

                range ( <lower limit>,<upper limit>)

If we write – range (0,5 )

Then a list will be created with the values [0,1,2,3,4] i.e. from lower limit to the value one less than ending limit.

range (0,10,2) will have the list [0,2,4,6,8].
range (5,0,-1) will have the list [5,4,3,2,1].

in and not in operator

in operator-
        3 in [1,2,3,4] will return True.
        5 in [1,2,3,4] will return False.

not in operator-
        5 not in [1,2,3,4] will return True.

Notes Chapter 4 Conditional and Iterative Statements

Table of a number by For loop

Syntax of For Loop

for <var> in <sequence>:
           <statements to repeat>

Notes Chapter 4 Conditional and Iterative Statements

Output

Notes Chapter 4 Conditional and Iterative Statements

Table of a number by while loop

Syntax of While Loop

While <LogicalExpression>:
          <loop body with increment
             or decrement>

Notes Chapter 4 Conditional and Iterative Statements

Output

Notes Chapter 4 Conditional and Iterative Statements

Jump Statements

Notes Chapter 4 Conditional and Iterative Statements
Notes Chapter 4 Conditional and Iterative Statements
Notes Chapter 4 Conditional and Iterative Statements
Notes Chapter 4 Conditional and Iterative Statements
Notes Chapter 4 Conditional and Iterative Statements
Notes Chapter 4 Conditional and Iterative Statements

Output of both the programs

Notes Chapter 4 Conditional and Iterative Statements

Nested Loop

Notes Chapter 4 Conditional and Iterative Statements

Output

Notes Chapter 4 Conditional and Iterative Statements
Notes Chapter 4 Conditional and Iterative Statements

Related Posts

error: Content is protected !!