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.

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 7 – Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1A) UTPA – Fall 2011.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Introduction to Problem Solving and Control Statements.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Problem Solving and Control Statements: Part 2
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Problem Solving and Control Statements: Part 2
Control Statements: Part 2
Control Structures: 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 5 – Control Structures: Part 2
The University of Texas Rio Grande Valley
The University of Texas – Pan American
- Additional C Statements
The University of Texas – Pan American
Chapter 3: Introduction to Problem Solving and Control Statements
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Program Control Topics While loop For loop Switch statement
Chapter 6 Control Statements: Part 2
Problem Solving and Control Statements
Case & Repetitive Statements
2.6 The if/else Selection Structure
Chapter 5 – Control Structures: Part 2
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Presentation transcript:

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 lecture slides. – Xiang Lian

Objectives In this chapter, you will: Learn more control structures Repetition statements For … Next, Do … Loop While, Do … Loop Until Selection Select … Case Know how to use Exit and Continue statements to break or terminate the current iteration Learn how to use logical operators

In the Last Class: Repetition Structure Visual Basic provides 7 repetition statements Do While … Loop While … End While Do Until … Loop Do … Loop While Do … Loop Until For … Next For Each … Next

Example of For … Next Repetition Statement Output even numbers between 2 and 10 For counter As Integer = 2 To 10 Step 2 outputLabel.Text &= counter & " " Next

Discussions on For … Next Repetition Statement For counter As Integer = 2 To 10 Step 2 … Next Keywords: For, To, Step, Next "counter As Integer" declares a counter of integer data type counter – control variable name Integer – control variable type Initial value of control variable – 2 Final value of control variable – 10

Discussions on For … Next Repetition Statement (cont'd) For counter As Integer = 2 To 10 Step 2 … Next Increment of control variable – Step 2 If "Step 2" is missing, the default value is 1 Output all integers from 2 to 10 For counter As Integer = 2 To 10 outputLabel.Text &= counter & " " Next Result: 2, 3, 4, 5, 6, 7, 8, 9, 10

General Form of a For … Next Statement For initialization To finalValue Step increment statement Next

Other Examples of For … Next (1) Declaring the control variable before a For…Next Statement Dim counter As Integer For counter = 2 To 10 Step 2 outputLabel.Text &=counter & " " Next Using expressions in the For…Next statement For j As Integer = x To 4*x*y Step y \ x

Other Examples of For … Next (2) Default increment: 1 For i = 1 To 100 For i = 1 To 100 Step 1 Decrement For i = 100 To 1 Step -1

Example 5.5: InterestCalculator.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

Example 5.5: InterestCalculator.vb (cont'd) NumericUpDown control Minimum property Maximum property Increment property Value property

Example 5.5: InterestCalculator.vb (cont'd) Val(…) function Convert strings to numbers Dim principal As Decimal = Val(principalTextBox.Text) Ignores white space in the string E.g. "33 5" will be converted to 335 vbTab Format by adding a tab

Example 5.5: InterestCalculator.vb (cont'd) String.Format("{0:C}", amount) Output variable amount in currency format E.g., $1,050.00 TextChanged event of controls TextBox and NumericUpDown resultListBox.Items.Clear()

Nested Repetition Statements For i = 1 To 10 Step 1 For j = 1 To 20 Step 2 … Next i Next j

Example 5.8: SquareOfCharacters.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html

Example 5.8: SquareOfCharacters.vb (cont'd) For row As Integer = 1 To sideLength For column As Integer = 1 To sideLength outputTextBox.AppendText(fillCharacter & " ") Next column outputTextBox.AppendText(vbCrLf) Next row

Do…Loop While and Do…Loop Until Repetition Statements The loop body is always executed at least once Dim product As Integer = 1 Do product = product * 3 Loop While product <=100 ------------------------------------------------------------------------------- Loop Until product >100

Use Exit to Terminate Repetition Statements Exit Do Terminate the repetition statements such as: Do While…Loop, Do Until…Loop, Do…Loop While, Do…Loop Until Exit For Terminate For…Next Exit While Terminate While…End While Exit Select Terminate Select…Case

Use Continue in Repetition Statements Continue statement only terminates the current iteration Continue Do Terminate the repetition statements such as: Do While…Loop, Do Until…Loop, Do…Loop While, Do…Loop Until Continue For Terminate For…Next Continue While Terminate While…End While

In the Last Class: Selection Structure If … Then If … Then … Else Select … Case [grade>=60] display "passed" [grade<60]

Multiple-Selection Statement: Select … Case Select Case grade Case 100 perfectScoreCount += 1 ' increment perfectScoreCount aCount += 1 ' increment aCount Case 90 To 99 ' grade was between 90 and 99 Case 80 To 89 ' grade was between 80 and 89 bCount += 1 ' increment bCount Case 70 To 79 ' grade was between 70 and 79 cCount += 1 ' increment cCount Case 60 To 69 ' grade was between 60 and 69 dCount += 1 ' increment dCount Case Else ' grade was less than 60 fCount += 1 ' increment fCount End Select

Multiple-Selection Statement: Select … Case Case Else is optional If no case matches and there is no Case Else, then program control continues with the first statement after Select … Case End Select terminates the Select … Case statement

Example 5.9: ClassAverage.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html TextBox gradeTextBox.Focus() gives the focus to the gradeTextBox String.Empty

Expression 1 And Expression 2 Logical Operators Logical And operator If gender = "F" And age >=65 Then seniorFemales += 1 End If Expression 1 Expression 2 Expression 1 And Expression 2 False True

Logical Operators (cont'd) Logical Or Operator Expression 1 Expression 2 Expression 1 Or Expression 2 False True

Short-Circuit Evaluation AndAlso If gender = "F" AndAlso age >=65 If gender is not "F", then "age >=65" will not be evaluated OrElse If gender = "F" OrElse age >=65 If gender is "F", then "age >=65" will not be evalutated

Other Logical Operators Logical Xor operator Logical Not operator If Not (value = 0) Then … Expression 1 Expression 2 Expression 1 Xor Expression 2 False True

Rules of Operator Precedence priority ^ +, - (sign operations) *, / \ Mod +, - (addition and subtraction) & =, <>, <, <=, >, >= (equality and relational) Not And, AndAlso Or, OrElse Xor =, +=, -=, *=, /=, \=, ^=, &= high low