WHAT LOOPS AREUSED FOR? • Almost all the programming languages provide a concept called loop, which helps in executing one or more statements up to a desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly.
3.
• USING ITERATIONS • •Using Do until . . . Loop • Using Do . . . Loop Until • Using Do While. . . Loop • Using Do. . . Loop While • • Using While … End While • Using for next statement • Using Nested for next statements
4.
• We wantto count from 1 to 10 using different looping methods 1- USING DO UNTIL . . . LOOP • the do until checks the condition , if it is true, execute, if it is false exit the loop • Console.WriteLine("Do Until . . . Loop") • Dim i as integer = 0 • Do Until (i = 10) • i += 1 • Console.WriteLine(i) • Loop
5.
2- USING DO. . . LOOP UNTIL • The do loop until executes first, then checks the condition, if it is true, execute, if it is false exit the loop. It means the statement inside the loop will be executed once at least. • Console.WriteLine("Do . . . Loop Until ") • Dim i as integer = 0 • Do • i+= 1 • Console.WriteLine(i) • Loop Until (i = 10)
6.
3- USING DOWHILE … LOOP Console.WriteLine("Do While … Loop") • Dim i as integer = 0 • Do While (i < 10) • i += 1 • Console.WriteLine(i) • Loop 4- USING DO … LOOP WHILE Console.WriteLine("Do While … Loop") • Dim i as integer = 0 • Do • i+= 1 • Console.WriteLine(i) • Loop While (i < 10)
7.
5- USING WHILE… END WHILE LOOP • Console.WriteLine("While … End While") • Dim i as integer = 0 • While (i< 10) • i += 1 • Console.WriteLine(i) • End While 6- USING FOR NEXT LOOP • Console.WriteLine("For Next") • For i = 1 To 10 STEP 3 • Console.WriteLine(i) • Next •
8.
• NESTED LOOPS •Print cross table from 1 to 5 • 'print cross table using for next • For i = 1 To 5 • For j= 1 To 5 • Console.WriteLine(i & " * " & j & " = " & i* j) • Next • Next
9.
• EXAMPLES -USE FOR NEXT STATEMENT TO COUNT FROM 1 TO 10 • For i As Integer = 1 To 10 • Console.WriteLine(i) • Next • COUNT FROM 10 TO 1 • For i As Integer = 10 To 1 Step -1 • Console.WriteLine(i) • Next • PRINT ODD NUMBERS FROM 1 TO 10 • For i As Integer = 1 To 10 Step 2 • Console.WriteLine(i) • Next • PRINT EVEN NUMBERS FROM 10 TO 0 • For i As Integer = 10 To 0 Step -2 • Console.WriteLine(i) • Next
10.
Nested loop example •RIGHT TRIANGLE, WRITE A PROGRAM TO FIND ALL PYTHAGOREAN TRIPLES FOR SIDE1, SIDE2 AND HYPOTENUSE, OF PYTHAGOREAN TRIPLES • Dim x As Integer 'perfect squares • For i = 1 To 30 • For j = 1 To 30 • x = Math.Sqrt(i ^ 2 + j ^ 2) • If x ^ 2 = i ^ 2 + j ^ 2 Then Console.WriteLine(i & "," & j & " = " & x) • Next • Next
11.
• WRITE AFOR NEXT LOOP TO PRINT ASCII CHARACTERS FROM 0 TO 255 • Dim I As Integer • For I = 0 To 255 • Console.WriteLine(I & vbTab & Chr(I)) • Next
12.
• CALCULATE SQUAREROOT OF A PERFECT SQUARE NUMBER N USING FOR NEXT • Dim N As Integer = 36 • Dim Root As Integer • For i = 1 To N • If i * i = N Then • Root = i • Exit For • End If • Next • Console.Write(Root)
13.
• DESIGN AMENU TO SIMULATE A CALCULATOR THAT IS DISPLAYED AS FOLLOWS • Add 2 numbers • Subtract 2 numbers • Multiply 2 numbers • Divide 2 numbers • Exit • Please select a number
14.
• According toselection, mathematical operation is performed and display the result, the main menu is then displayed again • Dim x, y, k As Integer • Do While True • Console.WriteLine("1- Add 2 numbers") • Console.WriteLine("2- Subtract 2 numbers") • Console.WriteLine("3- Multiply 2 numbers") • Console.WriteLine("4- Divide 2 numbers") • Console.WriteLine("5- Exit") • Console.WriteLine("Please select a number") • k = Val(Console.ReadLine()) • If k >= 1 And k <= 4 Then • Console.Write("Enter First Number: ") • x = Console.ReadLine • Console.Write("Enter Second Number: ") • y = Console.ReadLine • End If
15.
• Console.ForegroundColor =ConsoleColor.DarkGreen • Select Case k • Case 1 • Console.WriteLine(x & " + " & y & " = " & x + y) • Case 2 • Console.WriteLine(x & " - " & y & " = " & x - y) • Case 3 • Console.WriteLine(x & " x " & y & " = " & x * y) • Case 4 • Console.WriteLine(x & " ÷ " & y & " = " & x / y) • Case 5 • Exit Do • Case Else • Console.ForegroundColor = ConsoleColor.Red • Console.WriteLine("Invalid selection") • End Select • Console.ForegroundColor = ConsoleColor.White • Loop • Console.ReadLine() • End Sub • End Module