Image
Interordi Menu
Mega X.exe
Forum Ghost
Offline
6444 posts
Quote

All right, I'll be frank. We've just started loops in my programming class (we're learning basic) and there's several things I don't get.

See, I have to create three programs using Loops, IF statements and variables, but I don't understand loops well enough to code them.

For instance, one of them is an application that allows the user to enter a series of integers. The application will then display the sum of the odd integers and the sum of the even integers.

Another one is a program that allows the user to enter the gender (either F or M) and GPA for one or more students. The application should calculate the average GPA for all students, the average GPA for male students and the average GPA for female students.

The last one is an application that allows the user to determine how many Fibonacci numbers are displayed.

I know that the first one uses a ForNext, and the last two use a DoLoop. But otherwise I'm hopelessly confused.

Staff Backer Doctacosa
Admin
SciLab Official
Benevolent Dictator
Offline
6437 posts
Princess Celestia
Princess Celestia
Got all items in the AFD2012 event!
Acquired on 1 April 2012
Squid beaker
Squid beaker
Earned all 150 original CL achievements
Acquired on 17 January 2016
Unity.EXE emblem
Unity.EXE emblem
Defeat Bass.EXE in the AFD2013 event!
Acquired on 1 April 2013
Zenny
Zenny
Unlock all of the main forum features!
Acquired on 1 April 2014
Lilly Satou
Lilly Satou
Acquired on 1 April 2012

... and 25 more
Quote

Here's a basic example of a For loop in Basic. The exact syntax will change depending of the flavor you're working with.

[font=Courier]for $i = 1 to 10
print $i
next $i[/font]

This will increment the value $i from 1 to 10 and display it every time. As such, you'll get:
[font=Courier]1
2
3
...
10[/font]

The loop will end when the final condition is met (in this case, when $i = 10).


Do / Loop work on a different way: a block of code will loop indefinitely as long as a condition is NOT met. As such, you could have:

[font=Courier]$i = 0
do
$i = $i + 1
print $i
loop while ($i < 10)[/font]

This will give the same result as the above code using For / Next.

Edited by Dr. Cossack on May 8, 2006 at 11:17:15.


The admin formerly known as Dr. Cossack.

Looking for me elsewhere? Maybe look at my Fediverse account for some more-or-less random postings! If you're a gamer, check out my Osmium profile. I'm building that tool!

Mega X.exe
Forum Ghost
Offline
6444 posts
Quote

Sorry Doc, I meant Visual Basic. :hehe:

Edited by Mega X.exe on May 8, 2006 at 13:31:28.

Staff Backer Doctacosa
Admin
SciLab Official
Benevolent Dictator
Offline
6437 posts
Princess Celestia
Princess Celestia
Got all items in the AFD2012 event!
Acquired on 1 April 2012
Squid beaker
Squid beaker
Earned all 150 original CL achievements
Acquired on 17 January 2016
Unity.EXE emblem
Unity.EXE emblem
Defeat Bass.EXE in the AFD2013 event!
Acquired on 1 April 2013
Zenny
Zenny
Unlock all of the main forum features!
Acquired on 1 April 2014
Lilly Satou
Lilly Satou
Acquired on 1 April 2012

... and 25 more
Quote

Quote:
Originally posted by Mega X.exe
Sorry Doc, I meant Visual Basic. ^_^;;


That doesn't change the validity of the code I gave. Try to put both examples, without the dollar signs, on a command button put in the middle of an empty form. The numbers will appear on the form itself. :) (I've edit the Do / Loop example to fix a small bug)

Now that I think about it, the following example would be closer to my For / Next one:
[font=Courier]$i = 0
do
$i = $i + 1
print $i
loop until ($i = 10)[/font]

Do / Loop Until will run in circles as long as the condition isn't met, which is $i = 10 in this case.

Edited by Dr. Cossack on May 8, 2006 at 11:18:34.


The admin formerly known as Dr. Cossack.

Looking for me elsewhere? Maybe look at my Fediverse account for some more-or-less random postings! If you're a gamer, check out my Osmium profile. I'm building that tool!

Mega X.exe
Forum Ghost
Offline
6444 posts
Quote

I think that I understand loops now. At any rate it should come with practice. Anyway, I've got some code that doesn't seem to work, so if anyone here can proofread it for me...

Quote:
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'declare variables
Dim intA As Integer
Dim intB As Integer
Dim intNum As Integer
Dim intOdd As Integer
Dim intEven As Integer

'assign input to variables
intA = Convert.ToInt32(Me.txtIntA.Text)
intB = Convert.ToInt32(Me.txtIntA.Text)


'runs the loop and calculates the sum
If IsNumeric(intA And intB) Then
End If
For intNum = intA To intB
If intNum Mod 2 = 0 Then
intEven = (intNum + intEven)
Else : intOdd = (intNum + intOdd)
End If
Next

'display odd sum and even sum
Me.Odd.Text = Convert.ToString(intOdd)
Me.Even.Text = Convert.ToString(intEven)
End Sub


Edited by Mega X.exe on May 8, 2006 at 13:30:53.

Staff Backer Doctacosa
Admin
SciLab Official
Benevolent Dictator
Offline
6437 posts
Princess Celestia
Princess Celestia
Got all items in the AFD2012 event!
Acquired on 1 April 2012
Squid beaker
Squid beaker
Earned all 150 original CL achievements
Acquired on 17 January 2016
Unity.EXE emblem
Unity.EXE emblem
Defeat Bass.EXE in the AFD2013 event!
Acquired on 1 April 2013
Zenny
Zenny
Unlock all of the main forum features!
Acquired on 1 April 2014
Lilly Satou
Lilly Satou
Acquired on 1 April 2012

... and 25 more
Quote

Is that in VB.NET, by any chance? I didn't touch any versions after VB6, and that code looks odd to me. The examples I gave are all for VB6.


The admin formerly known as Dr. Cossack.

Looking for me elsewhere? Maybe look at my Fediverse account for some more-or-less random postings! If you're a gamer, check out my Osmium profile. I'm building that tool!

Mega X.exe
Forum Ghost
Offline
6444 posts
Quote

Yeah, it's .NET. But now that I look at it again, there's an extra End If in there that might be throwing things off.