Infinite loop -- simple trap for beginners like me

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Infinite loop -- simple trap for beginners like me

Post by fzabkar »

This For ... Next loop was driving me mad until I figured out what was happening.

Code: Select all

Dim i As uByte

Print "Starting For ... Next loop"

For i = 0 To &HFF
    Print i,
    Sleep 10
Next i

Print "Completed For ... Next loop"

Sleep
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Infinite loop -- simple trap for beginners like me

Post by fxm »

This is why this trap is highlighted by a note in the documentation of FOR...NEXT:
Note: for integer data types, it is not possible to loop up to the highest possible value (or down to the lowest possible value) that can be stored in the variable type, because the loop only breaks when the incremented variable exceeds endvalue, which can never happen. For example, if you try to iterate an variable from 0 to 255, the loop will only break once the variable reaches 256 or more. Using a UByte variable for the counter wouldn't work, because although it can hold the numbers 0 to 255, it cannot hold 256. See Standard Data Type Limits to find the upper and lower limits for the standard data types.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Infinite loop -- simple trap for beginners like me

Post by D.J.Peters »

The topic are head or feet controlled loop.

No for/next or while/wend loop can count an ubyte from 0 to 255 but with a do/loop you can ask for an overflow of a ubyte,ushort,ulong ....

Joshy

Code: Select all

Dim i As ubyte
do
  Print i,
  Sleep 10 : i+=1
loop until i=0
Print "Completed do loop"
Sleep
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Infinite loop -- simple trap for beginners like me

Post by Vortex »

Hi fzabkar,

You should get a warning message like the following :

Code: Select all

D:\FreeBASIC>fbc32 --version
FreeBASIC Compiler - Version 1.09.0 (2021-12-31), built for win32 (32bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
standalone

Code: Select all

Dim i As uByte

For i = 0 To &HFF
    Print i,
Next i

Code: Select all

fbc32 loop.bas

loop.bas(3) warning 45(1): FOR counter variable is unable to exceed limit value
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Infinite loop -- simple trap for beginners like me

Post by dodicat »

Why not be consistent and have to translate everything?

Code: Select all

 
 
width loword(width)+&H2
declare function Roman(as longint) as string
Dim i As uByte

Print "Starting For ... Next loop"

For i = &H0 To &HFF-&H1
    Print Roman(i),
    Sleep &HA
Next i
print Roman(i)

Print "Completed For ... Next loop"

Sleep


function Roman(num as longint) as string

    dim as string m(...) = { "", "M", "MM", "MMM" }
    dim as string c(...) = { "",  "C",  "CC",  "CCC",  "CD", _
                            "D", "DC", "DCC", "DCCC", "CM" }
    dim as string x(...) = { "",  "X",  "XX",  "XXX",  "XL", _
                            "L", "LX", "LXX", "LXXX", "XC" }
    dim as string i(...) = { "",  "I",  "II",  "III",  "IV", _
                            "V", "VI", "VII", "VIII", "IX" }
    dim as string thousands = m(num \ 1000)
    dim as string hundreds = c((num mod 1000) \ 100)
    dim as string tens = x((num mod  100) \ 10)
    dim as string ones = i(num mod 10)

    dim as string ans = thousands + hundreds + tens + ones

    return ans
end function

 
Andrew92
Posts: 8
Joined: Apr 28, 2022 23:19

Re: Infinite loop -- simple trap for beginners like me

Post by Andrew92 »

Sorry, I'm a newb, What's this trap I'm looking for?
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: Infinite loop -- simple trap for beginners like me

Post by PaulSquires »

Andrew92 wrote: May 27, 2022 21:10 Sorry, I'm a newb, What's this trap I'm looking for?
Read fxm's reply to the original poster. It describes why the loop variable causes the problem.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Infinite loop -- simple trap for beginners like me

Post by dodicat »

Andrew92.
Please don't be confused with my last post, I am complaining about the use of &HFF instead of 255, which adds to the confusion.
ubyte, used as a for next variable, cannot hold the value 256 which is what the next keyword will generate in the loop after 255.
But unlike some compilers, the loop doesn't crash when ubyte exceeds it's bounds, it merely cycles ubyte back to 0.

Code: Select all


dim as long n
dim as ubyte u
print "ubyte","long"
for n =253 to 255
      u=n
      print u,n
next
print "__________________"
u=n
print u,n

sleep
 
(As per fxm's link)
Post Reply