1 / 7

Do Loops

Learn the fundamentals of Do loops in programming to efficiently repeat sequences of statements. Explore syntax, conditions, and examples of Do While Loop.

Download Presentation

Do Loops

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. Do Loops • A loop is one of the most important structures in programming. • Used to repeat a sequence of statements a number of times. • The Do loop repeats a sequence of statements either as long as or until a certain condition is true. Chapter 6

  2. Pseudocode and Flow Chart for a Do Loop Chapter 6

  3. Do Loop Syntax Condition is tested, If it is True, the loop is run. If it is False, the statements following the Loop statement are executed. Do Whilecondition statement(s) Loop These statements are inside the body of the loop and are run if the condition above is True. Code inside “Do While Loop” may not executed. If the evaluated condition is False, the code inside the loop are not executed. Chapter 6

  4. Lab sheet 6.1 – Fill List box with number Series Chapter 6

  5. Code – Without using Loop Private Sub btnDisplay_Click(...) Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num lstNumbers.Items.Add(num) End Sub Chapter 6

  6. Code – Do While Loop Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num Loop End Sub This program segment performs the same function as example 1b, but it is much shorter than the code in example 1a. Chapter 6

  7. Example: Repeat Request as Long as Response in Incorrect Dim passWord As String = "" Do While passWord <> "SHAZAM" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop. Chapter 6

More Related