1. Homepage of Dr. Zoltán Porkoláb
    1. Home
    2. Archive
  2. Teaching
    1. Timetable
    2. Multiparadigm programming (MSc)
    3. C programming (BSc for physicists)
    4. Project tools (BSc)
    5. Bolyai College
    6. C++ (for foreign studenst)
    7. Software technology lab
    8. BSc and MSc thesis
  3. Research
    1. Templight
    2. CodeChecker
    3. CodeCompass
    4. Projects
    5. Publications (up to 2011)
    6. PhD students
  4. Affiliations
    1. Dept. of Programming Languages and Compilers
    2. Ericsson Hungary Ltd

The C programming language – Lecture 5.

Statements, control structures

Statements, constrol structures are the smallest standalone elements in an imperative programming language, like C, that commands the computer to carry out some actions.

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). 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
  printf("else branch");

Compound statement

Creating a single statement from multiply statements. A body of a function is a compound statement.

1
2
3
4
5
6
7
8
9
if ( x < 10 )
{
  ;
}
else
{
  printf("compund statement");
  printf("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 )
    printf("x < 10 and y > 5");
else
  printf("x < 10 and y <= 5");

is equivalent of:

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

and different from this:

1
2
3
4
5
6
7
if ( x < 10 )
{
  if ( y > 5 )
    printf("x < 10 and y > 5");
}
else
  printf("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 )
{
  printf("x < 10 and y > 5");
}
else if ( x < 10  &&  y <= 5 )
{
  printf("x < 10 and y <= 5");
}
else if ( x >= 10  &&  y > 5 )
{
  printf("x >= 10 and y > 5");
}
else if ( x >= 10  &&  y <= 5 )
{
  printf("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: printf("Undefined"); break;
  case  2: printf("Monday");    break;
  case  3: printf("Tuesday");   break;
  case  4: printf("Wednesday"); break;
  case  5: printf("Thursday");  break;
  case  6: printf("Friday");    break;
  case  1:
  case  7: printf("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  )
{
  printf( "%d ", 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  (C99)

where

  1. opt-expr-1 is an optional initialization expression, executed once before the first iteration. Since C99 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 C99 it is possible to replace the initialization expression with a declaration (with initialization).

1
2
3
4
5
for ( int i = 0; i < 10; ++i )
{
  printf( "%d ");
}
// i 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 )
  {
    printf( "negative found");
    break;
  }
  printf("do something with non-negatives");
}
// 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 )
  {
    printf( "negative found");
    continue;
  }
  printf("do something with non-negatives");
  // ...
  // 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 )
    {
      printf( "negative found");
      return t[i];
    }
  }
  return 0;
}

Goto (jump) statement

goto label ;

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