Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Similar documents
Revision of Basics of Python

Chapter 5 Conditional and Iterative Statements (Part-II) To carry out repetitive task, python provides following iterative/looping statements:

Introduction. C provides two styles of flow control:

Conditionals and Recursion. Python Part 4

Computer Science 217

Chapter 4 Repetition Structures. Dr. Zhang COSC 1436 Spring 2017 June 15, 2017

Control flow statements

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Introduction to Python (All the Basic Stuff)

Computer Programming: C++

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

ENGR 1181 MATLAB 09: For Loops 2

Loops / Repetition Statements

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Control Structures 1 / 17

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Computer Science 217

SBT 645 Introduction to Scientific Computing in Sports Science #3

Programming for Experimental Research. Flow Control

Introduction to Mathematical and Scientific Programming TUTORIAL WEEK 2 (MATH 1MP3) Winter 2019

SCoLang - Language Reference Manual

Lecture 7 Tao Wang 1

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Introduction to Computer Programming for Non-Majors

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

ENGR 101 Engineering Design Workshop

Lecture 04 More Iteration, Nested Loops. Meet UTA Jarrett s dog Greta, lying in her nest

MEIN 50010: Python Flow Control

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Lecture 02 Making Decisions: Conditional Execution

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

An Introduction to Python

Algorithms and Data Structures

LOOPS. Repetition using the while statement

Loops / Repetition Statements

DECISION CONTROL AND LOOPING STATEMENTS

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Comp Exam 1 Overview.

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

CSc 110, Autumn Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

Computer Science 217

Decision Making -Branching. Class Incharge: S. Sasirekha

Comp 150 Exam 2 Overview.

Quiz Determine the output of the following program:

Control Structures in Java if-else and switch

Scripting Languages. Python basics

Selection Statement ( if )

Python Programming: An Introduction To Computer Science

CS 112: Intro to Comp Prog

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Python Programming: An Introduction to Computer Science

Textbook. Topic 5: Repetition. Types of Loops. Repetition

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Decision Making and Loops

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

(Python) Chapter 3: Repetition

Turtle Graphics Conditional Statements, Loops 3#

CITS 4406 Problem Solving & Programming

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

REPETITION CONTROL STRUCTURE LOGO

CSc 110, Spring Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Flow Control. Key Notion. Statement Categories. 28-Oct-10

Prepared by: Shraddha Modi

Loops. Upsorn Praphamontripong. CS 1110/CS 1111 Introduction to Programming Spring 2018

ECE 122. Engineering Problem Solving with Java

Decision Structures CSC1310. Python Programming, 2/e 1

Language Reference Manual

LECTURE 5 Control Structures Part 2

Repetition, Looping. While Loop

OER on Loops in C Programming

Python 2 Conditionals and loops

Chapter 8. Statement-Level Control Structures

Chapter 4. Flow of Control

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

Module 4: Decision-making and forming loops

Real Python: Python 3 Cheat Sheet

Introduction to C/C++ Lecture 3 - Program Flow Control


A453 Task 1: Analysis: Problem: Solution:

Decision Making in C

Decision making with if Statement : - Control Statements. Introduction: -

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

THE AUSTRALIAN NATIONAL UNIVERSITY Final Examination November COMP1730 / COMP6730 Programming for Scientists

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

8. Control statements

Advanced Computer Programming

Flow Control. So Far: Writing simple statements that get executed one after another.

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

CSCE 110: Programming I

Transcription:

Chapter 5 : Informatics practices Class XI ( As per CBSE Board) Conditional & Looping Constructs

Control Statements Control statements are used to control the flow of execution depending upon the specified condition/logic. There are three types of control statements. 1. Decision Making Statements 2. Iteration Statements (Loops) 3. Jump Statements (break, continue, pass)

Decision Making Statement Decision making statement used to control the flow of execution of program depending upon condition. There are three types of decision making statement. 1. if statements 2. if-else statements 3. Nested if-else statement

Decision Making Statement 1. if statements An if statement is a programming conditional statement that, if proved true, performs a function or displays information.

