|
Subject:
|
For loop problems
|
|
Posted By:
|
cube
|
Post Date:
|
11/25/2004 8:35:57 AM
|
Hi. i was wondering if anyone would beable to help me a bit.
in college yesterday we had to make a program to convert decimal numbers in to 8 bit binery. we werent aloud to use any premade functions to do this.
the way i decided to do it was this:
If number >= 128 Then
number = number - 128
text1 = 1
else
text1 = 0
End If
i had 8 of these statments, each one covering the next part of the binery (64,32,16,8,4,2,1)
it all worked fine and i got the grade. then i tryed to compress the code in to one loop, so i dint have 8 if statments, just one that would jump forward to the next number
unfortunatly i cant get it to work. im not shure what im doing wrong. im not getting any strange errors, the program runs fine. it just wont update the text boxes with the answers proply. i think my problem is that i dont fully understand how loops work yet and ive made a stupid mistake. i was wondering, if anyone would take a look at it and see if they can find what ive done wrong, to help pount me in the right direction?
this is only the 6th program ive made, so theres bound to be loads of problems
thanks for your time! http://www.tentative-title.com/binery/Form1.frm http://www.tentative-title.com/binery/Project1.vbp
|
|
Reply By:
|
BrianWren
|
Reply Date:
|
11/29/2004 11:44:44 AM
|
For i = 7 To 0 Step -1 If number >= 2^i Then number = number - 2^i text1 = text1 & "1" else text1 = text1 & "0" End If Next i
2^7 = 128, 2^0 = 1 Note that if you don’t have “txt = txt & ” but have “txt = ”, you will overwrite the value of txt for each iteration. (It is ‘binAry,’ not ‘binEry.’ Some teachers will ding you for things like that...)
|
|
Reply By:
|
marcostraf
|
Reply Date:
|
11/29/2004 3:01:37 PM
|
This code does not need to modify the number variable:
for i = 0 to 7 if (number AND (2^(7-i))) <> 0 then add "1" else add "0" endif next
Marco
|
|