Pages

Friday, 29 March 2013

How to Use the For loop in Visual Basic.net Console Application?

For Loop is a kind of iteration statement which allows the statement repeatedly executed. The statement repeat until the limitation or the condition of the for loop is TRUE. This kind of iteration statement is always use if the statement needs to repeat many time.

To print many times of "Hello World!" must need to use the for loop so that the line of code written in a simple. The code below a sample of for loop statement that print 10 times of "Hello World!".

Module Module1
    Sub Main()
        For x As Integer = 1 To 10
            Console.WriteLine("Hello World!")
        Next
        Console.ReadLine()
    End Sub
End Module


Remember:

The X on the for loop statement above is the variable assign as integer value when the statement repeat.

The 1 value is Initialization or the Start value of the for loop statement and the 10 value is the limitation of the for loop statement.

        For x As Integer = Start To Limitation
        

        Next

The Programmer can also use the manual coding to print 10 times of "Hello World!" but what if 1000 times to repeat the "Hello World". Is it the programmer use the manual code? its better that the programmer use the for loop statement to do that kind of coding.

Base on the code above, The Programmer change the Value 10 into 1000 in the for loop statement above. It shows the code below to print 1000 times of "Hello World!"

Module Module1
    Sub Main()
        For x As Integer = 1 To 1000
            Console.WriteLine("Hello World!")
        Next
        Console.ReadLine()
    End Sub
End Module


How to Print Numbers from the Highest number to the lowest Number Using For loop?

         To print numbers from 10 to 1 using the for loop is just change the position of the Start and the Limitation of for loop statement.

Module Module1
    Sub Main()
        For x As Integer = 10 To 1 Step -1
            Console.WriteLine(x)
        Next
        Console.ReadLine()
    End Sub
End Module


How it is Happen? 

         The Start value of x is 10 and the limitation value of the for loop statement is 1 and if you notice that is has a Step -1 in a for loop statement. Step -1 is a function to do the numbers loop in to backward counting.

here is the further explanation why is it happen that the numbers counting from the highest into lowest value.

Remember 10 is the starting and 1 is the Limitation and the for loop statement must be true, if the statement is false then the Step -1 will trigger and the  value of x is deducted by 1.

Let's test now...

        the starting value of x is 10
  • Meaning to say that 10 must be the first value to print because it  is  the first value of x.
        limitation value is 1
        
        the condition is this:
             is it 10 is equal to 1?  if the Answer is FALSE then Step -1 will action and the value deducted into 1 and the new value of x is 9 and the new value of x will print which is 9. so we have already 10 and 9 printed into the screen which is show like these;

                    10

                                            9
      the for loop statement repeat until the condition is TRUE which is if the value of x is 1. It shows the output below if the for loop is stop to repeat the statement.

No comments:

Post a Comment