Good programmers will choose the best path to complete a program. It's a good practice if you write concise or minimal code to achieve. This can be achieved by mastering the language features and learn by experience what the most efficient approach to achieve results is. But the problem is here; sometimes we can't know how to make it easy and simple to get the results we want, so we take the hard path and get to complicated steps. By experience and skill, it will be easy to figure out how write codes in a simple, short and symmetrical way.
The previous lesson is how to repeat or lets say " loop " the code block for many times. What if we want to repeat this block code only 2 times or for a certain number? Let's modify the Main method and greet 4 people only ( which means , the loop will be repeated 4 times only ). Look at the image below:
Let's link to the previous lessons and see how this example worked.
The counter variable i to the start value which is 1. VB will test it and see that it is less or equal than th end value, which is 4 here. If the start value is less than or equal the end value then the loop will work and the block code will be executed. When you press Enter ( after running the program ); VB will add 1 to the counter variable i and this way the For Loop will continue working and printing the certain number of statements on the screen. And the program will do the same steps again, see the block code if it is equal than or greater than and print the next statement and so for and so on.
The counter variable (i) is the tool that makes the loop repeat for certain number of times. If it is 1 time then it do it's own work, if it was 2 then it will do it's work also. Here is a table of the value variable (i) for the For Statement to check if it will loop again or not.
You can use any numbers you want for the start and end value. That was just an example of how many times you want the statement to stay looped or repeated.
School will start soon and I'm willing to write two more lessons and then I will try to keep giving lessons of the things I'm reading although I know I will be busy in my senior year and won't have that much time like now. The next lesson will be about classes and how to create your own.
No comments:
Post a Comment