Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.

Slides:



Advertisements
Similar presentations
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Advertisements

Chapter 31 Fundamentals of Programming in Visual Basic (Continue IV) Strings Variables and Strings Using Text Boxes for Input and Output Concatenation.
5.05 Apply Looping Structures
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 5 new The Do…Loop Statement
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
R EVIEW OF C HAPTER 2. H OW TO D EVELOP A VB A PPLICATION Design the Interface for the user Literally draw the GUI Drag buttons/text boxes/etc onto form.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Chapter 3 - VB 2005 by Schneider1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers.
CS0004: Introduction to Programming Variables – Strings.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
ENGR 112 Decision Structures.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Tutorial 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Chapter 05 (Part III) Control Statements: Part II.
Repetition Statements
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
String Manipulation 10/21/2015 Lect#6 GC Strings have their own properties and methods, just like a textbox or label or form does. 10/21/2015 Lect#6.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Higher Computing Software Development -So Far- 5/10/10.
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
String Manipulation Reference:
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 3 – Fundamentals of Programming in Visual Basic
Chapter 3 – Variables, Input, and Output
String Manipulation Reference:
Visual Basic 6 (VB6) Data Types, And Operators
Problem Solving and Control Statements: Part 2
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 3 – Variables, Input, and Output
Chapter 3 Data Types, Variables, and Expressions
3rd prep. – 2nd Term MOE Book Questions.
Chapter 5 The Do…Loop Statement
Chapter 3 – Variables, Input, and Output
String Manipulation Reference:
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
Chapter (3) - Looping Questions.
Section 3.3 Numbers Arithmetic Operations Variables
Variable Review & IO User 12/26/2018.
Introduction to Problem Solving and Control Statements
String Variable, Methods and Properties
String Manipulation Reference:
String Variable, Methods and Properties
Introduction to Computer Science
String Variable, Methods and Properties
Conditional Loops Counted Loops
Presentation transcript:

Created By Mayson Al-Duwais1

Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special statement called: Exit statement. The program terminates that repetition statement and continues execution with the first statement after the repetition statement. Exit Do statement to exit from Do While … Loop, Do … Loop While statement, Exit Select statement to exit from a Select … Case statement. Exit For statement to exit from For … Next and Created By Mayson Al-Duwais 2

Example Exit Do Do...Loop While Do While....Loop Dim i, result As Integer i = 1 result = 1 Do result *= i Label1.Text &= ("i= " & i & "result= " & result & vbCrLf) If result > 10 Then Label1.Text &= “Exit Do" Exit Do End If i += 1 Loop While (i < 10) Dim i, result As Integer i = 1 result = 1 Do While (i < 10) result *= i Label1.Text &= ("i= " & i & " result= " & result & vbCrLf) If result > 10 Then Label1.Text &= “Exit Do" Exit Do End If i += 1 Loop Created By Mayson Al-Duwais3 When this condition is met, Exit Do statement is executed causing loop to stop and the execution control goes to the first statement after the loop

Examples: Exit Select, Exit For Select...Case For...Next Dim name As String = “Ahmad” Select name Case “hind” Label1.Text= “Name is ” & name Case “Ahmad” Label1.Text= “Exit Select” Exit Select Case “Asma” Label1.Text= “Name is ” & name Case Else Label1.Text= “Not Correct” End Select For number1 As Integer =1 To 5 Label1.Text= “number1 =” & number1 & vbCrLf If number1 Mod 3 = 0 Then Label1.Text &= “Exit For” Exit For End If Next Created By Mayson Al-Duwais4

Using Continue in Repetition Statements A Continue statement terminates only the current iteration of a repetition statement and continues execution with the next iteration of the loop. The Continue Do statement can be used in a Do While … Loop and Do … Loop While statement. The Continue For statement can be used in For … Next statement. Created By Mayson Al-Duwais 5

How Continue For work? When Continue For is encountered in a For … Next statement, execution continues with the statement’s increment expression, then the program evaluates the loop- continuation test. Created By Mayson Al-Duwais 6

Example Continue For For number1 As Integer = 1 To 5 Label1.Text &= "number1 =" & number1 & vbCrLf If number1 Mod 3 = 0 Then Label1.Text &= "Continue For" & vbCrLf Continue For End If Next Created By Mayson Al-Duwais7

How Continue Do Work? When Continue is used, the program evaluates the loop- continuation (or loop-termination) test immediately after the Continue statement executes. If a control variable’s increment occurs in the loop body after the Continue statement, the increment is skipped. Note that Continue Do is used for both Do...Loop While and Do While...Loop Created By Mayson Al-Duwais8

Example Continue Do When Increment is After Continue Do When Increment is Before Continue Do Dim i, j As Integer i = 1 j = 1 Do While (i < 6) j *= i Label1.Text &= ("i=" & i & “,j=" & j & vbCrLf) If j = 6 Then Label1.Text &= "Continue Do" & vbCrLf Continue Do End If i += 1 ‘ increment After continue Loop Dim i, j As Integer i = 1 j = 1 Do While (i < 6) j *= i Label1.Text &= ("i=" & i & “,j=" & j & vbCrLf) i += 1 ‘ increment Before continue If j = 6 Then Label1.Text &= "Continue Do" & vbCrLf Continue Do End If Loop Created By Mayson Al-Duwais9

