DAY5 C Language Control Flow Explained

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:

OperatorMeaning
>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:

OperatorMeaningExample
&&Logical AND (AND)a && b
``Logical OR (OR)`ab`
!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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值