[C Programming Tutorial Experiment] Basic Exercises (6)

code one

//执行下列程序后,a和b的输出值分别是?
 
#include <stdio.h>
int main()
{
    
    
	int a=3,b=5,c;
	c=(a>b)? a++ :b--;
	printf("a=%d,b=%d,c=%d\n",a,b,c); //a=3,b=4,c=5 √
	
	return 0;	
 } 

The output is:

The basics of this question, the answer is obvious!
insert image description here

code two

//执行下列程序段后,x、y、z的值分别是?
#include <stdio.h>
int main()
{
    
    
	int x=10,y=20,z=30;
	if(x>y) z=x; x=y; y=z;
	
	printf("x=%d,y=%d,z=%d\n",x,y,z); //x=20,y=30,z=30 √ 
  	return 0;
  }  

Note that there is no curly braces after the judgment of the if statement, then in this question, since x and y do not satisfy the judgment in the if conditional statement, it is said that z=x; will not be executed. Then the two statements x=y; y=z; are executed.
insert image description here

code three

//以下程序输出为?
#include <stdio.h>
int main()
{
    
    
	int a=5,b=0,c=0;
	if(a+b+c) printf("***\n"); else printf("$$$\n");
	// *** √ 
	return 0;
 }

Output:
If the if statement is satisfied, that is, if the condition in the if is true, then the print statement immediately following the if statement will be executed directly, and the statement after the else will not be executed, so the following result is output.
insert image description here

code four

 //以下程序输出结果为?
#include <stdio.h>
int main()
{
    
    
	int m=5;
	if(m++>5) 	printf("%d\n",m); // 6
	else 	printf("%d\n",m--); 
	
	return 0;    
} 

The result is obvious:
insert image description here
next test:

//测试一下:
#include <stdio.h>
int main()
{
    
    
	int m=4; //这里把m改成4看一下。 
	if(m++>5) 	printf("%d\n",m); 
	else 	printf("%d\n",m--); //5。因为 m-- 是先使用后自减  
	
	printf("m=%d\n",m); //所以说这时m的值为4 
	return 0;    
} 

Input: Combining the content in the comment and the above example, it can be understood.
insert image description here

code five

//执行下列程序后,x的值为?
#include <stdio.h>
int main()
{
    
    
	int a=2,b=5,c=7,d=8;
	int x;
	
	if(a>b)   //不满足,所以直接执行下面else中的x=7 
		if(c>d) x=1;
		else if(a<c)
			if(b<d) x=2;
			else x=3;
		else x=6;
	else x=7;
	
	
	printf("x=%d\n",x);
	
	return 0;
 } 

This sub-question is mainly to understand which else if matches with.

insert image description here
You can test it again:

//测试 
 //执行下列程序后,x的值为?
#include <stdio.h>
int main()
{
    
    
	int a=2,b=5,c=7,d=8;
	int x;
	
	if(a<b)   //这里 修改为a<b。满足,故继续执行下面的第一个if 
		if(c>d) x=1; //不满足,所以继续执行下面的else-if判断语句   注意喔! 
		else if(a<c) //满足,所以执行下面的if语句 
			if(b<d) x=2; //满足,所以执行x=2。而后跳出循环 
			else x=3;
		else x=6;
	else x=7;
	
	
	printf("x=%d\n",x); //x=2
	
	return 0;
 } 

The output is:
insert image description here
analyze it:
insert image description here

Code 6: switch, break,

 //运行下列程序,输出结果为?
#include <stdio.h>
int main()
{
    
    
	int x=1,a=0,b=0;
	switch(x){
    
    
		case 0: b++; // 0不满足。所以需要继续执行 
		case 1: a++; // 1。满足,所以执行a++,得到a的值为1 
		case 2:	a++; b++; //上一条语句中没有break,所以这条语句必定会执行 
						//所以a为2,b为1 
	}
	printf("a=%d,b=%d\n",a,b); // a=2,b=1 
	return 0;
   }

The output is as follows:
insert image description here
add break and test again:

//测试
#include <stdio.h>
int main()
{
    
    
	int x=1,a=0,b=0;
	switch(x){
    
    
		case 0: b++; // 0不满足。所以需要继续执行 
		case 1: a++;  break;// 1。满足,所以执行a++,得到a的值为1 
		case 2:	a++; b++; //上一条语句中有break,执行完之后直接跳出break;所以该条语句不执行。 
						//所以a为1,b为0(b仍然为原值) 
	}
	printf("a=%d,b=%d\n",a,b); // a=1,b=0 
	return 0;
   }    
   

Output:
see comments!
insert image description here
If the above break is changed to continue, then... an error will be reported! ! !
┭┮﹏┭┮ ┭┮﹏┭┮ ┭┮﹏┭┮ ┭┮﹏┭┮ ┭┮﹏┭┮
insert image description here

Guess you like

Origin blog.csdn.net/qq_44731019/article/details/123676941