Using Continue in Repetition Statements Exit and Continue in Nested Control Statements Exit For or Continue For can be used in a Do While...Loop and Do...Loop While statements, as long as that statement is nested in a For … Next statement. Created By Mayson Al-Duwais 10

Using Exit with Nested Repetition Statements of different types Dim result As Integer = 1 For index As Integer = 1 To 3 Do While result < 8 result += 1 Label1.Text &= "index = " & index & " result = " & result & vbCrLf If result = 3 Then Label1.Text &= "Continue While index = " & index & ", result = " & result & vbCrLf Continue Do End If ' End If If result = 5 Then Label1.Text &= "Exit While index = " & index & ", result = " & result & vbCrLf Exit Do End If ' End If Loop ' End Do While...Loop Next ' End For...Loop Created By Mayson Al-Duwais11

Using Exit with Nested Repetition Statements of different types Created By Mayson Al-Duwais12

Using Continue in Repetition Statements Exit and Continue in Nested Control Statements If there are nested loops of the same type (for example, a For … Next statement within a For … Next statement), the statement that immediately comes after the Exit or Continue statement is the one affected. Created By Mayson Al-Duwais13

Using Exit with Nested Repetition Statements of same types Dim result, index As Integer result = index = 1 Do While index < 3 index += 1 Do While result < 8 result += 1 Label1.Text &= "index = " & index & " result = " & result & vbCrLf If result = 3 Then Label1.Text &= "Continue While index = " & index & ", result = " & result & vbCrLf Continue Do End If ' End If If result = 5 Then Label1.Text &= "Exit While index = " & index & ", result = " & result & vbCrLf Exit Do End If ' End If Loop ' End Do While...Loop Created By Mayson Al-Duwais14

Using Exit with Nested Repetition Statements of same types Created By Mayson Al-Duwais15

Created By Mayson Al-Duwais16

Strings Variables and Strings Using Text Boxes for Input and Output String Properties and Methods: LengthToUpper TrimToLower IndexOfSubstring

Strings (continued) Concatenation The Empty String Initial Value of a String Widening and Narrowing Internal Documentation Line Continuation Scope of a Variable 18

19 String Literal A string literal is a sequence of characters surrounded by quotation marks. Examples: "hello" " " "#ab cde?"

20 String Variable A string variable is a name to which a string value can be assigned. Examples: country ssn word firstName

String Variable (continued) Declaration: Dim firstName As String 21 variable namedata type Assignment: firstName = "Fred"

String Variable (continued) You can declare a string variable and assign it a value at the same time. Dim firstName As String = "Fred" 22

String Variable You can assign the value of one string variable to another. Dim strVar1 As String = "Hello" Dim strVar2 As String = "Goodbye" strVar2 = strVar1 lstOutput.Items.Add(strVar2) Output: Hello 23

Variables and Strings Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim president As String president = "George Washington" lstOutput.Items.Add("president") lstOutput.Items.Add(president) End Sub Output: president George Washington 24

Data Conversion Because the contents of a text box is always a string, sometimes you must convert the input or output. dblVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) 25 converts a String to a Double converts a number to a string

Concatenation Combining two strings to make a new string quote1 = "We'll always " quote2 = "have Paris." quote = quote1 & quote2 txtOutput.Text = quote & " - Humphrey Bogart" Output: We'll always have Paris. - Humphrey Bogart 26 Created By Mayson Al-Duwais

Appending To append str to the string variable var var = var & str Or as a shortcut var &= str 27 Created By Mayson Al-Duwais

String Properties and Methods "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123 Hike".Length is 8. "123 Hike".ToLower is 123 hike. "a" & " bcd ".Trim & "efg" is abcdefg. 28 Created By Mayson Al-Duwais

Positions in a String Positions of characters in a string are numbered 0, 1, 2, …. Consider the string “Visual Basic”. Position 0: V Position 1: i Position 7: B Substring “al” begins at position 4 29 Created By Mayson Al-Duwais

Substring Method Let str be a string. str.Substring(m, n) is the substring of length n, beginning at position m in str. “Visual Basic”.Substring(2, 3) is “sua” “Visual Basic”.Substring(0, 1) is “V” 30 Created By Mayson Al-Duwais

IndexOf Method Let str1 and str2 be strings. str1.IndexOf(str2) is the position of the first occurrence of str2 in str1. (Note: Has value -1 if str2 is not a substring of str1.) "Visual Basic".IndexOf("is") is 1. "Visual Basic".IndexOf("si") is 9. "Visual Basic".IndexOf("ab") is Created By Mayson Al-Duwais

The Empty String The string "", which has no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = "" 32 Created By Mayson Al-Duwais

Initial Value of a String Variable By default the initial value is the keyword Nothing Strings can be given a different initial value as follows: Dim name As String = "Fred" 33 Created By Mayson Al-Duwais