Solved

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

Posted on 2011-03-05
7
893 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 202
how to update exe applicatio from internet ? 6 80
Delphi...Split view - idea? 1 70
Delphi: sending SMS on android platform 1 21
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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