Process control if

Keywords: less C

1, What is process control

Sequence of program code execution

2, Classification of process control

(1) Sequential execution

(2) Select execution
1. definition:
Some codes may or may not be executed, and some codes may be executed selectively
2. classification:
(1) if (more important than switch)
● the simplest use of if
Format: if statement;
○ function: if the expression is true, execute the statement; if the expression is false, do not execute the statement

/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: the simplest use of if
*/
# include <stdio.h>
int main(void)
{
	/*Expression logic value is 1 (for true) statement execution,
	Statement with expression logical value 0 (representing false) does not execute*/
	if (3>2)
		printf("AAAA\n");
	if (3)
		printf("BBBB\n");
	if (0)
		printf("CCCC\n");
	if (0 == 0)
		printf("DDDD\n");
	
	return 0;
}
/*
The output results in vs Ou 2013 are:
------------------------
AAAA
BBBB
DDDD
------------------------
*/

● scope of if (important)
① Without braces, the if range is the closest statement to itself
○ format 1:
If (expression)
Statement A;
Statement B;
if by default, only execution or non execution of A can be controlled, but execution or non execution of B cannot be controlled, and B will certainly execute.

/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: the scope of if
*/
# include <stdio.h>
int main(void)
{	
	/*All output, no braces, if range is the closest to itself
	That statement*/
	/*Whether the second statement belongs to if or not, it will output [because the expression is true
	And the second statement will be executed in sequence after the first statement is executed]*/
	if (3>2)
		printf("AAAA\n");
	printf("BBBB\n");

	/*According to the execution result, the second statement does not belong to if*/
	if (1>2)
		printf("CCCC\n");
	printf("DDDD\n");
	
	return 0;
}
/*
	The output results in vs Ou 2013 are:
-------------------------------
AAAA
BBBB
DDDD
-------------------------------
	Summary: if by default, only one statement can be executed or not
*/

○ format 2:
If (expression)
{
Statement A;
Statement B;
}
By default, if can only control the execution or non execution of one statement. If you want to control the execution or non execution of multiple statements, you must enclose these statements with {}.

/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: the scope of if
*/
# include <stdio.h>
int main(void)
{	
	if (3 > 2)
	{
		printf("AAAA\n");
		printf("BBBB\n");
	}

	printf("FFFF\n");

	if (1 > 2)
	{
		printf("CCCC\n");
		printf("DDDD\n");
	}
	
	printf("EEEE\n");
	
	return 0;
}
/*
The output results in vs Ou 2013 are:
------------------------
AAAA
BBBB
FFFF
EEEE
------------------------
*/

if... else... Usage
② Scanf and other similar functions are not very safe. To ensure the security of the program, it is recommended to use the safe version at the end of_s -- > scanf_s

/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: if else... Usage
*/
# include <stdio.h>
int main(void)
{	
	int i, j;
	
	printf("Please input i,j , separated by spaces:");
	/*error C4996: 'scanf': This function or 
	variable may be unsafe. Consider using 
	scanf_s instead. To disable deprecation, 
	use _CRT_SECURE_NO_WARNINGS. See online 
	help for details.*/
	/*scanf And other similar functions are not safe. To ensure that
	For the security of the program, it is recommended to use the safe version ending in "us" later
	->scanf_s*/
	scanf_s("%d %d",&i,&j);
	
	if (i > j)
		printf("i greater than j\n");
	else
		printf("i less than j\n");
	
	return 0;
}
/*
	The output results in vs Ou 2013 are:
--------------------------------
Please enter the values of i, j, separated by spaces:
9 2
i Greater than j
--------------------------------
	Conclusion: the explanation of Mr. Hao bin in 2009
	The file name is: if else...
	Or there is "." in the file name. You need to add. c to the suffix of the file name
	Otherwise, there will be problems with the file
*/
/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: if else... Usage
*/
# include <stdio.h>
int main(void)
{
	/*if......else......First statement*/
	/*if Without braces, you can only control the statement closest to it*/
	if (3 > 2)
		printf("AAAA\n");
	/*Otherwise, all but if*/
	/*else Without braces, you can only control the statement closest to it*/
	else
		printf("BBBB\n");
	/*Second statement*/
	printf("CCCC\n");
	
	return 0;
}
/*
	The output results in vs Ou 2013 are:
--------------------------------
AAAA
CCCC
--------------------------------
*/

③ Pause: system(pause) for output pause;
④ alt + F4 off
If... else if…… Else... Usage
Format:
if(1)
Statement A;
else if(2)
Statement B;
else if(3)
Statement C; (statement C can be executed, indicating that neither 1 nor 2 is tenable, nor 3 is tenable. Sequential execution, no matter whether or not the statement C is followed by the established statement)
else
Statement D;

/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: if else if…… Else... Usage
*/
# include <stdio.h>
int main(void)
{
	double delta = -1;

	if (delta > 0)
		printf("There are two solutions.\n");
	/*printf("Ha ha \ n "); error adding this statement*/
	//Otherwise, all but if
	else if (delta == 0)
		printf("There is a solution.\n");
	else
		printf("No solution!\n");
	
	return 0;
}
/*
	The output results in vs Ou 2013 are:
--------------------------------
No solution!
--------------------------------
*/

● C language's handling of true and false
Non zero is true.
Zero is false.
True 1
False 0
● if example - grade of score
100~90 excellent
90~80 good.
80~60 pass
60 ~ 0 fail

/*
Time: January 2020
 Editor: pinkowonote
 Function:
Purpose: if example - grade of score
*/
# include <stdio.h>
int main(void)
{
	float score; 
	
	printf("Please enter your test score:");
	scanf_s("%f", &score);

	if (score > 100)
		printf("You must be dreaming!\n");
	else if (score >= 90 && score <= 100)
		printf("excellent\n");
	else if (score >= 80 && score < 90)
		printf("good\n");
	else if (score >= 60 && score < 80)
		printf("pass\n");
	else if (score >= 0 && score < 60)
		printf("Fail,!Please keep trying!\n");
	else /*No more input is required (score < 0) because else (expression)
	Include all cases except the previous expression!*/
		printf("The score you entered is too low. Please do not feel inferior!\n");

	return 0;
}
/*
	The output results in vs Ou 2013 are:
--------------------------------
Please enter your test score: 65
 pass
--------------------------------
*/

● analysis of common problems of if
○ sort any three numbers

(2)switch

(3) Circular execution
1. definition:
2. classification:
(1)for
(2)while
break
Continue

Published 4 original articles, won praise 0, visited 28
Private letter follow

Posted by snowman15 on Tue, 21 Jan 2020 04:45:24 -0800