Broad Network


Jump Statements in the C Computer Language

Part 25 of the Complete C Course

Foreword: Jump Statements in the C Computer Language

By: Chrysanthus Date Published: 11 Jun 2024

Introduction

Jump statements are the break, continue, return and goto statements.

The break Statement
The "break;" statement can be used to terminate a loop before its determined end. Read and test the following code and note that the loop ends just after n is 2.

        for (int n=0; n<5; ++n)
            {
                printf("%i\n", n);
                    if (n == 2) {
                            break;
                        }
            }

The output is 0, 1, 2 in three different lines. For each iteration of the loop, the if-condition is checked for the value of true. When n is 2, the if-condition will return true; making the if-block to execute. In the if-block, there is just one statement, the break statement. It is just one word, break. Always end the break statement and other statements with a semicolon. The break statement stops the loop from repeating. In this case, it stopped the loop when the internal if-condition occurred (was true).

The continue Statement
An iteration can be caused to be skipped as the loop is repeating. The continue statement is used for this. It is just one word, continue. Always end it with a semicolon. The following code illustrates this, when n is 2. The iteration for n equals 2, is skipped.

        for (int n=0; n<5; ++n)
            {
                if (n == 2)
                    {
                        continue;
                    }
                printf("%i\n", n);
            }

The output is 0, 1, 3, 4, missing 2. In order to skip the iteration of the whole block, put the continue statement and its condition at the beginning of the block. Test the above code.

This is how the continue statement behaves:

- In a while loop, it jumps back to the condition.
- In a for-loop, it jumps to the update (increment) expression.

The goto Statement
Consider the following program:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        printf("%c ", 'A');
        goto labl1;
        printf("%c ", 'B');
        labl1:
        printf("%c ", 'C');
        goto labl2;
        printf("%c ", 'D');
        labl2:
        printf("\n");

        return 0;
    }

The output is A C. The first printf() call statement in main(), prints 'A'. The next statement in main() is the goto labl1 statement. And so the execution skips the statements following, until it reaches the label, labl1. From there it executes the statements that follow. When it reaches "goto labl2;" , it skips the statements that follow, until it reaches, labl2. From there, it executes the statements that follow.

A label is an identifier (name), followed by a colon. The goto statement ends with a semicolon like most other statements. A goto statement causes an unconditional jump to the statement prefixed by the named label, in the enclosing function definition. The label can still start on the same line as its following statement. The above sequence of "goto label" and "label" can be in any function, and just just the main() function. The jump can also go backwards, but in such a case, an infinite looping may develop.

C Function and the return Statement

The Function Structure
A function definition is of the form:

    returnType functionName (type1 ident1, type2 ident2, \85)
        {
            * statements *
            return [value]; // optional
        }

In the structure,

    returnType functionName (type1 ident1, type2 ident2, \85)

is the interface or signature, and

    (type1 ident1, type2 ident2, \85)

is the parameter list.

The return statement is optional, and is usually the last statement, in many situations. If the return statement (line) is omitted, then the returnType of the function signature has to be, void.

The return Statement
The return statement itself can simply be:

    return;

In this case nothing is returned. In such a case the returnType of the function should be void.

If the returnType is not void, then the return statement should be:

    return expression;

where expression can just be a value (datum e.g 56). The return value can also be an identifier or a pointer or a more elaborated expression.

Meaning of the return Statement
The return statement means, stop executing the function at the return statement, and return to the caller. The following program illustrates this:

    #include <stdio.h>
    
    int fn() {
        printf("%i\n", 1);
        printf("%i\n", 2);
        return 56;
        printf("%i\n", 3);
        printf("%i\n", 4);
    }
    
    int main(int argc, char *argv[])
    {
        int var = fn();
        printf("%i\n", var);

        return 0;
    }

The output is 1 2 56, on three different lines. Read and test the code. Execution of the function definition, fn(), stops at the return of 56. The statements that follow in the function definition are not executed. The calling function is, "fn();" in the main() function definition. The called function is the definition of fn().

The return Statement and the if-construct in a Function
The programmer can decide whether or not the statements below the return statement in a function, can be executed, using the if-construct. In the next two programs, the return statement is inside the block of an if-construct in the function definition.

In the following program, the statements below the if-construct are not executed because the if-condition is true (read and test it).

    #include <stdio.h>
    
    float flt = 2.5;

    float fn() {
        printf("%i\n", 1);
        printf("%i\n", 2);
        if (flt == 2.5)
            {
                return 56.0;
            }
        printf("%i\n", 3);
        printf("%i\n", 4);
    }
    
    int main(int argc, char *argv[])
    {
        float var = fn();
        printf("%f\n", var);

        return 0;
    }

The output is: 1, 2, 56.000000, on three different lines. In this fn() function definition and the one below, there is no concluding return statement. In the following program, the statements below the if-block are executed, because the if-condition is false (read and test it):

    #include <stdio.h>
    
    float flt = 2.5;

    float fn() {
        printf("%i\n", 1);
        printf("%i\n", 2);
        if (flt == 1.1)
            {
                return 56.0;
            }
        printf("%i\n", 3);
        printf("%i\n", 4);
    }
    
    int main(int argc, char *argv[])
    {
        float var = fn();
        printf("%f\n", var);

        return 0;
    }

The output is: 1 2 3 4 0.000000 on five different lines. The fifth value occurs as 0.000000 because the fn() function definition returned an empty value. This means that the var variable in the main() function, received nothing. And so it assumed the default float value of 0.000000 .

Returning from outside the Function
A function does not only have to return what has been created inside the function definition. It can also return what has been created outside the function. The following program illustrates this, with the fn() function:

    #include <stdio.h>
    
    float flt = 2.5;

    float fn() {
        //some statements
        return flt;
    }
    
    int main(int argc, char *argv[])
    {
        float var = fn();
        printf("%f\n", var);

        return 0;
    }

The output is 2.500000.

Default else
The default else part of the if/else-if/else construct can be the concluding return statement. The following program illustrates this:

    #include <stdio.h>
    
    int hisVar = 10000;

    char *const fn() {
        if (hisVar == 10)
            {
                printf("Value is small\n");
            }
        else if (hisVar == 100)
            {
                printf("Value is medium\n");
            }
        else if (hisVar == 1000)
            {
                printf("Value is large\n");
            }
        
        return "hisVar is very large\n";
    }
    
    int main(int argc, char *argv[])
    {
        char *const var = fn();
        printf("%s", var);

        return 0;
    }

The output is:

    hisVar is very large

In the fn() function definition, instead of typing,

        else
            {
                printf("hisVar is very large\n");
            }

the statement,

        return "hisVar is very large\n";

was typed on the last line, independently of the if/else-if construct.

Return in main
Everything being equal, the last statement in the main() function should be:

    return 0;

Here, the integer, 0 is returned to the caller, which is the operating system.



Related Links

More Related Links

Cousins

BACK NEXT

Comments