Solved

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

Posted on 2011-03-05
7
917 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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 38

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

617 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