Repository navigation
If Then
Home › Control Flow Statements › If...Then
Control flow statement for conditional branching
If expression Then [statement(s)] [Else [statement(s)]] [End If]
If expression Then : [statement(s)] [Else [statement(s)]] : End If
If expression Then
[statement(s)]
[ ElseIf expression Then ]
[statement(s)]
[ Else ]
[statement(s)]If...Then is a way to make decisions.
It is a mechanism to execute code only if a condition is true, and can provide alternative code to execute based on more conditions:
expression can be one of several forms:
x = 5
x >= 5 And x <= 10
y <> 0 AndAlso x \ y = 1
(in this case, "x \ y = 1" will only be evaluated if "y <> 0" is True)
Both multi-line and single-line Ifs can be nested. In the latter case, the optional End Ifs can be useful to control where nested Ifs begin and end.
The multi-line syntax allows several Elseifs (but none after a Else) and tests can be nested (there must be in this case as many End Ifs as Ifs).
If the condition of the If is not true, those of Elseifs blocks are tested in succession:
In the -lang fb and -lang fblite dialects, colons (:) can be used instead of newlines to construct multi-line If blocks on a single line.
Note: The single-line syntax If...Goto, as shortcut for If...Then Goto, is deprecated and it only exists for compatibility with QB.
- In the -lang qb and -lang fblite dialects, variables declared inside an
If..Thenblock have a procedure-wide scope as in QB In the -lang fb and -lang deprecated dialects, variables declared inside anIf..Thenblock are visible only inside the block, and cannot be accessed outside it. To access duplicated symbols defined as global outside this block, add one or preferably two dot(s) as prefix:.SomeSymbolor preferably..SomeSymbol(or only..SomeSymbolif inside a With..End With block).
In the -lang qb dialect, if there is a new line or a single-line comment (') directly after THEN, then the IF will be multi-line. A colon, aRemor any other statement will result in a single-line IF.
In the -lang fb and -lang fblite dialects, if there is a new line, a single-line comment ('), a colon (:), or aRemstatement directly after THEN, then the IF will be multi-line. Any other statement will result in a single-line IF.
- END IF was not supported in single-line IFs in QBASIC.
'' Here is a simple "compute the square root" code using a single-line if...then for the decision,
'' but with multiple statements extended with colons (:)
Dim As Double d , r
r = -1
d = 2
'd = -3
If d > 0 Then r = Sqr(d) : Print "square root computed:" Else r = 0 : Print "square root not computed:"
Print r
Sleep'' Here is a simple "guess the number" game using a multi-line if...then for a decision.
Dim As Integer num, guess
Randomize
num = Int(Rnd * 10) + 1 'Create a random number between 1 and 10...
Print "guess the number between 1 and 10 (or CTRL-C to abort)"
Do 'Start a loop
Input "Guess"; guess 'Input a number from the user
If guess > 10 OrElse guess < 1 Then 'The user's guess is out of range
Print "The number can't be greater then 10 or less than 1!"
ElseIf guess > num Then 'The user's guess is too high
Print "Too high"
ElseIf guess < num Then 'The user's guess is too low
Print "Too low"
Else 'The user guessed the right number!
Print "Correct!"
Exit Do 'Exit the loop
End If
Loop 'Go back to the start of the loop
SleepCategory: Control Flow Statements
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing