Home > Articles > Programming > C/C++

This chapter is from the book

Program Flow

The statements in a program are executed in sequence, except when directed to do otherwise by a for, while, do-while, if, switch, or goto statement or a function call.

  • An if statement conditionally executes code depending on the truth value of an expression.
  • The for, while, and do-while statements are used to form loops. In a loop, the same statement or group of statements is executed repeatedly until a condition is met.
  • A switch statement chooses a set of statements to execute based on the arithmetic value of an integer expression.
  • A goto statement is an unconditional jump to a labeled statement.
  • A function call is a jump to the code in the function’s body. When the function returns, the program executes from the point after the function call.

These control statements are covered in more detail in the following sections.

if

An if statement conditionally executes code depending on the truth value of an expression. It has the following form:

if ( expression )

  statement

If expression evaluates to true (non-zero), statement is executed; otherwise, execution continues with the next statement after the if statement. An if statement may be extended by adding an else section:

if ( expression )

  statement1

else

  statement2

If expression evaluates to true (non-zero), statement1 is executed; otherwise, statement2 is executed.

An if statement may also be extended by adding else if sections, as shown here:

if ( expression1 )

  statement1

else if ( expression2 )

  statement2

else if ( expression3 )

  statement3

...

else

  statementN

The expressions are evaluated in sequence. When an expression evaluates to non-zero, the corresponding statement is executed and execution continues with the next statement following the if statement. If the expressions are all false, the statement following the else clause is executed. (As with a simple if statement, the else clause is optional and may be omitted.)

Conditional Expression

A conditional expression is made up of three sub-expressions and has the following form:

expression1 ? expression2 : expression3

When a conditional expression is evaluated, expression1 is evaluated for its truth value. If it is true, expression2 is evaluated and the value of the entire expression is the value of expression2. expression3 is not evaluated.

If expression1 evaluates to false, expression3 is evaluated and the value of the conditional expression is the value of expression3. expression2 is not evaluated.

A conditional expression is often used as shorthand for a simple if statement. For example:

a = ( b > 0 ) ? c : d;
is equivalent to
if ( b > 0 )

  a = c;

else

  a = d;

while

The while statement is used to form loops as follows:

while ( expression ) statement

When the while statement is executed, expression is evaluated. If it evaluates to true, statement is executed and the condition is evaluated again. This sequence is repeated until expression evaluates to false, at which point execution continues with the next statement after the while.

You will occasionally see this construction:

while (1)
  {
    ...
  }

Since the constant 1 evaluates to true, the preceding is an infinite loop from the while’s point of view. Presumably, something in the body of the loop checks for a condition and breaks out of the loop when that condition is met.

do-while

The do-while statement is similar to the while, with the difference that the test comes after the statement rather than before:

do statement while ( expression );

One consequence of this is that statement is always executed once, independent of the value of expression. Situations where the program logic dictates that a loop body be executed at least once, even if the condition is false, are uncommon. As a consequence, do-while statements are rarely encountered in practice.

for

The for statement is the most general looping construct. It has the following form:

for (expression1; expression2; expression3) statement

When a for statement is executed, the following sequence occurs:

  1. expression1 is evaluated once before the loop begins.
  2. expression2 is evaluated for its truth value.
  3. If expression2 is true, statement is executed; otherwise, the loop ends and execution continues with the next statement after the loop.
  4. expression3 is evaluated.
  5. Steps 2, 3, and 4 are repeated until expression2 becomes false.

expression1 and expression3 are evaluated only for their side effects. Their values are discarded. They are typically used to initialize and increment a loop counter variable:

int j;

for (j=0; j < 10; j++)
  {
    // Something that needs doing 10 times
  }

Any of the expressions may be omitted (the semicolons must remain). If expression2 is omitted, the loop is an infinite loop, similar to while( 1 ):

int i;

for (i=0; ; i++)
  {
    ...
    // Check something and exit if the condition is met
  }

break

The break statement is used to break out of a loop or a switch statement:

int j;

for (j=0;  j < 100; j++)
 {
    ...

    if ( someConditionMet ) break; //Execution continues after the loop
 }

Execution continues with the next statement after the enclosing while, do, for, or switch statement. When used inside nested loops, break only breaks out of the innermost loop. Coding a break statement that is not enclosed by a loop or a switch causes a compiler error:

error: break statement not within loop or switch

continue

continue is used inside a while, do-while, or for loop to abandon execution of the current loop iteration. For example:

int j;

for (j=0;  j < 100; j++)
 {
    ...

    if ( doneWithIteration ) continue; // Skip to the next iteration
    ...
}

When the continue statement is executed, control passes to the next iteration of the loop. In a while or do-while loop, the control expression is evaluated for the next iteration. In a for loop, the iteration (third) expression is evaluated and then the control (second) expression is evaluated. Coding a continue statement that is not enclosed by a loop causes a compiler error.

Comma Expression

A comma expression consists of two or more expressions separated by commas:

