C language learning experience (8)

Three structures of C language

1. Sequence structure

Execute sequentially from beginning to end (simple and easy to understand)

2. Choose a structure

(1) Branch statement
statement:
There is one in C language; it is a statement int a = 10;//A statement
has only one; it is also a statement (empty statement)

if statement

Syntax structure:
if (expression 1)
statement 1;
else if (expression 2)
statement 2;
else
statement 3;
specific code:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main(){
int a;
while ( 1){
scanf("%d", &a);
if (a <18)
{
printf("Underage\n");
}
else if (a >= 18 && a <30) //Pay attention to the two conditions Use logical characters to connect && or ||
{
printf("青年\n");
}
else
{
printf("中年\n");
}
}
return 0;
}
0 is false and the rest are true (negative numbers are also true (True)
Note:
The match of else is matched with the nearest if (unmatched) of else

. Try to put the constant to the left in the judgment statement
if (5 == num)//Don't just write one = assignment

example:

Output an odd number between 1-100.
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main(){
int i;
for (i = 1; i <= 100; ++i){
if (1 == i%2)
{
printf("%d\n", i);
}
}
return 0;
}

switch statement

Simplify multiple if statements
switch allows nested
structure:
switch (integer expression)
{
statement item; //case integer constant expression: + statement
)
Specific code:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main(){
int day=2;
switch (day)//day must be an integer
{
case 1:
printf("Monday");
break; //break statement
case 2:
printf("Tuesday");
break ;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
}
return 0;
}//The output result is Tuesday if not Add break; after entering the corresponding case, it will continue to output the statement
conclusion:
case is the entrance and break is the exit

example:

Output 1-5 for working days and 6-7 for rest days.
Specific codes:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main(){
int day;
while (1){
scanf("%d", &day);
switch (day){
case 1:
case 2:
case 3:
case 4:
case 5:
printf("workday\n ");
break;
case 6:
case 7:
printf("holiday\n");
break;
}
}
return 0;
}
If the input range exceeds the limited value of case, add
default: //That is, when input 22, it will output none
printf("none\n");
break;

3. Loop structure

while statement

打印1-10:
int main(){
int i = 1;
while (i<=10)
{
printf("%d\n", i);
++i;
}
return 0;
}

break statement and continue statement

Both are
breaks that act on the loop statement ; // jump out of the loop
continue; // end the current loop and restart a new loop, that is, terminate the code behind continue and start a new round

getchar() function and putchar() function

Input and output strings // but only the first character is returnedUsage
:
int main(){
char ch=getchar(); //char can be replaced by int float double, but when int, xzc can only output x instead of xzc, 10 can only output 1
printf("%c\n", ch); //putchar(ch);//The two are equivalent
return 0;
}//What character is input and what character is output

Guess you like

Origin blog.51cto.com/15079723/2598246