day02
補充:標識符
標識符:C語言規範,只能有字母(a-z,A-Z),數字(0-9),下劃線(_)構成,不能爲數字。
命名的時候,要做到見明知意。
大小寫是有區別的,比如tedu和TEDU是兩個不同的標識符。 C語言中雖然沒有規定正式的長度,但是一般也會受到不同編譯器的限制,也會受到操作系統的限制。
標識符的判斷:
3p ss*qq -6p
二、運算符
1.自增自減
如果,運算符放在數字的前面,先進行自增或者減運算,再進行其他運算; 如果,運算符放在數字的後面,先進行其他運算,再進行自增或者減的運算。
#include <stdio.h>
extern void opr1();
extern void sopr();
extern void switch_case();
//for循環
void for_demo(){
/*
//1 +3 + 5 + 7 + ...+ 99 int i, sum;
sum = 0;
for (i = 1; i < 100; i+=2) {
sum += i; }
printf("%d", sum);*/
int i, j;//用來表示i - 行與j - 列 for (i = 0; i < 9; i++)//控制行 {
for (j = 0; j < 9; j++)//控制列 {
printf("%s", "*"); }
printf("\n");//換行 }
printf(" * "); printf("***");
}
void test(){ /*
int x = 10;
int y = 20;
++x;//11
--y;//19 printf("%d,%d",x, y); x++;
y--; printf("%d,%d",x, y);
int x = 12, y = 1;
int p = x - (y--);//1
int q = (x++) - (--y);//2
printf("%d,%d",p, q);//11, 13 //14
*/
}
void while_demo(){
int i = 1; int sum = 0; do
{
sum += i; i ++; //語句塊
} while (i <= 100); printf("sum = %d", sum);
/*
int i = 1;
int sum = 0; while (i <= 100) {
sum = sum + i;
i++; }
printf("sum = %d", sum); */
/*
while (0)
{
printf("峯哥很牛逼!!!");
}*/ }
int main() {
for_demo(); //while_demo();
//opr1(); //sopr(); //switch_case();
return 0; }
2.除法運算
如果除數和被除數都爲整數的時候,那麼結果也是一個整數。 如果運算中有浮點型數據參與運算,那麼得到的結果是一個浮點型。系統會將整型隱式的轉化爲浮點類型。
3.取模運算 取模運算的結果正負取決於我們被模運算%左邊的符號,與模運算右邊的數的符號沒有關係。 重點:不能對小數進行取模運算。
#include <stdio.h>
/*三目運算符*/ void sopr()
{
/*
int a = 25, b = 38; int max;
if (a > b) {
max = a; }
else {
max = b; }
max = a > b ? a : b;
printf("max = %d\n", max); */
int year, day; printf("請輸入一個年份:"); scanf_s("%d", &year);
/*
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
printf("%d是閏年", year); }
(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) ? "閏年":"平年"; */
day = ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) ? 29 : 28; printf("%d", day);
}
4.三目運算符 一般形式:表達式1?表達式2:表達式3 ||“或”
&& “且”
!“非”
#include <stdio.h>
void opr1() {
//int a = 10, b = 20; //int x = a / b; //printf("%d", x);//0 //double x = a * 1.0 / b; //printf("%lf", x);//0.5 /*
int result; result = a result = a result = a */
- b; + b; * b;
int x = 9;
int y = 5;
int result;
result = x % y;//2
//result = -x % y;
printf("%d\n", -9 % 5);//-2 //printf("%d\n", 9.5 % -5);//-2 2 printf("%d\n", -9 % -5);//2 -2
}
三、分支結構-循環
1.if-else 在使用if語句的時候,我們需要注意以下兩點:
在使用if的時候,判斷條件要用括號括起來; 語句塊是由{}包圍起來,但是注意一般後面不需要加分號了,如果加上也沒關係,但是一般不建議這樣去使用。
2.switch-case
switch是另外一種選擇分支語句,用來代替我們之前使用的if-else的多個分支的語句,基本格式是下面的寫法:
switch(表達式){
case 整型數值1:語句1; case 整型數值2:語句2; case 整型數值3:語句3; ...
case 整型數值n:語句n; default:語句 n + 1;}
#include <stdio.h>
void switch_case() {
//定義一個年、月、日的基本數據類型int
int year, month, day = 31; printf("請輸入一個年月:"); scanf_s("%d", &year); scanf_s("%d", &month);
if(month < 1 || month > 12) {
printf("輸入正確的月份:1-12"); }
switch (month) {
/* case 1: case 3: case 5: case 7: case 8:
case 10: case 12:
day = 31;
break;*/ case 4:
case 6: case 9: case 11:
day = 30;
break; case 2:
//優化----
//day = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) ? 29:28; day = 28 + (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)); break;
}
printf("%d\n", day); }
3.while循環 while的一般格式如下:
while(表達式){ 語句塊;
}
int main(){
int i=1, sum=0;
while(i<=100){ sum+=i;
i++; }
printf("%d\n",sum);
return 0; }
代碼分析:
1) 程序運行到 while 時,因爲 i=1,i<=100 成立,所以會執行循環體;執行結束後 i 的值變爲 2,sum 的值變爲 1。
2) 接下來會繼續判斷 i<=100是否成立,因爲此時 i=2,i<=100 成立,所以繼續執行循環體;執行結束後 i 的值變爲 3,sum 的值變爲3。
3) 重複執行步驟 2)。
4) 當循環進行到第100次,i 的值變爲 101,sum 的值變爲5050;因爲此時 i<=100 不再成立,所以就退出循環,不再執行循環體,轉而執行while循環後面 的代碼。
如果循環條件中不包含變量,會發生什麼情況呢?
1) 循環條件成立時的話,while 循環會一直執行下去,永不結束,成爲“死循環”。
2) 循環條件不成立的話,while 循環就一次也不會執行。運行程序,什麼也不會輸出。
while(1){ printf("qqq");
}
while(0){ printf("qqqq");
}
4.do-while循環 do-while循環的一般格式看下面:
do-while與while的區別如下: 它會先執行一下“語句塊”,如果爲真,就會繼續執行循環;如果是假,則終止循環。不管如何,do-while總會至少執行一次“語 句塊”;
在實際開發中,while的應用要比do-while要多。
5.for循環
執行順序 作業:
1.設計一個百分制的成績評定:
0-59分: D 60-80分:C 80-90分:B 91-100分:A 要求:不能輸入小於0的數,也不能輸入大於100的數字。 2.閏年的判斷,如果是閏年,則輸入月份後,打印出對應的天數。利用三目運算符結合使用。 3.畫菱形。
do
{
//語句塊
} while (表達式);
int i = int
do {
} while (i <= 100); printf("sum = %d", sum);
1;
sum = 0;
sum += i; i ++; //語句塊
for(表達式1;表達式2;表達式3){ 語句塊;
}