First acquaintance with branch statements and loop statements

There are five types of statements in C language:
control statement
function call statement
empty statement
expression statement
compound statement

Control statements are used to control the execution flow of the program to realize various structural modes of the program (C language supports three structures: sequential structure, optional
Selection structure, loop structure), they are composed of specific statement definers, and C language has nine control statements.
Can be divided into the following three categories:
Conditional judgment statements are also called branch statements: if statement, switch statement;
Loop execution statement: do while statement, while statement, for statement;
Turn statement: break statement, goto statement, continue statement, return statement.

Branch statement

if

Syntax structure of if statement:
①Single-branch statement: if (expression) + statement;
②Double branch statement: if (expression) + statement 1; else + statement 2;
③Multi-branch statement: if (expression) + statement 1; else if (expression 2) + statement 2; else (expression) + statement 3;
And the if statement can only control one statement by default, if you want to control the statement segment, you need to add {};

#include <stdio.h>

int main()
{<!-- -->
int age = 0;
scanf("%d", &age);
if(age < 18)
{<!-- -->
printf("Underage\
",age);
//Single branch statement. If the condition is true, execute the statement, otherwise do not output.
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int age = 0;
scanf("%d", &age);
if(age < 18)
{<!-- -->
printf("Underage\
",age);
// double branch statement
}
else (age >= 18)
{<!-- -->
printf("adult\
",age);
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int a = 0;
int b = 2;
if(a == 1)
{<!-- -->
if(b == 2)
printf("hehe\
");
else
printf("haha\
");
//else matches the nearest if
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int age = 0;
if(age > 18)
{<!-- -->
printf("Adult.\
");
printf("I'm in love.\
");
}
return 0;
}
//Output the three randomly input numbers in descending order
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main()
{<!-- -->
int a = 0;
int b = 0;
int c = 0;
scanf("%d %d %d", &a, &b, &c);
if (a < b)
{<!-- -->
int tmp = a;
a = b;
b = tmp;
}

if (a < c)
{<!-- -->
int tmp = a;
a = c;
c = tmp;
}

if (b < c)
{<!-- -->
int tmp = b;
b = c;
c = tmp;
}
printf("%d %d %d", a, b, c);
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int age = 0;
scanf("%d", &age);
if(age < 18)
{<!-- -->
printf("Underage\
",age);
//Multi-branch statement. If you need to control multiple outputs after the if statement, you need to use {}
}
else if(age >= 18)
{<!-- -->
printf("adult\
",age);
}
else
{<!-- -->
printf("Input error\
");
}
return 0;
}
#include <stdio.h>
int main()
{<!-- -->
int age = 0;
scanf("%d", &age);
if (age < 16)
printf("Youth.\
");
else if (age >= 16 & amp; & amp; age < 30 )
printf("Youth.\
");
else if (age >= 30 & amp; & amp; age < 40)
printf("Middle age.\
");
else if (age >= 40 & amp; & amp; age < 56)
printf("Mature.\
");
else if (age >= 56 & amp; & amp; age < 80)
printf("Old age.\
");
else
printf("Old birthday.\
");
\t\t
return 0;
}
#include <stdio.h>
int main()
{<!-- -->
int a = 0;
int b = 2;
if (a == 1)
//if...else...is a statement. When the first condition if is not true, the internal judgment is not executed.
{<!-- -->
if (b == 2)
{<!-- -->
printf("hehe\
");
}
}
else
//else matches the nearest if
{<!-- -->
printf("haha\
");
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int a = 0;
int b = 2;
if(a == 1)
{<!-- -->
if(b == 2)
printf("hehe\
");
}
else
{<!-- -->
printf("haha\
");
}
return 0;
}
//1. Determine whether a number is odd
#include <stdio.h>

int main()
{<!-- -->
int num = 0;
scanf("%d", &num);
if (num % 2 == 1)
{<!-- -->
printf("odd number", num);
}
return 0;
}
//2. Use for loop to output odd numbers between 1-100
#include <stdio.h>

int main()
{<!-- -->
int num = 1;
for(num = 1; num <=100; num + =2)
{<!-- -->
printf("%d ",num);
}
return 0;
}
//2.2 Use while loop to output odd numbers between 1-100
#include <stdio.h>

int main()
{<!-- -->
int num = 1;
while(num <= 100)
{<!-- -->
if(num % 2 == 1)
{<!-- -->
printf("%d ",num);
}
num++;
}
return 0;
}
//2.3 Use the improved method of while loop to output odd numbers between 1-100
#include <stdio.h>

int main()
{<!-- -->
int num = 1;
while(num <= 100)
{<!-- -->
printf("%d ",num);
}
num + =2;
}
return 0;
}

switch

The switch statement is also a branch statement, mainly used in multi-branch situations. Among them, case determines the entrance of the branch, and break determines the exit of the branch. Among them, the default statement can be placed in any appropriate position, it doesn’t matter before or after.
The grammatical structure of the switch statement is: switch (constant expression) + {statement item}; where the case integer constant expression: {statement};
Switch statements can be nested in switch statements, and a break can only jump out of one case, and cannot jump out of multi-layer case statements.

//Enter different branches through the case statement and perform corresponding output
#include <stdio.h>

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

int main()
{<!-- -->
int day = 0;
scanf("%d", & amp; day);
switch (day)
//In the switch statement, we can't directly realize the branch, and use it with break to realize the real branch.
{<!-- -->
case 1:
case 2:
case 3:
case 4:
case 5:printf("Workday\
");
break;
case 6:
case 7:printf("holiday\
");
break;
default:printf("Input error\
");
//When the value entered in the integer expression cannot be entered from the case statement, enter it from the default statement
break;
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int n = 1;
int m = 2;
switch (n)
{<!-- -->
case 1:
m++;
case 2:
n + + ;
case 3:
switch (n)
{<!-- -->
case 1:
n + + ;
case 2:
m++;
n + + ;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m=%d,n=%d\
", m, n); //5 3
\t
return 0;
}

Loop statement

while

The grammatical structure of the while statement is: while (expression) {loop statement};

#include <stdio.h>

int main()
{<!-- -->
int num = 0;
while (0 == num)
{<!-- -->
printf("hehe\
", num);
}
return 0;
}
#include <stdio.h>

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

int main()
{<!-- -->
int i = 1;
while(i <= 10)
{<!-- -->
if(5 == i)
{<!-- -->
break;
//break in while is used to permanently terminate the loop, when a certain condition is met in the loop, it directly terminates all subsequent loops
}
printf("%d ",i); //1~4
i + + ;
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 1;
while(i <= 10)
{<!-- -->
if(5 == i)
{<!-- -->
continue;
}
printf("%d ",i); //1~4 infinite loop
i + + ;
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 0;
while(i < 10)
{<!-- -->
i + + ;
if(5 == i)
{<!-- -->
continue;
//continue is used to terminate this cycle, that is, the code after continue in this cycle will not be executed again.
}
// Jump directly to the judgment part of the while statement. Make the entry judgment of the next cycle
printf("%d ",i); //1,2,3,4,6,7,8,9,10
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
char ch = 0;
scanf("%d", &ch);
//scanf function can receive various types of data
printf("%d\
",ch);
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
char ch = 0;
ch = getchar();
putchar(ch);
//getchar and putchar are only for character type data, and the getchar function does not need to receive parameters, but the putchar function needs to receive
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int ch = 0;
while((ch = getchar()) != EOF)
{<!-- -->
getchar(ch);
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
char password[20] = {<!-- --> 0 };
int ch = '\0';
printf("Please enter the password: >");
scanf("%s",password);
while(getchar() != '\
')
{<!-- -->
;
}
printf("Please confirm the password (Y/N):>");
ch = getchar();
if(ch == Y);
{<!-- -->
printf("The password is entered correctly.\
");
}
else
{<!-- -->
printf("Password input error.\
");
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
char password[20] = {<!-- --> 0 };
int ch = '\0';
printf("Please enter the password: >");
scanf("%s", password);

while (getchar() != '\
')
{<!-- -->
;
}

printf("Please confirm the password (Y/N):>");
\t
if (getchar() == 'Y')
{<!-- -->
printf("The password is entered correctly.\
");
}
else
{<!-- -->
printf("Password input error.\
");
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
    char ch = '\0';
    while ((ch = getchar()) != EOF)
    {<!-- -->
        if (ch < '0' || ch > '9')
            continue;
        putchar(ch);
    }

    return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int k = (m > n ? n : m);
while (1)
{<!-- -->
if (m % k == 0 & amp; & amp; n % k == 0)
{<!-- -->
break;
}
k--;
}
printf("%d", k);
return 0;
}

#include <stdio.h>

int main()
{<!-- -->
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
//scanf() reads a space or \0 to stop
int k = 0;
while (k = m % n)
{<!-- -->
m = n;
n = k;
}
printf("%d",n);
return 0;
}

 
//Find the greatest common multiple of two numbers
#include <stdio.h>

int main()
{<!-- -->
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int k = 0;
int c = m * n;
while (k = m % n)
{<!-- -->
m = n;
n = k;
}
printf("%d",c/n);
//least common multiple = multiply two numbers/greatest common divisor
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int arr[10] = {<!-- -->1,2,3,4,5,6,7,8,9,10};
int k = 7;
int i = 0;
int flag = 0;
\t
for(i = 0;i < 10;i ++ )
{<!-- -->
if(arr[i] == k)
{<!-- -->
printf("found, the subscript is: %d", i);
flag = 1;
break;
}
}
\t
if(flag == 0)
{<!-- -->
printf("Cannot find.\
");
}
\t
return 0 ;
}
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>

int main()
{<!-- -->
char arr1[] = "welcome to my party!!!!!!";
char arr2[] = "************************";
int left = 0;
int right = strlen(arr1) - 1;
while (left <= right)
{<!-- -->
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\
", arr2);
sleep(1000);
system("cls");
left ++ ;
right--;
}

printf("%s\
", arr2);

return 0;
}

for

The grammatical structure of the for loop statement: for (expression 1; expression 2; expression 3) loop statement; where expression 1 is the initialization part, expression 2 is the condition judgment part, and expression 3 is the adjustment part.
Do not modify the loop variable in the body of the for loop to prevent the for loop from getting out of control.
The initialization part, condition judgment part, and adjustment part of the for loop can be omitted, but when the judgment part is omitted, the judgment part is always true, and the loop is an endless loop

//Use the for loop to output the numbers from 1 to 10
#include <stdio.h>

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

int main()
{<!-- -->
int i = 0;
for(i = 1;i <= 10;i ++ )
{<!-- -->
if(5 == i)
{<!-- -->
break;
}
printf("%d ",i); //1,2,3,4
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 0;
for(i = 1;i <= 10;i ++ )
{<!-- -->
if(5 == i)
{<!-- -->
continue;
}
printf("%d ",i); //1,2,3,4,6,7,8,9,10
}
return 0;
}
//Use the array subscript to output the numbers from 1 to 10
#include <stdio.h>

int main()
{<!-- -->
int arr[10] = {<!-- -->1,2,3,4,5,6,7,8,9,10};
int i = 0;
for(i = 0; i < 10; i ++ )
{<!-- -->
printf("%d ",arr[i]);
}
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 0;
int j = 0;
for(i = 0;i < 3;i ++ )
{<!-- -->
for(j = 0;j < 3;j ++ )
{<!-- -->
printf("hehe\
");
}
}

return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 0;
int j = 0;
for(;i < 3;i ++ )
{<!-- -->
for(;j < 3;j ++ )
{<!-- -->
printf("hehe\
");
}
}

return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int x = 0;
int y = 0;
for(x = 0, y = 0; x < 3 & amp; & amp; y < 5; + + x, + + y)
{<!-- -->
printf("hehe\
");
}
return 0;
}
#include <stdio.h>
#include <string.h>

int main()
{<!-- -->
int i = 0;
char password[20] = {<!-- --> 0 };
int flag = 0;
\t
for(i = 1;i <= 3; i ++ )
{<!-- -->
printf("Please enter the password: >");
scanf("%s",password);
//To determine whether two strings are equal, you need to use the strcmp library function
if(0 = strcmp(password,"123456"))
//If the first string is less than the second string, strcmp returns a number less than 0;
{<!-- -->
//If the first string is greater than the second string, strcmp returns a number greater than 0;
printf("The password is entered correctly, the login is successful.\
");
//If the first string is equal to the second string, strcmp returns 0;
flag = 0;
break;
}
        else
        {<!-- -->
        printf("The %d password input is wrong.\
",i);
        }
   }
   
   if(flag = 0)
   {<!-- -->
   printf("The password is entered incorrectly three times, exit the program.\
");
   }
\t
return 0;
}
//calculate the factorial of n
#include <stdio.h>

int main()
{<!-- -->
int n = 0;
int i = 1;
int sum = 1;
scanf("%d", &n);
for (i = 1; i <= n; i ++ )
{<!-- -->
sum = sum * i;
}
printf("%d", sum);
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int n = 0;
int i = 0;
int ret = 1;
int sum = 0;
for(n = 1;n <= 10;n ++ )
{<!-- -->
ret = 1;
for(i = 1;i <= n;i ++ )
{<!-- -->
ret = ret * i;
}
sum = sum + ret;
}
printf("%d\
", sum);
\t\t
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int n = 0;
int i = 0;
int ret = 1;
int sum = 0;
scanf("%d", &n);
for(i = 1;i <= n;i ++ )
{<!-- -->
ret = ret * i;
sum = sum + ret;
}
\t\t
printf("%d\
", sum);
\t\t
return 0;
}
#include <stdio.h>
int main()
{<!-- -->
int n = 4;
int i = 1;
int sum = 1;
int add = 0;
for (i = 1; i <= n; i ++ )
{<!-- -->
sum = sum * i;
add = sum + add;
}
printf("%d", add);
return 0;
}
#include <stdio.h>
int main()
{<!-- -->
int i = 0;
int k = 0;
for(i =0,k=0; k=0; i + + ,k + + ) //k=0, the judgment is always false, and the loop does not enter
    k++;
return 0;
}
//Output prime numbers between 100 and 200
#include <stdio.h>
#include <math.h>
int main()
{<!-- -->
int i = 0;
for (i = 101; i <= 200; i + = 2)
{<!-- -->
int j = 0;
int flag = 1;
for (j = 2; j <= (int)sqrt(i); j ++ )
{<!-- -->
if (i % j == 0)
{<!-- -->
flag = 0;
break;
}
}

if (flag == 1)
{<!-- -->
printf("%d ", i);
}
}
return 0;
}

return 0;
}

do while

[Loop first and then judge, the loop statement is executed at least once]

#include <stdio.h>

int main()
{<!-- -->
int i = 0;
do
{<!-- -->
printf("%d ",i);
i + + ;
}wwhile(i <= 10);
\t
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 0;
do
{<!-- -->
printf("%d ",i);
if( 5 == i)
{<!-- -->
break;
}
i + + ;
}wwhile(i <= 10); //1,2,3,4,5
\t
return 0;
}
#include <stdio.h>

int main()
{<!-- -->
int i = 0;
do
{<!-- -->
printf("%d ",i);
if( 5 == i)
{<!-- -->
continue;
}
i + + ;
}wwhile(i <= 10); //1,2,3,4,5 infinite loop
\t
return 0;
}
//guess the number game

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void menu()
{<!-- -->
printf("************************\
");
printf("******** 1. play *****\
");
printf("********0.exit *****\
");
printf("************************\
");
}
//rand function is specially used to generate random numbers
//The rand function returns a random number between 0~RAND_MAX(32767)
// #define RAND_MAX 0x7fff
// The rand function needs to use a srand function to set the random number generator before using it
// The srand function only needs to be called once in the program, and does not need to be called frequently
// timestamp
// In C language, the time function returns the timestamp
// NULL - null pointer
//
void game()
{<!-- -->
  int num = 0;
// 1. Generate a random number
int ret = rand() % 100 + 1;
// 2. Guess the number
while (1)
{<!-- -->
printf("Please guess the number: >");
scanf("%d", &num);
if (num < ret)
{<!-- -->
printf("The guess is small, please continue to guess\
");
}
else if (num > ret)
{<!-- -->
printf("The guess is too big, please continue to guess\
");
}
else
{<!-- -->
printf("Congratulations! You guessed it!\
");
break;
}
}
  system("cls");
}

int main()
{<!-- -->
int input = 0;
srand((unsigned int)time(NULL));
do
{<!-- -->
menu();
//print menu
printf("Please choose whether to enter the game (1/0):>");
scanf("%d", &input);
switch (input)
{<!-- -->
case 1:
game(); //The game function is the overall logic of the number guessing game
break;
case 0:
printf("Exit the game program!\
");
break;
default:
printf("Input error, please re-enter!\
");
break;
}
} while (input);
\t
return 0;
}

goto statement

The C language provides goto statements that can be abused at will and tags that mark jumps. Theoretically speaking, the goto statement is not necessary. In practice, the code can be easily written without the goto statement.
However, the goto statement is still useful in some occasions. The most common usage is to terminate the processing of the program in some deeply nested structures.
For example: Jump out of two or more layers of loops at a time. In this case, using break in a multi-layer loop cannot achieve the purpose. It can only exit from the innermost loop to the previous loop.

#include <stdio.h>

int main()
{<!-- -->
again:
printf("hehe\
");
goto again;
return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main()
{<!-- -->
char input[20] = {<!-- --> 0 };
systme("shutdown -s -t 60");
again:
printf("Your computer will automatically shut down within 60s, if you enter: I am a big baby. The command will be automatically canceled.\
");
scanf("%s", &input);
if (strcmp(input, "big baby") == 0)
{<!-- -->
system("shutdown -a");
printf("The input is correct, the command has been cancelled.\
");
}
else
{<!-- -->
printf("Input error.\
");
goto again;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main()
{<!-- -->
char input[20] = {<!-- --> 0 };
systme("shutdown -s -t 60");
 while(1)
 {<!-- -->
printf("Your computer will automatically shut down within 60s, if you enter: I am a big baby. The command will be automatically canceled.\
");
scanf("%s", &input);
if (strcmp(input, "big baby") == 0)
{<!-- -->
system("shutdown -a");
printf("The input is correct, the command has been cancelled.\
");
break;
}
}
return 0;
}