1. Sequential Structure

The sequential structure is one of the basic structures in a program, where the code is executed line by line starting from the main function and ending when the function returns. It does not involve any conditions or loops, making it the simplest form of control flow.

Example:

#include <stdio.h>

int main(void) {
    int a = 5, b = 10;
    printf("a = %d, b = %d\n", a, b);  // Sequential execution
    return 0;
}

2. Branching Structure

A branching structure allows executing different blocks of code based on condition checks. The commonly used branching structures are if statements and switch statements. These statements let us perform different actions depending on the condition.

1. Relational Operators

Relational operators are used to compare two values, and the common operators include:

Operator Meaning
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to

In C, the result of relational operations is a Boolean value:

  • 0 represents false;
  • Non-zero values represent true.

Example: Using Relational Operators

#include <stdio.h>

int main(void) {
    int a = 5, b = 10;
    int result = a > b;  // Compare a and b
    printf("Result of a > b: %d\n", result);  // Output is 0 (false)
    return 0;
}

2. Logical Operators

Logical operators are used to work with Boolean values. The common logical operators include:

Operator Meaning Example
&& Logical AND (AND) a && b
` ` Logical OR (OR) `a b`
! Logical NOT (NOT) !a
Logical AND (&&)

Only when both expressions on the left and right are true, the whole expression becomes true.

Logical OR (||)

If either the left or right expression is true, the whole expression becomes true.

Logical NOT (!)

If the expression is true, ! converts it to false; if the expression is false, it converts it to true.

Example: Using Logical Operators

#include <stdio.h>

int main(void) {
    int a = 1, b = 0;
    printf("a && b = %d\n", a && b);  // Output 0 (false)
    printf("a || b = %d\n", a || b);  // Output 1 (true)
    printf("!a = %d\n", !a);          // Output 0 (false)
    return 0;
}

3. if Statements

The if statement is used to execute a code block based on a condition. It can be a simple if or an if with an else statement.

Example: Simple if Statement
#include <stdio.h>

int main(void) {
    int num = 5;
    if (num > 0) {
        printf("num is positive\n");
    }
    return 0;
}
Example: if with else Statement
#include <stdio.h>

int main(void) {
    int num = -5;
    if (num > 0) {
        printf("num is positive\n");
    } else {
        printf("num is negative\n");
    }
    return 0;
}
Example: Nested if Statement
#include <stdio.h>

int main(void) {
    int num = 5;
    if (num > 0) {
        if (num < 10) {
            printf("num is between 0 and 10\n");
        }
    }
    return 0;
}

4. switch Statement

The switch statement is used to handle multiple possible conditions. It is suitable for checking enumerated values or integer constants.

Example: switch Statement
#include <stdio.h>

int main(void) {
    int num = 2;
    switch (num) {
        case 1:
            printf("num is 1\n");
            break;
        case 2:
            printf("num is 2\n");
            break;
        default:
            printf("num is neither 1 nor 2\n");
    }
    return 0;
}

3. Looping Structures

Looping structures are used to repeatedly execute a block of code until a certain condition is met. Common types of loops include for loops, while loops, and do-while loops.

1. for Loop

A for loop is generally used when the number of iterations is known beforehand. The format is as follows:

for (initialization; condition; update) {
    // Loop body
}
Example: for Loop
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    return 0;
}

2. while Loop

A while loop is suitable when the number of iterations is not known, and it will continue executing until the condition is false.

Example: while Loop
#include <stdio.h>

int main(void) {
    int i = 1;
    while (i <= 5) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

3. do-while Loop

The do-while loop is similar to the while loop, except it guarantees that the loop body will execute at least once.

Example: do-while Loop
#include <stdio.h>

int main(void) {
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 5);
    return 0;
}

4. Ternary Operator

The ternary operator is a concise form of conditional checking. It is structured as follows:

condition ? expression1 : expression2;
  • If condition is true, it returns expression1;
  • If condition is false, it returns expression2.
Example: Using the Ternary Operator
#include <stdio.h>

int main(void) {
    int num = 5;
    int result = (num > 0) ? 1 : 0;
    printf("result: %d\n", result);  // Output 1
    return 0;
}
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