Frog fun, it turns out that you can use fish-style crazy words to figure out the branch structure in 30 seconds! ! !

Fish-style crazy talk: This crazy talk is not that crazy talk, but it is an easy-to-understand vernacular that can be understood and summarized after completing the questions and practicing and typing the code. I will try my best to insert fish-style crazy talk after each concept. Help everyone understand.
Maybe it’s not that rigorous. But the editor’s original intention is to make more people accept our concept.

This article will focus on a preliminary understanding of the concept of branch structure, as well as the characteristics and scenario usage of the two branch statements. People don’t talk much, so let’s get started.

Branch structure

1. What is the branch structure?

C language is a structured programming language. The structures here refer to sequential structures, branch (selection) structures, and loop structures. C language can realize these three structures. The branch structure we want to learn today is one of them.

Fish style crazy talk:

Just like because it seems to be raining today, you must bring an umbrella. This is a good example

2. The first selection statement: if statement

1.if basic sentence pattern

//if (expression)
 //Statement 1
#include<stdio.h>
int main()
{<!-- -->
int a = 2;
int b = (a == 2);
//When judging whether it is true, it returns 1 if it is true, and 0 if it is not true.
int d = (a == 3);
printf("%d\
%d\
", b,d);
    if (2 == a)
//Execute statement a if it is equal to 3, return non-0 and execute the following statement if
// Otherwise return 0, if is not executed
{<!-- -->
printf("I am a handsome guy\
");
}
if (3)
{<!-- -->
printf("hehe\
");
}
if (0)
{<!-- -->
printf("haha\
");
}
return 0;
}

Fish style crazy talk:

<1>. The preceding ” == ” means: “Equal
<2>. If whether to execute the following statement, see if () is 0, if it is 0, do not execute it, if it is not 0 (not necessarily 1) executed ;
<3>. As mentioned in the example: Determine whether a is equal to 2 and return 1 or 0
<4>. If is followed by a statement, curly braces are not required. When multiple statements are followed, curly braces must be used to connect them.
<5>. If () cannot be followed by “;” It is recommended that when you first get started, all if control execution should be marked with {} whether it is a single statement or multiple statements to avoid errors. .

<1>Error example:
#include<stdio.h>
int main()
{<!-- -->
if (2==3)
printf("haha\
");
printf("hehe\
");
printf("dede\
");

return 0;
}

<2>Error examples
#include<stdio.h>
int main()
{<!-- -->
if (0);
//Equivalent to -->
//if(0)
//;
//if directly controls the empty statement (empty statement)
printf("haha\
");
return 0;
}

Fish style crazy talk:

If the assignment is to put the constant on the right and the variable on the left, otherwise an error will be reported. It is recommended that you put the constant on the left and the variable on the right in the judgment part; so this can avoid the double equal sign from being written into a single equal sign, causing the judgment to become an assignment.



2.if-else sentence pattern

When the value of the expression is not 0, the if-controlled statement is executed. What happens when the expression is 0?
Then we can’t execute other statements?
At this time, the statement controlled by else comes in handy. When if is 0, the statement controlled by else can be executed.
Code below:

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


Fish style crazy talk:

Note that the mistakes I often make here are else + (judgment)

The following are error cases

3. Nested if statements

<1> .The basic version of the nested structure is shown:
#include<stdio.h>
int main()
{<!-- -->
int a = 0;
scanf("%d", & amp;a);
if (a > 0)
{<!-- -->
printf("positive number\
");
}
//First determine the condition of the first if a > 0;
else
//On the contrary: a<=0 and then select
{<!-- -->
//First judge a==0
if (a==0)
{<!-- -->
printf("zero\
");
}
//Finally a < 0.
else
{<!-- -->
printf("Negative number\
");
}
}
return 0;
}

