1. Homepage of Dr. Zoltán Porkoláb
    1. Home
    2. Archive
  2. Teaching
    1. Timetable
    2. Bolyai College
    3. C++ (for mathematicians)
    4. Imperative programming (BSc)
    5. Multiparadigm programming (MSc)
    6. Programming (MSc Aut. Sys.)
    7. Programming languages (PhD)
    8. Software technology lab
    9. Theses proposals (BSc and MSc)
  3. Research
    1. Sustrainability
    2. CodeChecker
    3. CodeCompass
    4. Templight
    5. Projects
    6. Conferences
    7. Publications
    8. PhD students
  4. Affiliations
    1. Dept. of Programming Languages and Compilers
    2. Ericsson Hungary Ltd

6. Statements

Statements, control structures

Statements, constrol structures are the smallest standalone elements in an imperative programming language commanding the computer to carry out some actions. As C++ has roots from the imperative C language, most of the statements are inherited from there.

Expression statement

An expression followed by a semicolon (;) is an expression-statement.

printf("Hello world\n")

is an expression (type is int, value is 12, as printf returns with the number of bytes about to print). It is valid (but unusual) to write this:

printf("Hello world\n") + 3

Most cases we just use the side effect of the expression and make it as a statement.

printf("Hello world\n");

Null statement

Null statement is a statement without effect. It is a single semicolon.

1
2
3
4
if ( x < 10 )
  ;
else
  std::cout << "else branch";

Compound statement

Compound statement (sometimes called as block is to create a single statement from multiply statements. A body of a function is a compound statement too.

1
2
3
4
5
6
7
8
9
if ( x < 10 )
{
  ;
}
else
{
  std::cout << "compund statement";
  std::cout << "in the else branch";
}

Its a good idea to use compound statements in every control structure.

If statement

The if (selection) statement is a control statement. It has two forms.

if (expression) statement
if (expression) statement1; else statement2;

The statements may (and suggested to be) compound statements.

Dangling ifs are matched to the closest if statement:

1
2
3
4
5
if ( x < 10 )
  if ( y > 5 )
    std::cout << "x < 10 and y > 5";
else
  std::cout << "x < 10 and y <= 5";

is equivalent of:

1
2
3
4
5
6
7
if ( x < 10 )
{
  if ( y > 5 )
    std::cout << "x < 10 and y > 5";
  else
    std::cout << "x < 10 and y <= 5";
}

and different from this:

1
2
3
4
5
6
7
if ( x < 10 )
{
  if ( y > 5 )
    std::cout << "x < 10 and y > 5";
}
else
  std::cout << "x >= 10";

Sometimes if and else statements appear in a specific form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if ( x < 10  &&  y > 5 )
{
  std::cout << "x < 10 and y > 5";
}
else if ( x < 10  &&  y <= 5 )
{
  std::cout << "x < 10 and y <= 5";
}
else if ( x >= 10  &&  y > 5 )
{
  std::cout << "x >= 10 and y > 5";
}
else if ( x >= 10  &&  y <= 5 )
{
  std::cout << "x >= 10 and y <= 5";
}
else
{
  printf("impossible");
}

Switch (selection) statement

The switch statement is an alternative selection statement.

switch (expression) statement

The statement almost always is a compound statement with labelled statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
int day_of_week;
//...
switch ( day_of_week )
{
  default: std::cout << "Udefined";  break;
  case  2: std::cout << "Monday";    break;
  case  3: std::cout << "Tuesday";   break;
  case  4: std::cout << "Wednesday"; break;
  case  5: std::cout << "Thursday";  break;
  case  6: std::cout << "Friday";    break;
  case  1:
  case  7: std::cout << "Week-end";  break;
}

The statements will fall through to the next case. That is why it is suggested to use the break statements, to pass the control to the next statement after the switch.

While (iteration) statement

while ( expression ) statement

The while loop checks condition for truthfulness before executing the statement.

1
2
3
4
5
6
7
8
9
10
11
12
struct list_type
{
  int       value;
  list_type *next;
};
// ...
list_type *ptr = first;
while ( NULL != ptr  )
{
  std::cout << ptr->value << " ";
  ptr = ptr->next;
}

Do-while (iteration) statement

do statement while ( expression ) ;

The do-while loop is different from the while loop since the statement will be executed at least ones. The conditional expression is checked only after the statement.

The do-while statement is equivalent of:

statement
while ( expression )
  statement

For (iteration) statement

for ( opt-expr-1 ; opt-expr-2 ; opt-expr-3 ) statement 
for ( declaration; opt-expr-2 ; opt-expr-3 ) statement 
for ( range-declaration : range-expression ) statement (C++11)

where

  1. opt-expr-1 is an optional initialization expression, executed once before the first iteration. In C++ opt-expr-1 can be replaced by a declaration.

  2. opt-expr-2 is an optional control expression, evaluated before every iteration, and the statement is executed only when the control expression is true.

  3. opt-expr-3 is an optional iteration expression, executed every time after the statement.

Therefore,

  
for ( e1 ; e2 ; e3 ) s;

is equivalent of

  
{
  e1;
  while ( e2 )
  {
    s;
    e3;
  }
}

The three expressions are all optionals. If the second expression is missing, then it is equivalent of true. The infinite loop is usually written in teh following way:

for( ; ; ) statement

You can still leave this tool with a return or break statement.

Since C++11 we can use range based for statement (also called foreach loop) to iterate on a certain interval of values:

1
2
3
4
5
6
std::vector<int> v = { 0, 1, 2, 3 };
for ( int x : v )
{
  std::cout << x << " ";
}
// x is not visible here.

In C++11 we frequently use type deduction for the declaration of the range variable using auto keyword.

1
2
3
4
5
6
std::vector<int> v = { 0, 1, 2, 3 };
for ( auto x : v )
{
  std::cout << x << " ";
}
// x is not visible here.

Break and Continue (jump) in loops

The break statement in a loop or a switch statement jumps to the following statement.

1
2
3
4
5
6
7
8
9
10
11
12
int t[10];
// ...
for ( int i = 0; i < 10; ++i )
{
  if ( t[i] < 0 )
  {
    std::cout << "negative found\n";
    break;
  }
  std::cout << "do something with non-negatives\n";
}
// break jumps to here

The continue statement will jump over the rest of the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
int t[10];
// ...
for ( int i = 0; i < 10; ++i )
{
  if ( t[i] < 0 )
  {
    std::cout << "negative found\n";
    continue;
  }
  std::cout << "do something with non-negatives\n";
  // ...
  // continue jumps to here
}

Return (jump) statement

return;
return expr;

The return statements returns from the actually executed function to the caller. There might be multiply return statements in a function.

Returning from main will finish the program execution.

1
2
3
4
5
6
7
8
9
10
11
12
int find_first_negative( int t[], int length)
{
  for ( int i = 0; i < length; ++i )
  {
    if ( t[i] < 0 )
    {
       std::cout << "negative found\n";
      return t[i];
    }
  }
  return 0;
}

Goto (jump) statement

goto label ;

where label is an identifier. Don’t use the goto statement.

Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government.