expression1, expression2, ..., expressionN

The expressions are evaluated in order from left to right, and the value of the entire expression is the value of the right-most sub-expression.

The principal use of the comma operator is to initialize and update multiple loop variables in a for statement. As the loop in the following example iterates, j goes from 0 to MAX-1 and k goes from MAX-1 to 0:

int j, k;

for (j=0, k=MAX-1; j < MAX; j++, k--)
  {
    // Do something
  }

When a comma expression is used like this in a for loop, only the side effects of evaluating the sub-expressions are important. The value of the comma expression is discarded. In the preceding example a comma expression is also used to increment j and decrement k after each pass through a for loop.

switch

A switch branches to different statements based on the value of an integer expression. The form of a switch statement is shown here:

switch ( integer_expression )
  {
     case value1:
       statement
       break;

     case value2:
       statement
       break;

     ...

     default:
       statement
       break;
}

In a slight inconsistency with the rest of C, each case may have multiple statements without the requirement of a compound statement.

value1, value2, ... must be either integers, character constants, or constant expressions that evaluate to an integer. (In other words, they must be reducible to an integer at compile time.) Duplicate cases with the same integer are not allowed.

When a switch statement is executed, integer_expression is evaluated and the switch compares the result with the integer case labels. If a match is found, execution jumps to the statement after the matching case label. Execution continues in sequence until either a break statement or the end of the switch is encountered. A break causes the execution to jump out to the first statement following the switch.

A break statement is not required after a case. If it is omitted, execution falls through to the following case. If you see the break omitted in existing code, it can be either a mistake (it is an easy one to make) or intentional (if the coder wanted a case and the following case to execute the same code).

If integer_expression doesn’t match any of the case labels, execution jumps to the statement following the optional default: label, if one is present. If there is no match and no default:, the switch does nothing, and execution continues with the first statement after the switch.

goto

C provides a goto statement:

goto label;

When the goto is executed, control is unconditionally transferred to the statement marked with label:, as here:

label: statement

  • Labels are not executable statements; they just mark a point in the code.
  • The rules for naming labels are the same as the rules for naming variables and functions.
  • Labels always end with a colon.

Using goto statements with abandon can lead to tangled, confusing code (often referred to as spaghetti code). The usual boilerplate advice is “Don’t use goto statements.” Despite this, goto statements are useful in certain situations, such as breaking out of nested loops (a break statement only breaks out of the innermost loop):

int i, j;

for (i=0; i < MAX_I; i++)
  for (j=0; j < MAX_J; j++)
     {
        ...
       if ( finished ) goto moreStuff;

     }

moreStuff:  statement     // more statements

Functions

Functions have the following form:

returnType functionName( arg1Type arg1, ..., argNType argN )
{

 statements

}

An example of a simple function looks like this:

float salesTax( float purchasePrice, float taxRate )
{
  float tax = purchasePrice * taxRate;
  return tax;
}

A function is called by coding the function name followed by a parenthesized list of expressions, one for each of the function’s arguments. Each expression type must match the type declared for the corresponding function argument. The following example shows a simple function call:

float carPrice = 20000.00;
float stateTaxRate = 0.05;

float carSalesTax = salesTax( carPrice, stateTaxRate );

When the line with the function call is executed, control jumps to the first statement in the function body. Execution continues until a return statement is encountered or the end of the function is reached. Execution then returns to the calling context. The value of the function expression in the calling context is the value set by the return statement.

Functions are sometimes executed solely for their side effects. This function prints out the sales tax but changes nothing in the program’s state:

void printSalesTax ( float purchasePrice, float taxRate )
{
  float tax = purchasePrice * taxRate;

  printf( "The sales tax is: %f.2\n", tax );

}

C functions are call by value. When a function is called, the expressions in the argument list of the calling statement are evaluated and their values are passed to the function. A function cannot directly change the value of any of the variables in the calling context. This function has no effect on anything in the calling context:

void salesTax( float purchasePrice, float taxRate, float carSalesTax )
{
  // Changes the local copy of carSalesTax but not the value of
  // the variable in the calling context

     carSalesTax = purchasePrice * taxRate;
     return;
}

To change the value of a variable in the calling context, you must pass a pointer to the variable and use that pointer to manipulate the variable’s value:

void salesTax( float purchasePrice, float taxRate, float *carSalesTax)
{
  *carSalesTax = purchasePrice * taxRate; // this will work

  return;
}

Declaring Functions

When you call a function, the compiler needs to know the types of the function’s arguments and return value. It uses this information to set up the communication between the function and its caller. If the code for the function comes before the function call (in the source code file), you don’t have to do anything else. If the function is coded after the function call or in a different file, you must declare the function before you use it.

A function declaration repeats the first line of the function, with a semicolon added at the end:

void printSalesTax ( float purchasePrice, float taxRate );

It is a common practice to put function declarations in a header file. The header file is then included (see the next section) in any file that uses the function.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020