150 likes | 215 Views
Learn C programming with a practice exercise on continuous addition of integers between two positive numbers. See examples and assignment operators.
E N D
Speaker: Wen-Ching Lo 10/7計程實習課
第一次作業繳交方式 • 請將作業上傳到hw1資料夾中 • 遲交一天扣10分(三天以後就0分) • 繳交方式: • 作業完成後,將你的程式改名為 學號.c (ex:987030XX.c) • 使用以下指令修改權限:chmod 604 學號.c • 再執行 cp 學號.c /home1/student/phd98/d9806/cp/hw1
if • if ( grade >= 60 ) printf( "Passed\n" );
if…else… • if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n");
if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); }
if…else if…else • if ( x < -1 ) y = 1; else if ( x < 0 ) y = 2; else if ( x < 1 ) y = 3; else y = 4;
&& 和 || • && 且 Ex: if ( A && B ) { …//A和B同時都要成立 } • || 或 Ex: if ( A || B ) { …//A和B兩者只要成立一個 }
while • int product = 2; //初始值 while ( product <= 1000 ) product = 2 * product;
Examples of other assignment operators: c += 3 ( c = c + 3) d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9) • Increment operator (++) • Can be used instead of c+=1 • Decrement operator (--) • Can be used instead of c-=1
c=5 printf( “%d”, ++c );//先做加1的動作,再印出C • Prints 6 printf( "%d", c++ ); //先做印出C的動作,再加1 • Prints 5 • In either case, c now has the value of 6 • Preincrementing and postincrementing have the same effect ++c; printf( “%d”, c ); • Has the same effect as c++; printf( “%d”, c );
習題 • 連續加法運算 輸入兩個正數(起始值與結束值且起始值小於結束值才可運算)做這兩個數之間整數的連續加法
請輸入連續加法的起始值:1 請輸入連續加法的結束值:10 the sum is 55 請輸入連續加法的起始值:1 請輸入連續加法的結束值:100 the sum is 5050 請輸入連續加法的起始值:50 請輸入連續加法的結束值:1 Bye Bye!