Decision Making Statement 1. if statements Syntax: if(condition): statement [statements] noofbooks = 2 if (noofbooks == 2): print('you have ') print( two books ) print( outside of if statement ) Output You have two books Note:To indicate a block of code in Python, you must indent each line of the block by the same amount. In above both print statements are part of if condition because of both are at same level indented but not the third print statement.

Decision Making Statement 1. if statements Using logical operator in if statement x=1 y=2 if(x==1 and y==2): print( condition matcing the criteria') Output :- condition matcing the criteria ----------------------------------------------------------- a=100 if not(a == 20): print('a is not equal to 20') Output :- a is not equal to 20

Decision Making Statement 2. if-else Statements If-else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false.

Decision Making Statement 2. if-else Statements Syntax: if(condition): statements else: statements a=10 if(a < 100): print( less than 100') else: print( more than equal 100') OUTPUT less than 100 *Write a program in python to check that entered numer is even or odd

Decision Making Statement 3. Nested if-else statement The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

Decision Making Statement 3. Nested if-else statement Syntax If (condition): statements elif (condition): statements else: statements E.G. num = float(input("enter a number: ")) if num >= 0: if num == 0: print("zero") else: print("positive number") else: print("negative number") OUTPUT Enter a number: 5 Positive number * Write python program to find out largest of 3 numbers.

Iteration statements(loop) are used to execute a block of statements as long as the condition is true. Loops statements are used when we need to run same code again and again. Python Iteration (Loops) statements are of three type :- 1. While Loop 2. For Loop 3. Nested For Loops

1. While Loop It is used to execute a block of statement as long as a given condition is true. And when the condition become false, the control will come out of the loop. The condition is checked every time at the beginning of the loop. Syntax while (condition): statement [statements] x = 1 while (x <= 4): print(x) x = x + 1 Output 1 2 3 4

While Loop continue While Loop With Else x = 1 while (x < 3): print('inside while loop value of x is ',x) x = x + 1 else: print('inside else value of x is ', x) Output inside while loop value of x is 1 inside while loop value of x is 2 inside else value of x is 5 *Write a program in python to find out the factorial of a given number

While Loop continue Infinite While Loop x = 5 while (x == 5): print( inside loop') Output Inside loop Inside loop

2. For Loop It is used to iterate over items of any sequence, such as a list or a string. Syntax for val in sequence: statements for i in range(3,5): print(i) Output 3 4

2. For Loop continue Example programs for i in range(5,3,-1): print(i) Output 5 4 range() Function Parameters start: Starting number of the sequence. stop: Generate numbers up to, but not including this number. step(optional): Determines the increment between each numbers in the sequence.

2. For Loop continue For Loop With Else for i in range(1, 4): print(i) else: # Executed because no break in for print("no Break") Output 1 2 3 4 No Break

2. For Loop continue Nested For Loop for i in range(1,3): for j in range(1,11): k=i*j print (k, end=' ') print() Output 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20

3. Jump Statements Jump statements are used to transfer the program's control from one location to another. Means these are used to alter the flow of a loop like - to skip a part of a loop or terminate a loop There are three types of jump statements used in python. 1.break 2.continue 3.pass

1.break it is used to terminate the loop. for val in "string": if val == "i": break print(val) print("the end") Output s t r The end

2.continue It is used to skip all the remaining statements in the loop and move controls back to the top of the loop. for val in "init": if val == "i": continue print(val) print("the end") Output n t The end

3. pass Statement This statement does nothing. It can be used when a statement is required syntactically but the program requires no action. Use in loop while True: pass # Busy-wait for keyboard interrupt (Ctrl+C) In function It makes a controller to pass by without executing any code. def myfun(): pass #if we don t use pass here then error message will be shown print( my program') OUTPUT My program

for i in 'initial': if(i == 'i'): pass else: print(i) 3. pass Statement continue OUTPUT n t a L NOTE : continue forces the loop to start at the next iteration while pass means "there is no code to execute here" and will continue through the remainder or the loop body.