Solved

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

Posted on 2011-03-05
7
912 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
Ephraim Wangoya 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 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