Solved

Is there an If, Then, Else statement in Delphi ?

Posted on 2011-03-05
7
881 Views
Last Modified: 2012-06-22
Hi,

I wonder if Delphi supports an if, then, else statement. I have never been able to get one working in Delphi. When I search through the help files that come with Delphi, I can only find information on Visual Basic, and C++. I find the included help files very hard to use and rarely find what I am looking for. This makes Programmers Exchange indispensible.

Thank you
0
Comment
Question by:ChLa
7 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 25 total points
ID: 35043763
Of course Delphi does. Try it this way:
var
  i:integer;
begin
  i:=1;
  If i=1 then
    showmessage('one')
  else
    showmessage('Not one');
end;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35043961
From Delphi help:
"There are two forms of if statement: if...then and the if...then...else. The syntax of an if...then statement is

if expression then statement

where expression returns a Boolean value. If expression is True, then statement is executed; otherwise it is not. For example,

if J <> 0 then Result := I/J;

The syntax of an if...then...else statement is

if expression then statement1 else statement2"
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 25 total points
ID: 35044288

All modern languages support some form of if else statement delphi included. There are various ways you can use it and a shorter form of it as well

in the simplest form, you have

if (Condition) then
begin
  //your block of statements
end
else
begin
  //other block
end;

For multiple conditions,

if (Condition) then
begin
  //your block of statements
end
else if (OtherCondition)
begin
  //other block
end
else
begin
  //some more
end;

There is another form of  if. Include StrUtils or Math to your uses clause
This takes the form
  IfThen(Condition, True, False)
Say we are comparing numbers a and b, this would be
var
  LargerValue: Integer;
begin
  LargerValue := IfThen(a>b, a, b);
end



0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Expert Comment

by:ewangoya
ID: 35044303
I missed a then in my second example
if (Condition) then
begin
  //your block of statements
end
else if (OtherCondition) then
begin
  //other block
end
else
begin
  //some more
end;

If you are comparing multiple ordinal values, you can use the case statement as well
eg
   case Value of
      1: ShowMessage('one');
      2: ShowMessage('two');
      3: ShowMessage('three');
      4: ShowMessage('four');
      else
          ShowMessage('I do not know');
   end;
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 35044580
complex help ?
the best help system was in delphi 7
the examples are somewhat lacking

your probably looking for the compact version of if then ?
you need to use the math and strutils units

uses StrUtils, Math;

var x, y, z: integer;
begin
  z := IfThen(X>1, 1, 0);
  y := IfThen(Z=1,2,1);

for strings:
var s, r: string;
begin
  r := 'Test';
  s := IfThen(r = 'Test', 'yes', 'no');
0
 
LVL 2

Expert Comment

by:RezaSadigh
ID: 35081710
is It a fun?
Of course delphi like other programming language has if then else and its format is like below
if (condition) then
begin
  (True Statements)
end
else
begin
  (False Statements)
end

e.g.

if 1=2 then
   ShowMessage('1=2')
else ShowMessage('1<>2')
0
 

Author Closing Comment

by:ChLa
ID: 35129529
thank you for your help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now