Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional IF statement

Status
Not open for further replies.

pya

Technical User
Dec 18, 2020
40
AU
Hi,

I have the foll 3 lines in a QBASIC code.

Is there a way of combining all 3 lines or at least combine into 2 lines?

IF 2^K = N GOTO 100
IF 2^K < N GOTO 110
IF 2^K > N GOTO 120

Thanx

pya

 
Hi pya,
You can compute X = SGN(2 ^ K - N) + 2
which delivers results:
X = 1 when 2 ^ K < N
X = 2 when 2 ^ K = N
X = 3 when 2 ^ K > N
and then you can use for branching ON .. GOTO statement
like ON X GOTO 110, 100, 120

Here is an example which works in QB64:
on_goto.bas
Code:
[COLOR=#804040][b]CLS[/b][/color]
[COLOR=#804040][b]INPUT[/b][/color] [COLOR=#ff00ff]"K = "[/color], K
[COLOR=#804040][b]INPUT[/b][/color] [COLOR=#ff00ff]"N = "[/color], N

[COLOR=#0000ff]REM if 2 ^ K < N then X = 1[/color]
[COLOR=#0000ff]REM if 2 ^ K = N then X = 2[/color]
[COLOR=#0000ff]REM if 2 ^ K > N then X = 3[/color]
[COLOR=#0000ff]REM X = SGN(2 ^ K - N) + 2[/color]
[COLOR=#0000ff]REM PRINT "X = ", X[/color]

[COLOR=#804040][b]ON[/b][/color] [COLOR=#008080]SGN[/color]([COLOR=#ff00ff]2[/color] ^ K - N) + [COLOR=#ff00ff]2[/color] [COLOR=#804040][b]GOTO[/b][/color] less, equal, greater

less:
[COLOR=#804040][b]PRINT[/b][/color] [COLOR=#ff00ff]"2 ^ K < N"[/color]
[COLOR=#804040][b]END[/b][/color]

equal:
[COLOR=#804040][b]PRINT[/b][/color] [COLOR=#ff00ff]"2 ^ K = N"[/color]
[COLOR=#804040][b]END[/b][/color]

greater:
[COLOR=#804040][b]PRINT[/b][/color] [COLOR=#ff00ff]"2 ^ K > N"[/color]
[COLOR=#804040][b]END[/b][/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top