In programming, do line and statement mean the same thing?

In programming, do the terms "line" and "statement" always mean the same thing?
In programming? Depends on the programming language.

In C++? No.
Line (eg. line 22) denotes a part of a text file, that is delimited by new line characters.
Statement denotes a C++ programming construct in a translation unit (in a program).
A program is a sequence of statements, which may be stored as lines in a text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* line 1  */ int main() // statement 1 (declaration statement)
/* line 2  */ { // begin statement 2 (compound statement)
/* line 3  */
/* line 4  */  int i // begin statement 3 (declaration statement)
/* line 5  */         = 0 ; // end statement 3 (declaration statement)
/* line 6  */
/* line 7  */  i = 7 ; /* statement 4 (expression statement) */ ++i ; // statement 5 (expression statement)
/* line 8  */
/* line 9  */      if( i < 12 ) // statement 6 (selection statement)
/* line 10 */      { // begin statement 7 (compound statement)
/* line 11 */
/* line 12 */         while( i < 9 ) /* statement 8 (iteration statement) */ ++i ; // statement 9 (expression statement)
/* line 13 */
/* line 14 */      } // end statement 7 (compound statement)
/* line 15 */
/* line 16 */ } // end statement 2 (compound statement)
/* line 17 */
/* line 18 */

Last edited on
closed account (2AoiNwbp)
In C++ a statement ends with semicolon ;
so it can be in multiple lines as in:
1
2
3
cout << myVar
       << " and "
       << myOtherVAr;           // statement in three lines 

or multiple statements in a single line as in:
 
int var1; double var2; var1 = 1; var2 = 1.0;

although the last one is an ugly form of writing statements.
It is better to write each statement in a single line, hence the confusion.
Last edited on
Topic archived. No new replies allowed.