1 / 18

Unit 2 for Computer Science I - VB

Unit 2 for Computer Science I - VB. Loops Using help You can nest for loops Try Catch. Loop Statements. Loops are used to repeat the same code several times. There are two types of loops: Definite Loop – Use this when you know how many loops need to be run.

archer
Download Presentation

Unit 2 for Computer Science I - VB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 2 for Computer Science I - VB • Loops • Using help • You can nest for loops • Try Catch

  2. Loop Statements • Loops are used to repeat the same code several times. There are two types of loops: • Definite Loop – Use this when you know how many loops need to be run. • An example would be if you need to add up a range of numbers. You know the starting and ending number in the range that needs to be added. • The For Loops are definite loops • Indefinite Loop – Use this when don’t know how many loops must be run. • An example would be a user playing a game. The loop would keep starting over until a user chooses to quit. • The While Loops are indefinite loops

  3. For …Next Statementhttp://msdn.microsoft.com/en-us/library/5z06z1kb(VS.90).aspx • Repeats a group of statements a specified number of times. [] are used to show optional parts • The syntax for the For …Next Loop is Forvariable=start valueToend value [Stepincrement] [Statement (s)] Next • A couple of side notes • The variable is local to the loop. I typically use one letter as the variable. • The step is optional and default value is 1. It can be negative. • Exit For – This statement can go inside the loop and be used to exit the loop at any time. The red are the keywords, and everything else you set up.

  4. For …Next Statement Example For Loop Example: The following adds 0+1+2+…+9+10 all together into the variable intSum. Forx=0To10Step1 intSum = intSum + x Next

  5. For …Each Statement • Repeats a group of statements for each element in a collection. Collections will be discussed later. • The syntax for the For …Each Loop is For Each element [ As datatype ] In group [statements ] Next [ element ]

  6. For …Each examples Mystring is a collection and x is character. It iterates through each character in mystring. For Each x As Char In mystring ListBox1.Items.Add(x) Next Me.Controls is a collection objects on a form. Each object will then be selected and modified by the loop. For Each thisControl As Control InMe.Controls thisControl.BackColor = Color.Red Next

  7. MOD and the INT function • Add this to your list of commands • The mod function will return the remainder portion of a division problem. • Example: • 6 MOD 4 will return 2 • INT(6 /4) will return 1 • 6/4 will return 1.5

  8. Loops examples • Use a for loop to write your name 20 times to a listbox • Use a for loop to write the number 10, 20, 30 … 90, 100 to a listbox. • Use a for loop to write all the numbers between 1 and 100 that are divisible by 3 and 4.

  9. Assignments • Cook a Turkey

  10. Indefinite Loops • There are 4 types of indefinite loops, all of which are about the same thing but slightly different. • DoWhileconditionStatement (s)Loop The loop continues as long as the condition is true. The loop may never start because the test is at the beginning. • Example:intX = 2DoWhileintX < =50listbox.items.add(intX) intX = intX + 2Loop This example will print out all the even numbers from 1 to 50.

  11. Indefinite Loops (continued) 2. DoStatement (s)Loop While condition The loop continues as long as the condition is true. The loop will always run through once because the condition is checked at the end of the loop. • Example:intX = 2Dolistbox.item.add(intX) intX = intX + 2Loop WhileintX < =50 This example will print out all the even numbers from 1 to 50.

  12. Indefinite Loops (continued) 3. DoUntil conditionStatement (s)Loop The loop continues as long as the condition is false. The loop may never start because the test is at the beginning. • Example:intX = 2DoUntilintX > 50listbox.item.add(intX) intX = intX + 2Loop This example will print out all the even numbers from 1 to 50.

  13. Indefinite Loops (continued) 4. DoStatement (s)Loop Until condition The loop continues as long as the condition is false. The loop will always run through once because the condition is checked at the end of the loop. • Example:intX = 2Dolistbox.item.add(intX) intX = intX + 2Loop UntilintX > 50 This example will print out all the even numbers from 1 to 50.

  14. Loops examples • Use a while loop to write your name 20 times to the a listbox. (Not a good choice for while loop because we know how many times to do this.) • Use a while loop to find the sum of all the numbers entered in by a user using an inputbox. • Use a while loop to find the average of all the numbers entered in by a user using an inputbox.

  15. Example for finding average • Dim inputstr As String • 'Dim inputint As Int16 • 'Dim sumint As Int16 • 'Dim inputCounterint As Int16 • 'inputstr = InputBox("Enter a number", "Blue Bar", "0") • 'inputCounterint = inputCounterint + 1 • 'Do While inputstr <> String.Empty • ' 'Convert str to integer • ' Integer.TryParse(inputstr, inputint) • ' ListBox1.Items.Add(inputint) • ' 'Update the sum of the numbers • ' sumint = sumint + inputint • ' 'Ask user for next number • ' inputstr = InputBox("Enter a number", "crap", "0") • ' inputCounterint = inputCounterint + 1 • 'Loop • 'ListBox1.Items.Add("__________") • 'ListBox1.Items.Add("Average = " & sumint / (inputCounterint - 1))

  16. Loop Example • Make an input box ask for a password and make the password your name. The input box should continue to show up until the correct password is entered. • Make the input box only display a message to enter a password and say only 3 guesses left. Each incorrect guess causes guess left to decrement. At end of guesses the program exits.

  17. Try Catch • Try Catch statements are commands that are used to control for an error. Simply put it is a program crash that you control. The following are some typical errors: • Dividing by zero. • Trying to read a file that has been deleted. • Using values incompatible with a data type. • Make a connection to anther computer. • It is best not to use Try Catch statements. If you know a program will crash then you should program against it.

  18. Try Catch syntax Try statement(s) – where error may occur Catch ex As Exception statement(s) – what to do if error occurs End Try • There is much more to this but this is all we need to know. • Example: Make a variable go beyond an int16 range.

More Related