程式語言:C語言 第四堂課


好多內容,KEY到手軟 囧

流程控制:
if(條件式為真)
{
敘述句;
} //當敘述句只有一句時,{}可省略
else //否則(即條件式不成立)
{
敘述句;
}

判斷條件式:
常使用比較運算子 > >= < <= == != 邏輯運算子 && || !(反向運算子) *真值表*
&&

||


巢狀迴圈可增加程式執行效率
if(條件式為真)
{
if(條件式為真)
{
敘述句;
}
}

#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
/* 變數宣告 */
char word[255] = "請輸入你的分數:";
int score;
/* 資料輸入 */
printf("%s",word);
scanf("%d",&score);
/* 判斷式 */
if(score <= 100 && score >= 0)
{
if(score >= 60)
{
printf("你及格了!\n");
}

if(score > 50 && score < 60) { printf("你可以參加補考\n"); } else if(score < 50 && score >=0)
printf("很可惜,你不能參加補考\n");

}
else
{
printf("輸入錯誤,請重新執行程式\n");
}

system("pause");
return 0;
}


連續判斷
if(條件1)
{
敘述式;
}
else if(條件2)
{
敘述式;
}
else
{
敘述式;
}

#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
/* 宣告變數 */
char txt[255] = "請輸入運算式:";
char oper;
int num1, num2;
/* 輸入資料 */
printf("%s",txt);
scanf("%d %c %d",&num1, &oper, &num2);
/* 判斷式 */
if( oper == '+')
{
printf("%d %c %d = %d\n", num1, oper, num2, num1 + num2);
}
else if(oper == '-')
{
printf("%d %c %d = %d\n", num1, oper, num2, num1 - num2);
}
else if(oper == '*')
{
printf("%d %c %d = %d\n", num1, oper, num2, num1 * num2);
}
else if(oper == '/')
{
printf("%d %c %d = %.2f\n", num1, oper, num2, (float)num1 / (float)num2);
}

system("pause");
return 0;
}


switch(字元或整數)
{
case 字元或整數:
敘述句;
break; //宣告結尾

case:
敘述句;
break;

default: //當前面的判斷都不成立時執行
敘述句;
break; //可用可不用
}

#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
/* 宣告變數 */
char txt[255] = "請輸入運算式:";
char oper;
int num1, num2;
/* 輸入資料 */
printf("%s",txt);
scanf("%d %c %d",&num1, &oper, &num2);
/* switch判斷式 */
switch(oper)
{
case '+':
printf("%d %c %d = %d\n", num1, oper, num2, num1 + num2);
break;
case '-':
printf("%d %c %d = %d\n", num1, oper, num2, num1 - num2);
break;
case '*':
printf("%d %c %d = %d\n", num1, oper, num2, num1 * num2);
break;
case '/':
printf("%d %c %d = %.2f\n", num1, oper, num2, (float)num1 / (float)num2);
break;
default:
printf("不可識別的運算式\n");
break;
}

system("pause");
return 0;
}


標籤:
敘述句;
goto 標籤; //少用,會破壞結構

while(條件式) //條件不成立的話不會執行
{
敘述式;
break; //終止
}

#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
/* 宣告變數 */
int sum = 0;
int i = 0;
/* while判斷式 */
while(i<=5) { sum = sum + i; i = i + 1; } printf("%d\n", sum); system("pause"); return 0; }


do //就算條件不成立也至少執行一次
{
敘述式;
}while(條件式);

#include <stdio.h>
#include <stdlib.h>
int main (vold)
{
int passwd;

do
{
printf("請輸入密碼:");
scanf("%d",&passwd);
if(passwd == 1234)
{
printf("密碼正確\n");
break;
}
else
{
printf("密碼錯誤\n");
system("pause");
system("cls");
}
}while(1>0);

system("pause");
return 0;
}


※自定函數
函數=>提供特定功能的黑盒子

傳回值資料形態 函數名稱(資料形態 參數名稱)
(
函數內容;
return 傳回變數名稱;
}

函數內宣告的變數名稱可與主函數(main)的名稱一樣
(系統會將它們存放在不同位址)

【myh.h檔】
int tarea (int a,int b)
{
int area;
area = a * b / 2;
return area;
}


#include <stdio.h>
#include <stdlib.h>
#include "myh.h"
int main (vold)
{
int area;
area = tarea(10,10);
printf("%d\n", area);
system("pause");
return 0;
}

0 回應:

◎感謝來訪,歡迎分享你的想法!
◎勾選「通知我」可以避免遺漏後續回覆的訊息
◎別忘了留下大名、連結等資料,我才有機會多認識你!