4

I am a bit confused about the nomenclature for the parts of an if statement. Consider the following example:

1:  if condition then
2:      statement_1;
3:  else
4:      statement_2;
5:  end if;

What is the "if clause" in this statement? Here are a few possibilities:

  • Is it lines #1 and #2?
  • Is it line #1?
  • Is it line #2?
  • Is is the condition in line #1?

And in the same example, what would be the "then clause"?

  • Is it line #2?
  • Is it the then keyword, plus line #2?
  • Is it just a synonym for "if clause"?

My main reference is Code Complete 2nd Ed., but the author seems to use exclusively the term "if clause", with the meaning being lines #1 and #2 in this example.

gnat
  • 21,442
  • 29
  • 112
  • 288
rick
  • 1,945
  • 2
  • 19
  • 16
  • 4
    Oh, my. How we programmers do love complicating things. – Robert Harvey Mar 31 '14 at 19:13
  • 1
    @RobertHarvey, I also find it amusing, but I am a programmer and an aspiring writer, so I need to get the basic terminology right! :) – rick Mar 31 '14 at 19:15
  • 2
    I am suspicious that different programmers will have different interpretations. Even if someone is able to demonstrate logically that their interpretation makes the most sense, other programmers (who have not read this explanation) will still not share such interpretations. – Brian Apr 01 '14 at 11:49
  • As Brian said, it seems there is no single opinion. Robert Harvey provided his view, and here is another one: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/if1.html – john c. j. May 12 '20 at 00:03

2 Answers2

5

The reason you hear about conditional "clauses" is because English has clauses. When you hear about conditional clauses in programming, what the person is speaking about is "that which embodies the condition."

So the if clause is

if condition then
    statement_1;

because that's the part that pertains to the if.

The else clause is

else
    statement_2;

because that's the part that pertains to the else.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • +1 Thanks, that makes a lot of sense. Just for completeness, what would be a "then clause" then? This term appears in many textbooks. – rick Mar 31 '14 at 19:47
  • 4
    There is no "then" clause. "Then" is a delimiter, a keyword that separates the `if` clause from the `else` clause. – Robert Harvey Mar 31 '14 at 19:54
  • @RobertHarvey Wikipedia call these blocks "conditional statements". Is it correct? That is to say, is it correct that "conditional clause" and "conditional statement" is the same thing? https://en.wikipedia.org/wiki/Conditional_(computer_programming): _"In contrast, the combination of this expression, the If and Then surrounding it, and the consequent that follows afterward constitute a conditional statement."_ – john c. j. May 11 '20 at 16:04
  • 1
    @johnc.j. The thing you need to ask yourself is "how do you define a 'statement' in this particular programming language?" – Robert Harvey May 11 '20 at 16:06
  • @RobertHarvey I'm not a "real" programmer, all I do is simply scripts for myself. What you say is too complex for me. – john c. j. May 11 '20 at 16:10
  • 1
    @johnc.j.: It's in the language specification. If the language specification says its a statement, it's a statement. If it's a statement that contains a conditional expression, it's a *conditional statement.* – Robert Harvey May 11 '20 at 16:14
  • 1
    @johnc.j. For example, [here](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement). It says "If...Then...Else **Statement.**" – Robert Harvey May 11 '20 at 16:16
  • 1
    @johnc.j. And [here](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else), which calls it an "if **statement**". – Robert Harvey May 11 '20 at 16:18
  • @RobertHarvey Yeah, thanks, but I asked if "**conditional statement**" is the same thing as "**conditional clause**"... – john c. j. May 11 '20 at 18:51
  • 1
    @johnc.j. You're focusing on the wrong thing. This level of detail in word definitions has zero relevance on your ability to effectively use these language structures to solve problems using code. – Robert Harvey May 11 '20 at 21:18
0

It is a lot easier if you write the code as

1  if condition
2  then
3      statement_1
4  else
5      statement_2
6  endif
  • 1 is the if-clause
  • 2 and 3 are the then clause
  • 4 and 5 are the else clause

In some languages, they use {}. In those terse languages, then is considered a noise word so it is dropped. In those languages, there is no then clause - 1 & 3 make up the if clause. Those using indentation only (like python) don't have a then or endif.

cup
  • 127
  • 6