Skip to content

Latest commit

 

History

History

Chapter3-Control_Flow

Chapter 3:

Control FLow

Statements and Blocks

    {
        /* block */
        printf("hello");
    }

    for (...)
        printf("") // compound statement

If-Else

    if (expression)
        statement1
    else
        statement2

Else-If

    if (expression)
        statement
    else if(expression)
        statement
    else if(expression)
        statement
    else
        statement

binsearch

Exercice :

ex3-1

Switch

switch (expression)
    case const-expr: statements
    case const-expr: statements
    default: statements

switch count

Exercice :

ex3-2

Loops - While and For

    while (expression)
        statement

    for (expr1; expr2; expr3)
        statement
    
    // equivalent to

    expr1;
    while (expr2)
    {
        statment
        expr3;
    }

atoi shellsort reverse

Exercice :

ex3-3

Loops - Do while

    do
        statement
    while (expression);

itoa

Break and Continue

break

exit from for, while, and do while

tim

continue

go to the next iteration of for, while, do

    for (i + 0; i < n; i++)
        if (a[i] < 0) /* skipe negative elements */
            continue;
        /* do positive elements */

Goto and Labels

    for (...)
        for (...)
            if (disaster)
                goto error;

error:
    clean up the mess

real example :

    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            if (a[i] == b[j])
                goto found;
    /* didn't find any common element */
found
    /* got one: a[i] == b[j]*/

all the exercises:

Exercises