Integer comparison: Difference between revisions

Line 1,694:
Note: Strictly speaking, it's preferable to use "==" when comparing integers as seen in this example. While the "=" sign will work as a comparison in most cases, technically it should be used for assignment, i.e. a = 3 when a is assigned the value of 3, as contrasted with a == 3, where the value of a is being compared with 3. FB will flag a warning when "==" is used to compare two single, doubles or floats since comparing real numbers can be inaccurate.
<lang futurebasic>
_window = 1
window 1
begin enum 1
_integer1Fld
_integer2Fld
_compareBtn
_messageLabel
end enum
 
local fn BuildWindow
dim as long n1, n2
window _window, @"Integer Comparison", (0,0,356,85)
 
textfield _integer1Fld,,, (20,44,112,21)
input "Enter two numbers (separated by a comma) to compare: "; n1, n2
TextFieldSetPlaceholderString( _integer1Fld, @"Integer 1" )
 
textfield _integer2Fld,,, (140,44,112,21)
if n1 < n2 then print : print n1; " is less than"; n2
TextFieldSetPlaceholderString( _integer2Fld, @"Integer 2" )
if n1 > n2 then print : print n1; " is greater than"; n2
 
if n1 == n2 then print : print n1; " equals"; n2
button _compareBtn,,, @"Compare", (253,38,90,32)
 
textlabel _messageLabel,, (18,20,320,16)
ControlSetAlignment( _messageLabel, NSTextAlignmentCenter )
end fn
 
local fn DoDialog( ev as long, tag as long )
long int1, int2
 
select ( ev )
case _btnClick
select ( tag )
case _compareBtn
int1 = fn ControlIntegerValue( _integer1Fld )
int2 = fn ControlIntegerValue( _integer2Fld )
 
if ( int1 < int2 ) then textlabel _messageLabel, @"The first integer is less than the second integer."
if ( int1 == int2 ) then textlabel _messageLabel, @"The first integer is equal to the second integer."
if ( int1 > int2 ) then textlabel _messageLabel, @"The first integer is greater than the second integer."
 
end select
 
case _controlTextDidChange
textlabel _messageLabel, @""
end select
end fn
 
fn BuildWindow
 
on dialog fn DoDialog
 
HandleEvents</lang>
408

edits