Branch and Loop Statements

Posted by manishsinha27 on Thu, 06 Jan 2022 18:54:26 +0100

Catalog

Branch statement

1. if statement

2. switch statement

Loop statement

1. while statement

2. for loop

3. do...while() loop

Branch statement

Definition: Statements in the code are executed in order from top to bottom. Now you need to check a condition to execute different code according to different conditions, then you need to use branch statements.

1. if statement

if statement: is a common branch statement.

The syntax structure of the if statement:

1. Single Branch

if (expression)
Statement 1;
    else
Statement 2;

2. Multi-Branch

if (expression)
Statement 1;
else if
Statement 2;
    else
Statement 3;

Sample code:

//Single Branch
#include<stdio.h>
int main() {
    int price = 0;
    printf("Spoon price:");
    scanf("%d", &price);
    if (price < 10)
        printf("Cheap!\n");
    else
        printf("Slightly expensive!\n");
    return 0;
}

//Multi-Branch
#include<stdio.h>
int main() {
    int age = 0;
    printf("Age is:");
    scanf("%d", &age);
    if (age < 10)
        printf("Child\n");
    else if (age >= 10 && age < 18)
        printf("A slightly older child\n");
    else if (age >= 18 && age < 30)
        printf("Adult\n");
    else
        printf("Adult\n");
    return 0;
}

Note: 1. In C, 0 means false, while non-0 means true.

2. If the expression is true, the corresponding code block is executed.

3. A statement can be curly bracketed or omitted.

4. The rule for else matching is to pair with its nearest if.

2. switch statement

switch statement: is a branch statement that is often used in multi-branch situations.

Syntax structure of switch statement:

switch (integer expression)
    {

case integer expression constant:
Statements;

    .......

    default:
        break;
    }

Sample code:

#include<stdio.h>
int main() {
	int day = 0;
	printf("Please enter 1-7 View the day of the week:");
	scanf("%d", &day);
	switch (day)
	{
	case 1:
		printf("Monday\n");
		break;
	case 2:
		printf("Tuesday\n");
		break;
	case 3:
		printf("Wednesday\n");
		break;
	case 4:
		printf("Thursday\n");
		break;
	case 5:
		printf("Friday\n");
		break;
	case 6:
		printf("Saturday\n");
		break;
	case 7:
		printf("Sunday\n");
		break;
	default:
		printf("Input error\n");
		break;
	}
    return 0;
}

Note: 1. A break statement divides the list of statements into different branches and executes without breaking until a default or break is encountered.

2. default clause, that is, the contents of the default clause are executed when the value does not match the preset value of the case clause before the default clause.

3. The default clause can only appear once in a switch statement, but it can appear anywhere.

Loop statement

Loop statement: Code usually executes once and continues to execute subsequent code in the order from top to bottom, but sometimes a section of code needs to be executed multiple times, so a loop statement is required.

1. while statement

while statement: one of the looping statements.

Syntax structure of switch statement:

while (expression)
    {
Loop statements;
    }

Sample code:

#include<stdio.h>
//Print 1 - 10
int main() {
    int i = 0;
	while (i<10)
	{
		i++;
		printf("%d ", i);
	}
    return 0;
}

2. for loop

for loop: one of the looping statements.

The syntax structure of the for statement:

for (expression 1; expression 2; expression 3)
    {
Loop statements;
    }

Expression 1: Initialization section, used to initialize loop variables.

Expression 2: A conditional judgment statement that determines whether the loop terminates.

Expression 3: Adjust the section used to adjust the cyclic condition.

Sample code:

#include<stdio.h>
//Print 1 - 10
int main() {
    for (int i = 1; i <= 10; i++) {
        printf("%d ", i);
    }
    return 0;
}

Note: 1. Initialization, judgment and adjustment of for loop can be omitted.

2. The omission of judgment means that judgment is always true and becomes an endless cycle. (

3. do...while() loop

do...while() loop: one of the looping statements.

Do... The grammatical structure of while() statements:

       do {
Loop statements;
} while (expression);

Note: This loop statement will be executed at least once!

Sample code:

#include<stdio.h>
int main() {
    int i = 10;
    do {
        printf("%d\n", i);
    } while (i < 10);
    return 0;
}

Note: Since this loop is executed at least once, fewer scenarios are used. (

Topics: C Back-end