• [ Pobierz caÅ‚ość w formacie PDF ]

    often used for incrementing, decrementing, or assigning to a
    variable. This expression is optional.
    A break, return, or goto statement can cause a for statement to end, even when
    the second expression does not evaluate to false. If you omit expression2, you must
    use a break, return, or goto statement to end the for statement.
    In C++ programs, you can also use expression1 to declare a variable as well
    as initialize it. If you declare a variable in this expression, or anywhere else in
    statement, that variable goes out of scope at the end of the for loop.
    You can set a compile option where a variable declared in the scope of a
    for statement is not local to the for statement.
    Examples of for Statements
    The following for statement prints the value ofcount20times. The for statement
    initially sets the value ofcountto1. After each iteration of the statement,countis
    incremented.
    int count;
    for (count = 1; count
    printf("count = %d\n", count);
    The following sequence of statements accomplishes the same task. Note the use of
    the while statement instead of the for statement.
    int count = 1;
    while (count
    {
    printf("count = %d\n", count);
    count++;
    }
    154 C/C++ Language Reference
    for Statement
    The following for statement does not contain an initialization expression:
    for (; index > 10; --index)
    {
    list[index] = var1 + var2;
    printf("list[%d] = %d\n", index, list[index]);
    }
    The following for statement will continue running untilscanfreceives the lettere:
    for (;;)
    {
    scanf("%c", &letter);
    if (letter == '\n')
    continue;
    if (letter == 'e')
    break;
    printf("You entered the letter %c\n", letter);
    }
    The following for statement contains multiple initializations and increments. The
    comma operator makes this construction possible. The first comma in the for
    expression is a punctuator for a declaration. It declares and initializes two integers,
    iandj. The second comma, a comma operator, allows bothiandjto be
    incremented at each step through the loop.
    for (int i = 0, j = 50; i
    {
    cout
    }
    The following example shows a nested for statement. It prints the values of an
    array having the dimensions[5][3].
    for (row = 0; row
    for (column = 0; column
    printf("%d\n", table[row][column]);
    The outer statement is processed as long as the value ofrowis less than5. Each
    time the outer for statement is executed, the inner for statement sets the initial
    value ofcolumnto zero and the statement of the inner for statement is executed3
    times. The inner statement is executed as long as the value ofcolumnis less than3.
    v  break Statement
    v  return Statement on page 158
    v  goto Statement on page 159
    v  Boolean Variables on page 33
    break Statement
    A break statement lets you end an iterative (do, for, or while) statement or a switch
    statement and exit from it at any point other than the logical end. A break may
    only appear on one of these statements.
    A break statement has the form:
    break ;
    Chapter 8. Statements 155
    break Statement
    In an iterative statement the break statement ends the loop and moves control to
    the next statement outside the loop. Within nested statements, the break statement
    ends only the smallest enclosing do, for, switch, or while statement.
    In a switch statement, the break passes control out of the switch body to the next
    statement outside the switch statement.
    v  do Statement on page 152
    v  for Statement on page 153
    v  while Statement on page 151
    v  switch Statement on page 148
    continue Statement
    A continue statement ends the current iteration of a loop. Program control is passed
    from the continue statement to the end of the loop body.
    A continue statement has the form:
    continue ;
    A continue statement can only appear within the body of an iterative statement.
    The continue statement ends the processing of the action part of an iterative (do,
    for, or while) statement and moves control to the loop continuation portion of the
    statement. For example, if the iterative statement is a for statement, control moves
    to the third expression in the condition part of the statement, then to the second
    expression (the test) in the condition part of the statement.
    Within nested statements, the continue statement ends only the current iteration of
    the do, for, or while statement immediately enclosing it.
    Examples of continue Statements
    The following example shows a continue statement in a for statement. The
    continue statement causes processing to skip over those elements of the array
    ratesthat have values less than or equal to1.
    /**
    ** This example shows a continue statement in a for statement.
    **/
    #include
    #define SIZE 5
    int main(void)
    {
    int i;
    static float rates[SIZE] = { 1.45, 0.05, 1.88, 2.00, 0.75 };
    printf("Rates over 1.00\n");
    for (i = 0; i
    {
    if (rates[i]
    continue;
    printf("rate = %.2f\n", rates[i]);
    156 C/C++ Language Reference
    continue Statement
    }
    return(0);
    }
    The program produces the following output:
    Rates over 1.00
    rate = 1.45
    rate = 1.88
    rate = 2.00
    The following example shows a continue statement in a nested loop. When the
    inner loop encounters a number in the arraystrings, that iteration of the loop
    ends. Processing continues with the third expression of the inner loop. The inner
    loop ends when the  \0 escape sequence is encountered.
    /**
    ** This program counts the characters in strings that are part
    ** of an array of pointers to characters. The count excludes
    ** the digits 0 through 9.
    **/
    #include
    #define SIZE 3
    int main(void)
    {
    static char *strings[SIZE] = { "ab", "c5d", "e5" };
    int i;
    int letter_count = 0;
    char *pointer;
    for (i = 0; i
    /* for each each character */
    for (pointer = strings[i]; *pointer != '\0'; ++pointer)
    { /* if a number */
    if (*pointer >= '0' && *pointer
    continue;
    letter_count++;
    }
    printf("letter count = %d\n", letter_count);
    return(0);
    }
    The program produces the following output:
    letter count = 5
    v  do Statement on page 152
    v  for Statement on page 153
    v  while Statement on page 151
    Chapter 8. Statements 157
    return Statement
    return Statement
    A return statement ends the processing of the current function and returns control
    to the caller of the function.
    A return statement has the form:
    return ;
    expression
    A return statement in a function is optional. The compiler issues a warning if a
    return statement is not found in a function declared with a return type. If the end
    of a function is reached without encountering a return statement, control is passed
    to the caller as if a return statement without an expression were encountered. A
    function can contain multiple return statements.
    v  Chapter 7. Functions on page 123
    Value of a return Expression and Function Value
    If an expression is present on a return statement, the value of the expression is
    returned to the caller. If the data type of the expression is different from the
    function return type, conversion of the return value takes place as if the value of
    the expression were assigned to an object with the same function return type.
    If an expression is not present on a return statement, the value of the return
    statement is undefined. If an expression is not given on a return statement in a
    function declared with a nonvoid return type, an error message is issued, and the
    result of calling the function is unpredictable. For example:
    int func1()
    {
    return;
    }
    int func2()
    {
    return (4321);
    }
    void main() {
    int a=func1(); // result is unpredictable!
    int b=func2();
    }
    You cannot use a return statement with an expression when the function is
    declared as returning type void.
    Examples of return Statements
    return; /* Returns no value */ [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • zambezia2013.opx.pl