Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Both Visual Basic and C# support the concept of looping, executing a bit of code multiple times depending on a particular condition. I'll demonstrate two kinds of loops. For loops and While loops in both languages, starting in C#. I will open the file ProgrammingCSharp. aspx and then I'll save the file under a new name of LoopingCSharp.aspx. A For loop is a loop that executes multiple times and uses a numeric value so that you can increment the value or decrement it each time through the loop. In C# you use a For loop by creating keyword for and then starting with an opening parenthesis you put in three expressions separated with semicolons.
The first expression declares a variable and sets its initial value. I'll create an integer variable named counter and set its initial value to 1. Then, after a semicolon I put in a Boolean expression. This expression will be evaluated each time through the loop and as long as it's true the loop will continue. When the expression is false, the loop will terminate and the code will jump to after the loop. I'll use the expression counter < 3. Then finally after the second semicolon you say what you want to do each time through the loop and I'll use an increment operator, the ++ operator which means add one to the current value of that variable. After the For declaration you put in a pair of braces and any code that place between the braces, the code block will be executed once for each time through the loop.
I will use the output function that I've already created and I'll output the value, the value of counter is, and then I'll append the value of the variable using the + operator, which in C# is how you concatenate values together. I'll save and test the page. I'll select Debug > Start without Debugging. When the page opens I'll click the Run Code button and you will that I output the value of counter twice. When the value of the variable matches 3 though, the loop is terminated. So that's how you create a For loop.
Now let's take a look at how to create a While loop. I'll press Alt+Shift+Enter to expand to full screen, so I can see all of the codes. I'll place the While loop after the For loop. I would like to use the same variable. That is, the counter variable, but right now the counter variable is going to expire when the For loop is completed. In the .NET Framework when you declare a variable inside a looping construct, it means that; that variable expires when the code block expires. I'll fix this by moving the declaration of the counter variable above the for block.
Notice I'm not setting its defaulted value; I'm only declaring it. Then I'll go down to the for block and remove the data type from the variable declaration. Now I'm just using the variable that was already declared and the variable will still be available after the For loop is complete. I will move the cursor after the For loop and I'll put in a While loop. In a While loop you place a Boolean expression between a pair of parenthesis. I'll set my Boolean expression to a value of counter > 0, meaning that I'm going to loop as long as counter is at least 1. Then I'll copy and paste the output call. So I'm outputting the value of the counter and then, this is very important, you are responsible for changing the value of the variable. If you forget the next step, you will have created an infinite loop.
That is there is nothing in the While construct to actually change the value of the expression or the variable. So each time through the loop I'll use the expression counter -- meaning decrement the value by one. I'll save the changes and I'll run the page. When I run the code, I start off in the For loop going up by one. When I hit the value of 3, the For loop terminates and then I get into the While loop and I walk back down the values 3, 2, and 1.
So that's looping in C#. Now let's do the same thing in Visual Basic. I'll open the file ProgrammingVB.aspx, I'll save the file as LoopingVB.aspx and then I'll create the same sort of code, just in the C# version. I'll start by declaring the variable. I'll use the Dim keyword and then counter As Integer. Now I'll be able to use that variable in both the For loop and in the While loop. Here is the syntax for a Visual Basic For loop. You start off with the keyword For. Notice that the initial character is uppercase in the Visual Basic. Then the variable name and then the initial value with the assignment operator, the equal sign. Then you put in the to keyword and indicate how high you want to count.
When I press Enter, Visual Web Developer automatically puts in the keyword Next. In a For loop you start off with a For declaration and you end with a next declaration. Now I'll call my output function. Once again I'll start will a literal value and then I'm going to concatenate the value, which in Visual Basic is done with the ampersand character. So that's the completed For loop. Now I'll do the While loop. The While loop starts off with the keyword while and I'll use a Boolean expression as in the C# version where I'm looping as long as the counter is greater than 0. A While loop ends with the construct End While. Now I'll once again copy and paste my output command. I'll select the command, press Ctrl+C, place the cursor within the While loop and press Ctrl+V and then again in a While loop you are responsible for modifying the counter and in Visual Basic you can't use those simple increment and decrement operators of ++ and --.
So instead you say counter = counter - 1. So there is your For loop and your While loop in Visual Basic. I'll run the page selecting Debug > Start without Debugging. I'll click the Run Code button and there is the result in Visual Basic. So the capabilities and functionalities of the languages is the same, but the syntax that you use is different between Visual Basic and C#.
Get unlimited access to all courses for just $25/month.Become a member