<2>. A simplified version of the nested structure is shown:
#include<stdio.h>
int main()
{<!-- -->
int a = 0;
while (scanf("%d", & amp;a) != EOF)
{<!-- --> //Direction 1: a > 0;
if (a > 0)
{<!-- -->
printf("positive number\
");
}
else if (a == 0)
//Direction two: a==0
{<!-- -->
printf("zero\
");
}
// Direction three: a<0.
else
{<!-- -->
printf("Negative number\
");
}
}
return 0;
}

Fish style crazy talk:

In essence, friends can think of it this way: apart from the conditions, there are two more conditions —-> which is equivalent to three equal conditions.

3. The second selection statement: switch statement

1.switch basic sentence pattern

//switch (expression)
//{<!-- -->
// case value1: statement
//case value2: statement
//default: statement
//}
#include<stdio.h>
int main()
{<!-- -->
int n = 0;
while (scanf("%d", & amp;n)!=EOF)
{<!-- -->
switch(n)
//n must be an integer
{<!-- -->
case(1):
printf("Monday\
");
break;
//break means stop
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;
}
}
\t
return 0;
}
<1>Error examples
#include<stdio.h>
int main()
{<!-- -->
int n = 0;
while (scanf("%d", & amp;n)!=EOF)
{<!-- -->
switch(n)
//n must be an integer
{<!-- -->
case(1):
printf("Monday\
");
break;
//break means stop
case(2):
printf("Tuesday\
");
case(3):
printf("Wednesday\
");
case(4):
printf("Thursday\
");
break;
case(5):
printf("Friday\
");
case(6):
printf("Saturday\
");
case(7):
printf("Sunday\
");
break;
        default:
printf("Input error\
");
break;
}
}
return 0;
}

<2>.break

The operation ends permanently. As long as the break is executed on the line, the jump selection will be terminated and it will end directly.

Fish style crazy talk:

If every situation is similar, it is best to add break after a sentence, including after dafault.

<3>Clever use of break
#include<stdio.h>
int main()
{<!-- -->
int n = 0;
while (scanf("%d", & amp;n)!=EOF)
{<!-- -->
switch(n)
//n must be an integer
{<!-- -->
case(1):
case(2):
case(3):
case(4):
case(5):
printf("working day\
");
break;
case(6):
case(7):
printf("rest day\
");
break;
        default:
printf("Input error\
");
break;
}
}
return 0;
}

<4>.default

Outside the above range, default will be executed.

Fish style crazy talk:

default does not have to be placed at the end, it can be placed anywhere. It’s just our habit

2. Understanding the judgment part of switch

Everyone has discovered it, right? Are the judgment parts above all integers?
Yes, that’s it, case (i), this i must be an integer**** constant
As shown in the picture

Fish style crazy talk:

In fact, it is okay for us to use characters, but it is recommended that you use them sparingly.

At this time, some friends asked,
Characters are not numbers, so how can they be used in the judgment part? Doesn’t this contradict what you said above?
Yes, you are right, but I am also right
Actually! ! !
Characters are stored in the computer as binary integers, so they are equivalent to numbers.
If you don’t believe it, then look down –>

#include<stdio.h>
int main()
{<!-- -->
char n = 0;
while (scanf("%c", & amp;n)!=EOF)
{<!-- -->
\t   
switch(n)
//n must be an integer
{<!-- -->
case('a'):
case('b'):
case('c'):
case('d'):
case('e'):
printf("working day\
");
break;
case('f'):
case('g'):
printf("rest day\
");
break;
        }
}
return 0;
}


But this is defective > cannot be used default
Once used, the following error will appear:

Summary

This article mainly explains the two types of branch structures: if and switch
When using if, pay attention to the use of {} and : and the use of nested understanding of else and if outside the condition.
The functions of break and default of switch and the restrictions on using integers when making judgments.

Creation is not easy. Finally, I hope that after reading this blog post, my friends will support the three levels if they think it is good. If they are inappropriate, I will correct them in the comment area. I hope that my article can bring even a little gain to your family members. The editor’s biggest motivation for creation