Recent

Author Topic: What is the convention in leaving If Then Else statement empty?  (Read 1907 times)

Lampbert

  • New Member
  • *
  • Posts: 33
What is the convention in leaving If Then Else statement empty?
« on: February 12, 2020, 09:40:47 pm »
Hello,

Still new to programming, so probably a basic thing...

Whenever I do an If Then Else statement and I don't want it to do anything if the conditions are met, I just leave the Then part empty, like so:

Code: Pascal  [Select][+][-]
  1. If a = a then begin
  2.   // Do nothing
  3. end
  4. else begin
  5.   DoSomething();
  6. end;

Is this correct, or is there a more official, cleaner, conventional way to do this?

Thank you.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: What is the convention in leaving If Then Else statement empty?
« Reply #1 on: February 12, 2020, 09:50:58 pm »
Hi!

Ask George Bool and you get the following answer:

Code: Pascal  [Select][+][-]
  1. If a <> a then begin
  2.           DoSomething();
  3.           end;
  4.  
//  else Do nothing without typing


 Winni

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4715
  • I like bugs.
Re: What is the convention in leaving If Then Else statement empty?
« Reply #2 on: February 12, 2020, 09:52:35 pm »
Maybe:
Code: Pascal  [Select][+][-]
  1. If a <> b then
  2.   DoSomething();
I switched the second "a" to "b" because "a" never differs from "a".
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19387
  • Glad to be alive.
Re: What is the convention in leaving If Then Else statement empty?
« Reply #3 on: February 12, 2020, 09:53:38 pm »
Code: Pascal  [Select][+][-]
  1. // assumes a is a Boolean.
  2. case a of
  3. true:Dosomething;
  4. else; // do nothing
  5. end;
objects are fine constructs. You can even initialize them with constructors.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: What is the convention in leaving If Then Else statement empty?
« Reply #4 on: February 12, 2020, 10:00:06 pm »
Dont confuse the newbie:

Case is for more  than 2 solutions.

For a boolean if then else  is enough.

Keep it simple, stupid!

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 19387
  • Glad to be alive.
Re: What is the convention in leaving If Then Else statement empty?
« Reply #5 on: February 12, 2020, 10:16:45 pm »
Dont confuse the newbie:

Case is for more  than 2 solutions.

For a boolean if then else  is enough.

Keep it simple, stupid!

Winni
Well, based on your reply and common sense, if would suffice......no need for else.....(Fired... >:D )
A simple matter of program flow.
« Last Edit: February 12, 2020, 10:18:21 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: What is the convention in leaving If Then Else statement empty?
« Reply #6 on: February 12, 2020, 10:23:59 pm »
 Nothing to argue, but a lot of words.

Lampbert

  • New Member
  • *
  • Posts: 33
Re: What is the convention in leaving If Then Else statement empty?
« Reply #7 on: February 12, 2020, 10:43:05 pm »
Thanks for everyone's answer. I learned this new operator now.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: What is the convention in leaving If Then Else statement empty?
« Reply #8 on: February 13, 2020, 12:10:28 am »
Hello,

Still new to programming, so probably a basic thing...

Whenever I do an If Then Else statement and I don't want it to do anything if the conditions are met, I just leave the Then part empty, like so:

Code: Pascal  [Select][+][-]
  1. If a = a then begin
  2.   // Do nothing
  3. end
  4. else begin
  5.   DoSomething();
  6. end;

Is this correct, or is there a more official, cleaner, conventional way to do this?

Thank you.
just a thought why not
Code: Pascal  [Select][+][-]
  1. If not (a = a) then begin
  2.   DoSomething();
  3. end;

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: What is the convention in leaving If Then Else statement empty?
« Reply #9 on: February 13, 2020, 12:27:17 am »
Hi!

As it was said above:

a=a  is always true. (In our solar system)

So reduce your example to

Code: Pascal  [Select][+][-]
  1. { nothing}
  2.  

But what you want  is

Code: Pascal  [Select][+][-]
  1. if a <> b then DoSomething;

Seems you have to learn something about boolean operators.

You also  can write

Code: Pascal  [Select][+][-]
  1. if NOT (a=b) then DoSomething;

But if you insist on the useless if condition - you can drop the begin end

Code: Pascal  [Select][+][-]
  1. if a= b then else DoSomething;

Winni



 

TinyPortal © 2005-2018