LOOP Basics.

Slides:



Advertisements
Similar presentations
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Advertisements

Loops – While, Do, For Repetition Statements Introduction to Arrays
Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Geography 465 Assignments, Conditionals, and Loops.
Python Control of Flow.
4. Python - Basic Operators
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
If statements while loop for loop
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python break,continue and pass Statements. The break Statement: for letter in 'Python': # First Example if letter == 'h': break print 'Current Letter.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Computer Programming -1-
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
G. Pullaiah College of Engineering and Technology
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Think What will be the output?
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Chapter 5: Repetition Structures
Programming Fundamentals
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Loop Control Structure.
Outline Altering flow of control Boolean expressions
Control Structures: if Conditional
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Control Structures: for & while Loops
Loops in C.
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
Chapter 3 – Control Structures
More Looping Structures
Introduction to Programming Using Python PART 2
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Introduction to Repetition Structures
The structure of programming
CHAPTER 5: Control Flow Tools (if statement)
The structure of programming
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
More Looping Structures
Loops CGS3416 Spring 2019 Lecture 7.
Thinking procedurally
Class code for pythonroom.com cchsp2cs
REPETITION Why Repetition?
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

LOOP Basics

INTRODUCTION Programming languages provide various control structures that allow for more complicated execution paths. Statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. A loop statement allows us to execute a statement or group of statements multiple times.

The following diagram illustrates a loop statement −

WHILE LOOP FOR LOOP NESETED LOOP Python programming language provides following types of loops to handle looping requirements. WHILE LOOP FOR LOOP NESETED LOOP

WHILE LOOP A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true. Syntax The syntax of a while loop in Python programming language is − While < logical expression> : statement(s) statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop. In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. Flow Diagram  Example:- a=5 While a>0: print(“hello”,a) a=a-3 Print(“loop over”) The above code will print- hello 5 hello 2 loop over

The Infinite Loop A loop becomes infinite loop if a condition never becomes FALSE. You must use caution when using while loops because of the possibility that this condition never resolves to a FALSE value. This results in a loop that never ends. Such a loop is called an infinite loop. Ex a=1 While a>1: a=a+1 Print(a) This will show result infinite times To stop infinite loop press ctrl+c during its endless repetition

FOR LOOP It has the ability to iterate over the items of any sequence, such as a list or a string. Syntax >>>for <variable> in <sequence>: statements_to_repeat Colon must be there

Flow diagram  For a in Range(1,4,7): Print(a) Print(a*a) This will give result as follows: 1 4 16 7 49

Range STATEMENT VALUES GENERATED RANGE(10) 0,1,2,3,4,5,6,7,8,9 5,8,11,14 RANGE(10,5,-1) 10,9,8,7,6 RANGE(10,1,-2) 10,8,6,4,2

Using else Statement with for and while Loop If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false. Ex #(for loop ) for num in range(10,20): #to iterate between 10 to 20 for i in range(2,num): #to iterate on the factors of the number if num%i == 0: #to determine the first factor j=num/i #to calculate the second factor print ('%d equals %d * %d' % (num,,i,,j)) break #to move to the next number, the #first FOR else: # else part of the loop print (num, 'is a prime number‘) #(While loop) count = 0 while count < 5: print (count, " is less than 5“) count = count + 1 else: print (count, " is not less than 5“)

Nested loop Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept. Syntax for iterating_var in sequence: statements(s) The syntax for a nested while loop statement in Python programming language is as follows − while expression: statement(s)

A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa. The following program uses a nested for loop to find the prime numbers from 2 to 100 − Ex  i = 2 while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print (i,"is prime“) i = i+1 print ("Good bye!")

THANK YOU BY CHIRAG AND VIPIN