C语言基础学习

C语言基础用法——基本语句

以下为大一上期进校时C语言课程的程序,存在部分程序编写不合理的地方,还劳烦各位读者指正。

程序主要涉及到 if-else / while / for / switch 等基础语句的用法 (以代码的形式体现):

/* if-else语句的具体用法 */

#include<stdio.h>
int main(){
     int iAge;
     printf("How old are you?");
     scanf("%d",&iAge);
	 if(iAge==18){
 		printf("Ah,we are of the same age.\n");
 	} 
 	 if(iAge>=16 && iAge<=20){
	 	printf("I like you age.\n");
	 	printf("We can make friends.\n");
	 } 
	 else{
 		printf("Thank you.\n");
 	}
 	return 0;
}

----------------------------------------------------------------------------
/* 续 */
    
#include<stdio.h>
int main(){
	int iScore;
	printf("Please input your score(1-100):");
	scanf("%d",&iScore);
	if(iScore>=90 && iScore<=100){
		printf("Very Good.\n");
	}
	else if(iScore>=60 && iScore<90){      /*"Else if" is equal to "if" alone*/
		printf("Pass\n");
	}
	else if(iScore>=0 && iScore<60){
		printf("Fail.\n");
	}
	else if(iScore>100 || iScore<0){
		printf("Error.\n");
	}
	return 0;

}

-------------------------------------------------------------------------

/* switch语句的用法 */
    
#include<stdio.h>
int main() {
	int iChoice;
	printf("1.Blue\t2.Green\t3.Red\n");
	printf("What is your favorite color?\n");
	scanf("%d",&iChoice);
	switch(iChoice){     /*括号里面需为整型*/
	    case 1:printf("I love it\n");
	    break;
	    case 2:printf("It is not my favorite color\n");
	    break;  /*If break is ignored,it will go to case 3*/ 
	    case 3:printf("It is my favorite color too\n");
	    break;
	    default:printf("Error in section.\n");  /*Like number more than 3*/
	}
	return 0;
}

/* If change numbers to characters,the code will be changed to the following one */
#include<stdio.h>
int main() {
	char cChoice;
	printf("A.Blue\tB.Green\tC.Red\n");
	printf("What is your favorite color?\n");
	scanf("%c",&cChoice);
	switch(cChoice){
	    case 'A':printf("I love it\n");
	    break;
	    case 'B':printf("It is not my favorite color\n");
	    break;  /*If break is ignored,it will go to case 3*/ 
	    case 'C':printf("It is my favorite color too\n");
	    break;
	    default:printf("Error in section.\n");  /*Like number more than 3*/
	}
	return 0;
}

-------------------------------------------------------------------

/* Repitions */
/* while 循环的用法 */
#include<stdio.h>
int main(){
	int i=1;
	while(i<9){
	    printf("%d",i);
	    i++;
	}
	return 0;
}

--------------------------------------------------------------------

/* do-while的用法 */
#include<stdio.h>
int main(){
	int i=9;
	do{
        printf("%d",i);
	    i--;
	}while(i);  /*An expression is true if it is not null*/
	return 0;
} 

区别:while语句先判断,再运算;do语句先运算,再判断,当符合条件时则停止,否则停机
---------------------------------------------------------------------
/*for*/
#include<stdio.h>
int main(){
	int i;
	for(i=0;i<10;i++){
	printf("%d",i);
	}
	/* i=0;
	   * while(i<10){
                      *  printf("%d",i);
	      *  i++;
                        * }
                         */
		return 0; 
}

----------------------------------------------------------------------
#include <stdio.h>
int main(){
	int n,p,val;
	for(p=0,n=10,val=1;p<n;p++,val*=2){   //"val*=2" is equal to "val=val*2"//
	 printf("\n%d\t%d",p,val);	
	}
	return 0;
}

----------------------------------------------------------------------

/* 循环的嵌套(Nested loops) */
#include<stdio.h>
int main(){
	int i,j;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			printf("i=%d j=%d\t",i,j);
		} 
		printf ("\n");
	}
	return 0;
} 
----------------------------------------------------------------------
    
#include<stdio.h>
int main(){
	int i,j;
	for(i=0;i<9;i++){
		for(j=0;j<9;j++){      /*If change j<9 into j<=i,the code will become a tri-angel*/ 
			printf("*");  
		} 
		printf ("\n");
	}
	return 0;
} 

/*Multiplicationtable*/
#include<stdio.h>
int main(){
    int i,j;
    for(i=1;i<=9;i++){
       for(j=1;j<=i;j++){
          printf("%d*%d=%d ",j,i,i*j);
        }
        printf("\n");
    }
    return 0;
}

-----------------------------------------------------------------------

/* break,continue,goto语句用法 */
#include<stdio.h>
int main(){
	int i;
	for(i=0;i<20;i++){
		if(i*i>150){
			break; /*It causes early termination of a loop*/
		}
		printf("%d\t%d\n",i,i*i);
	} 
	return 0;
}
 
-------------------------------------------------------------------------
    
/*Use "for" to calculate multiplication*/
#include <stdio.h>
int main(){
	
	int i,j,sum;
	printf("Input a number:");
	scanf("%d",&j);
	
	for(i=1,sum=1;i<=j;i++){
		sum*=i;
	}
	
	printf("%d",sum);
	return 0;
}

-------------------------------------------------------------------------
    
/*break,continue,goto*/
#include<stdio.h>
int main(){
	int a=100,b=4,c,d;
	for(c=1;c<=5;c++){
		if(b-c==0){
		continue;  /*Skip this circle and make the next one*/
		}
		d=a/(b-c);
		printf("%d/%d=%d\n",a,b-c,d);
	}
	return 0;
}

-------------------------------------------------------------------------

/*break,continue,goto*/
#include<stdio.h>
#define MY_SECRET_NUMBER 7
int main(){
	int iGuessingNumber;
	printf("There is a secret number in my mind.\n");
	printf("It is between 0 and 9.\n");
	printf("Can you guess what it is?");
guessAgain:  /*Here is a loop*/
                 scanf("%d",&iGuessingNumber);
	if(iGuessingNumber!=MY_SECRET_NUMBER){
		printf("No please try again.\n");
		goto guessAgain;
	} 
	else{
		printf("Yes it is!");
	}
	return 0;
}

------------------------------------------------------------------------

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值