Delphi Programming
Explore
Main Page
All Pages
Community
Interactive Maps
Recent Blog Posts
Top Content
most_popular
most_visited
Delphi 7
Object Pascal
Delphi 6
Creating DLLs
Simple Calculator Tutorial
Delphi 2007 for Win32
Delphi 8
highest_ratings
newly_changed
3rd Party Components
Delphi 7
Good Quality Applications Built With Delphi
Delphi Release Dates
Open Paradox db without password
RTLVersion Constant
Borland Compiler Conditional Defines
Community
top_users
Community portal
forum
Recent blog posts
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Delphi Programming
2,929
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Recent Blog Posts
Top Content
most_popular
most_visited
Delphi 7
Object Pascal
Delphi 6
Creating DLLs
Simple Calculator Tutorial
Delphi 2007 for Win32
Delphi 8
highest_ratings
newly_changed
3rd Party Components
Delphi 7
Good Quality Applications Built With Delphi
Delphi Release Dates
Open Paradox db without password
RTLVersion Constant
Borland Compiler Conditional Defines
Community
top_users
Community portal
forum
Recent blog posts
Editing
Object Pascal
(section)
Back to page
Edit
Edit source
View history
Talk (3)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Conditional Statements == A conditional statement is used to execute either one of the statements it contains or none of them, depending on some test. There are two basic flavors of conditional statements: if statements and case statements. '''If Statements''' procedure TForm1.Button1Click(Sender: TObject); begin { simple if statement } if CheckBox1.Checked then ShowMessage ('CheckBox1 is checked') end; procedure TForm1.Button2Click(Sender: TObject); begin // if-then-else statement if CheckBox2.Checked then ShowMessage ('CheckBox2 is checked') else ShowMessage ('CheckBox2 is NOT checked'); end; Notice that you cannot have a semicolon after the first statement and before the else keyword, or the compiler will issue a syntax error. The ''if-then-else'' statement, in fact, is a single statement, so you cannot place a semicolon in the middle of it. procedure TForm1.Button4Click(Sender: TObject); begin // compound if statement if CheckBox1.Checked then if CheckBox2.Checked then ShowMessage ('CheckBox1 and 2 are checked') else ShowMessage ('Only CheckBox1 is checked') else ShowMessage ('Checkbox1 is not checked, who cares for Checkbox2?') end; '''Case Statements''' If your ''if'' statements become very complex, at times you can replace them with ''case'' statements. A ''case'' statement consists in an expression used to select a value, a list of possible values, or a range of values. These values are constants, and they must be unique and of an ordinal type. Eventually, there can be an ''else'' statement that is executed if none of the labels correspond to the value of the selector. Here are two simple examples: case Number of 1: Text := 'One'; 2: Text := 'Two'; 3: Text := 'Three'; end; case MyChar of '+' : Text := 'Plus sign'; '-' : Text := 'Minus sign'; '*', '/': Text := 'Multiplication or division'; '0'..'9': Text := 'Number'; 'a'..'z': Text := 'Lowercase character'; 'A'..'Z': Text := 'Uppercase character'; else Text := 'Unknown character'; end;
Summary:
Please note that all contributions to the Delphi Programming are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab