UNIX C Programming

Chapter 7: C Control Statements: Branching and Jumps



In this chapter, students learn how to use the if and if else statements and how to nest them. They also learn how to write statements to jump from one section of a program to another. This is called controlling the program's flow. The teaching objectives important to this chapter include:

  • Explain the use if, else, and else if statements
  • Discuss the getchar() macro
  • Show how if statements are nested
  • Demonstrate the use of logical operators
  • Explain the use of the break and continue statements
  • Explain the switch statement
  • Explain the goto statement
  • Demonstrate how the goto statements can be avoided

The if statement is called a branching statement. It provides a juncture at which the program has to select which of two paths to follow. The general form of the if statement is:

	if (expression)

Normally the expression involves a test of a relational expression.

The purpose of the getchar() macro is to return the next character from the keyboard. It is written as

	ch = getchar();

The if statement can be combined with the else and the else if statements. The general form for a nested if statement is:

	if (expession1)
	else if (expression 2)

In this example, statement 3 would be executed if the tests for expression1 and expression2 are both false.

Logical operators are often used with if statements. The logical operators and their meaning are:

	Operator	Meaning
	&&		AND
	||		OR
	!		NOT

Logical operators return true (1) or false (0). With the logical AND (the && operator), the expressions on both sides of the operator must be true for the expression as a whole to be true. With the logical OR (the || operator), only one expression on either size of the operator has to be true for the expression as a whole to be true.

The conditional operator (? :) offers a shorthand way of writing if-else statements. The general form is:

	expression1 ? expression2 : expression3;

In this form, if expression1 is true, branch to expression2. If expression1 is false, branch to expression3.

The break, continue, and goto statements are jump statements, that cause program flow to jump to another location in a program.

The continue statement is often placed in a loop. It causes the remainder of an iteration to be skipped and the next iteration of the loop to be started.

The break statement is often placed in a loop and a switch statement. Within a loop, break causes the remainder of an iteration to be skipped and to break from the loop itself.

The switch statement is used when a program needs to choose from one of several alternatives. The general form of this statement is:

	switch (integer expression)

		case constant1:
		case constant2:
		/* there can be lots of cases,
			 we have lots of integers */
The switch statement is advised in multiple-choice settings. Each case label must be a constant.

The goto statement is an unconditional jump in a program and has the general form:

	goto label;
	label: statement;

where the label can either be placed before or after the goto statement. As a general rule, do not use goto in a C program. The rule is to "use sparingly, if at all." It is an indication of bad program flow if goto is used extensively, making it very hard to read or debug. It is also a violation of the concept of modular programming. See your text for a more detailed discussion of why not to use goto.