Welcome to Dream.In.Code
Become an Expert!

Join 149,605 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,900 people online right now. Registration is fast and FREE... Join Now!




BASIC loops

 
Reply to this topicStart new topic

BASIC loops, Just a lil curious

Revitalized
25 Sep, 2007 - 09:59 AM
Post #1

New D.I.C Head
*

Joined: 11 Sep, 2007
Posts: 17


My Contributions
So I am programming in BASIC Right now, And I just finished our assignment, but I'm confused as To why a slight variation in loop makes the program work.

This is the working code:
CODE
Repeat
    factors = 0
storage = Input("Enter a number ")
    For counter = storage To 1 Step -1
        If (storage Mod counter = 0)
            factors = factors + 1
        EndIf
    Next
Until factors = 2
Print "Prime!"

As you can tell, that is a program that waits until the user inputs a prime number. My curosity stems from the fact that my first attempt at this program didn't work. I used another Repeat...Until instead of a for loop, and it did not work. It was something like this:
CODE

spy = 0
Repeat
    factors = 0
storage = Input("Enter a number ")
    Repeat
        If (storage Mod counter = 0)
            factors = factors + 1
        EndIf
       spy = spy + 1
    Until (storage = spy)
Until factors = 2
Print "Prime!"
This code doesn't work, and I was wondering why?

(Well actually I think my code was like that, I accidently deleted my original code so this is based off of my memory.) If I can't get an answer, it's okay, as I've already done it with a For loop anyways.

I use the blitzbasic compiler. Thanks.

This post has been edited by Revitalized: 25 Sep, 2007 - 10:00 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: BASIC Loops
25 Sep, 2007 - 12:12 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Probably because in the second example there that doesn't work, your value of counter never gets changed. You never increment or decrement it. In the first example the value of counter is being changed each iteration through the for loop.

So that is probably your problem assuming that is not part of what you forgot to add reconstructing this. smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month