Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2004, 09:35 AM
Registered User
 
Join Date: Nov 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default For loop problems

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:
Code:
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
 
Old November 29th, 2004, 12:44 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

    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...)
 
Old November 29th, 2004, 04:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help with loop... cbtidball Access VBA 4 February 22nd, 2006 09:55 AM
Loop Problems tready Excel VBA 1 November 15th, 2005 02:37 AM
Do While Loop Problems gmoney060 Classic ASP Databases 2 August 8th, 2004 08:52 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.