Liberty BASIC Community Forum
« for/next loop »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 5:56pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: for/next loop  (Read 168 times)
Chester
New Member
Image


member is offline

Avatar




PM


Posts: 6
xx for/next loop
« Thread started on: Dec 29th, 2017, 2:07pm »

I want to use a for/next loop to print this sentence!
name$ = "The quick brown fox jumped over the lazy dog"
for



next
end

If you do not know how many words the user may type, how would you write this?

Note! I want the print-out to look like this:
The
quick
brown
fox
jumped
over
the
lazy
dog
« Last Edit: Dec 29th, 2017, 2:59pm by Chester » User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: for/next loop
« Reply #1 on: Dec 29th, 2017, 2:35pm »

Code:
name$ = "The quick brown fox jumped over the lazy dog"
for i=1 to 9    'we need to know HOW MUCH. So I just count that
    print word$(name$,i)
next
end 
User IP Logged

damned Dog in the Manger
Chester
New Member
Image


member is offline

Avatar




PM


Posts: 6
xx Re: for/next loop
« Reply #2 on: Dec 29th, 2017, 2:54pm »

on Dec 29th, 2017, 2:35pm, tsh73 wrote:
Code:
name$ = "The quick brown fox jumped over the lazy dog"
for i=1 to 9    'we need to know HOW MUCH. So I just count that
    print word$(name$,i)
next
end 

Note! if you do not know how many words that the user make type, how would you write this?
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5618
xx Re: for/next loop
« Reply #3 on: Dec 29th, 2017, 3:09pm »

Get the first word using Word$(), Then use a While Wend loop. While the word is not an empty string, print it, else when the word is an empty string you will leave the While Wend Loop.

Now code it 😀
User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: for/next loop
« Reply #4 on: Dec 29th, 2017, 3:12pm »

Quote:
if you do not know how many words

then you are not supposed to use FOR loop.
WHILE/WEND or DO/LOOP.
Code:
name$ = "The quick brown fox jumped over the lazy dog"
wrd$="*"
i=0
do
    i=i+1
    wrd$=word$(name$,i)
    print wrd$ 
loop until wrd$=""
end
 
User IP Logged

damned Dog in the Manger
tooanalytical


member is offline

Avatar




PM


Posts: 1165
xx Re: for/next loop
« Reply #5 on: Dec 30th, 2017, 01:41am »

This example uses two FOR loops.

Code:
sample$="the quick brown fox jumped over the lazy dog"

REM count the spaces and hold in spc
spc=0
sz=0
sz=len(sample$)
for i0=1 to sz
if mid$(sample$,i0,1)=" " then spc=spc+1
next i0

REM show each word from sample$ onto a new line each
for i1=1 to spc
print word$(sample$,i1)
next i1

END


 
User IP Logged

kokenge
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 25
xx Re: for/next loop
« Reply #6 on: Dec 30th, 2017, 02:15am »

I usually do it this way.
I know it's probably not good coding practice.

Code:
name$ = "The quick brown fox jumped over the lazy dog"
for i = 1 to len(name$)
    wrd$ = word$(name$,i)
    if wrd$ = "" then exit for
    print i;" ";wrd$ 
next i
end
 

User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls