Introduction to Computing Dr. Nadeem A Khan. Lecture 8.

Slides:



Advertisements
Similar presentations
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Advertisements

Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Computer Science 1620 Loops.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 17.
Introduction to Computing Dr. Nadeem A Khan. Lecture 19.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
BACS 287 Programming Fundamentals 4. BACS 287 Programming Fundamentals This lecture introduces the following iteration control structure topics: – Do.
For Next Looping My first Looping Structure. For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
Introduction to Computing Dr. Nadeem A Khan. Lecture 5.
For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
ENGR 112 Decision Structures.
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.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
Count and add list of numbers From user input and from file.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Repetition Structures
CS 100 Introduction to Computing Seminar October 7, 2015.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Counting Loops.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Controlling Program Flow with Looping Structures
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
UNIT 5 Lesson 15 Looping.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Alternate Version of STARTING OUT WITH C++ 4th Edition
Case & Repetitive Statements
Chapter 6 - VB.Net by Schneider
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 8

Flow chart: loop structure No Process Step(s) Is condition true ? Yes

► Example 1: Sub Command1_Click ( ) Dim num As Integer Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop End Sub Do Loops

► The result: Do Loops

► Example 2: Sub Command1_Click ( ) Dim passWord as String, info As String If Ucase(Text1.Text) = “SECRET.TXT” Then Let passWord=“” Do While passWord <> “SHAHID” Let passWord = InputBox$(“Enter passWord”) Let passWord = Ucase(passWord) Loop End If ‘ Remaining statements on next slide Do Loops (Contd.)

► Example 2 (Contd.): ‘Continues from the previous slide Open “Text1.Text” For Input As #1 Input #1, info Picture1.Print info Close #1 End Sub Do Loops (Contd.)

Do-Loop-Until: General Format ► Do statements Loop Until condition Flow chart?

Do-Loop-Until: General Format ► Modify example 2 to Do-loop-Until Format

► Example 1: Sub Command1_Click ( ) Dim passWord as String, info As String If Ucase(Text1.Text) = “SECRET.TXT” Then Do Let passWord = InputBox$(“Enter passWord”) Let passWord = Ucase(passWord) Loop Until passWord = “SHAHID” End If ‘ Remaining statements on next slide Do-Loop-Until (Contd.)

► Example 1 (Contd.): ‘Continues from the previous slide Open Text1.Text For Input As #1 Input #1, info Picture1.Print info Close #1 End Sub Do-Loop-Until (Contd.)

Do-Loop: Another Example ► When will the following program come to its end?

Do-Loop: Another Example Sub Command1_Click ( ) Dim num As Single num = 7 Do While num <> 0 num=num-2Loop End Sub

Processing List of data!

Processing List of Data ► File PHONE.TXT contains the following four lines: “Ahmad”, “ ” “Aslam”, “ ” “Bhati”, “ ” “Jedallah”, “ ” =>Write a program to display names and numbers?s

► Program 1 Sub_Command1_Click Dim nom As String, phoneNum As String Dim count As Integer count=0 Open “PHONE.TXT” For Input As #1 Do While count<=4 Input #1, nom, phoneNum Picture1.Print nom, phoneNum count=count+1Loop Close #1 End Sub Processing List of Data (Contd.)

How to write the same program without knowing the number of enteries ?

Processing List of Data (Contd.) ► End of File Function EOF(n) where n is the reference number of the open file

Processing List of Data (Contd.) ► Program 2 Sub_Command1_Click Dim nom As String, phoneNum As String Open “PHONE.TXT” For Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum Picture1.Print nom, phoneNum Loop Close #1 End Sub

Counters/Accumulators ► What are: Counters and Accumulators? ► Identify them in the following program

Sub Command1_Click ( ) Dim numCoins As Integer, sum!, value! Open “COINS.TXT” For Input As #1 Let numCoins=0 Let sum =0 Do While Not EOF(1) Input #1, value Let numCoins = numCoins +1 Let sum = sum + value Loop Picture1.Print numCoins;“Coins of value”;sum; “cents” Close #1 End Sub

Exit Do ► Can we not exit the do loop when it is useless to continue its execution further?

Exit Do (Contd.) ► Exit Do: Exits the Do loop

Nested Loops ► Nested loop: Loop inside a loop ► What will be printed by the following program?

Sub Command1_Click ( ) Dim num As Integer, counter As Integer Let counter=1 Do While counter<=4 Let num=1 Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop Let counter=counter+1 Picture1.PrintLoop End Sub Nested Loops (Contd.)

► The result: Nested Loops

► The same: DointCounter=intCounter+1 Loop Until intCounter =10 DointCounter=intCounter+1 Loop While intCounter <10 Other variants of Do

► While condition statementsWend While.. Wend

► The general format with step of 1: For i = m To n Statements Next i For…Next Loops

► Convert to For.. Next Loop Sub Command1_Click ( ) Dim num As Integer Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop End Sub For Next Loops

► Example with For.. Next Loop Sub Command1_Click ( ) Dim num As Integer For num=1 To 10 Picture1.Print num; Next num End Sub For Next Loops

► The result: For Next Loops

► The general format with steps: For i = m To n Step s Statements Next i => Each time increment: i = i + s; m, n, s could be numeric expressions m, n, s could be numeric expressions For…Next Loops

► Example with For.. Next Loop with steps Sub Command1_Click ( ) Dim num As Integer For num= 1 To 10 Step 2 Picture1.Print num; Next num End Sub The output? For Next Loops

► The result: For Next Loops

► Rewrite the following program using nested For.. Next Loop Nested Loops: For…Next

Sub Command1_Click ( ) Dim num As Integer, counter As Integer Let counter=1 Do While counter<=4 Let num=1 Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop Let counter=counter+1 Picture1.PrintLoop End Sub Nested Loops: For…Next

Sub Command1_Click ( ) Dim num As Integer, counter As Integer For counter = 1 To 4 For num =1 To 10 For num =1 To 10 Picture1.Print num; Next num Picture1.Print Next counter End Sub Nested Loops: For…Next

► The result: Nested Loops

► Exit For: Works similar as Exit Do Exit For

Select Case Blocks

► Example: Sub Command1_Click ( ) Picture1.Cls Let num = Val(Text1.Text) Select Case num Case 1 Picture1.Print “Buckle my shoe.” Case 2 Picture1.Print “Pick the sticks.” Case 3,4,5,6 Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Select Case Block ► General form Select Case selector Case valueList1 action1 Case valueList2 action2.. Case Else action of last resort End Select

Another example

Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Read Chapter 5 completely!