Solved

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

Posted on 2011-03-05
7
876 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 36

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

19 Experts available now in Live!

Get 1:1 Help Now