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

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
CS0004: Introduction to Programming Repetition – Do Loops.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
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 22.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP.
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 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 11.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
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.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
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.
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 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Arrays. What is the output of: Private Sub cmdButton_Click() Dim i As Integer, a(1 To 4) As integer Open "DATA.TXT" For Input As #1 For i = 1 To 4 Input.
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.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Logic Structure - focus on looping Please use speaker notes for additional information!
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
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)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
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:
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
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.
Selection Using IF THEN ELSE CASE Introducing Loops.
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.
Visual Basic 6 (VB6) Data Types, And Operators
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 6 - VB.Net by Schneider
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 23

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

Processing List of Data (Contd.) ► Program 1 Sub_Command1_Click Dim nom As String, phoneNum As String 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

Flag ► Flag: A variable to keep track whether a certain event has occurred ► How should we realize the following task?

Flags (Contd.) ► File WORDS.TXT contains the following three lines: “cambist”, “croissant”, “deification” “hydrophyte”, “incisor”, “maculature” “macerate”, “narcolepsy”, “shallon” =>Task: Count the number of words and Report if the words are in alphabetical order

Flags (Contd.) ► Solution: Sub Command1_Click ( ) ‘First Part Dim orderFlag%, wordCounter% Dim word1$, word2$ Let orderFlag=0 Let wordCounter=0 Let word1= “” Open “WORDS.TXT” For Input As #1 ‘program continues on next slide

Flags (Contd.) ‘The second part Do While Not EOF(1) Input #1, word2 Let wordCounter = wordCounter+1 If word1>word2 Then Let orderFlag =1 End If Let word1=word2 LoopClose#1 ‘Program continues on the next slide

Flags (Contd.) ‘The last part Picture1.Print “The number of words is”; WordCounter If orderFlag =0 Then Picture1.Print “Wsords are in alphabetical order.” Else Picture1.Print “Words are not in alphabetical order.” End If ‘End of program 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 ► Modify the previous program using Exit Do

Exit Do(Contd.) ‘The second part Do While Not EOF(1) Input #1, word2 Let wordCounter = wordCounter+1 If word1>word2 Then Let orderFlag =1 Do Exit‘exits the do loop if wrong order End If Let word1=word2 LoopClose#1 ‘Program continues on the next slide

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