Upload
a-num-sara
View
61
Download
3
Embed Size (px)
Citation preview
คำ��สั่��งแบบมี�เง �อนไขConditional Statements
C# Programming
เน �อห� นิ�พจนิ�ตรรกศาสตร�นิ�พจนิ�ตรรกศาสตร� คำ าส��งแบบมี�เง��อนิไขคำ าส��งแบบมี�เง��อนิไข ต�วอย่�างต�วอย่�าง โฟล์�วชาร�ตโฟล์�วชาร�ต
น�พจน�ตรรกศ�สั่ตร� ต�วดำ าเนิ�นิการตรรกศาสตร�ต�วดำ าเนิ�นิการตรรกศาสตร�สั่�ญลั�กษณ์�สั่�ญลั�กษณ์�คำณ์�ตศ�สั่ตร�คำณ์�ตศ�สั่ตร�
สั่�ญลั�กษณ์� สั่�ญลั�กษณ์� C#C# ต�วอย่"�ต�วอย่"�งง
คำว�มีหมี�ย่คำว�มีหมี�ย่
== ==== x == y x == y x x เท่"�ก�บเท่"�ก�บ y?y?
≠≠ !=!= x != yx != y x x ไมี"เท่"�ก�บไมี"เท่"�ก�บ y?y?
>> >> x > yx > y x x มี�กกว"�มี�กกว"� y?y?
≥≥ >=>= x >= yx >= y x x มี�กกว"�หร อมี�กกว"�หร อเท่"�ก�บเท่"�ก�บ y?y?
<< << x < yx < y x x น$อย่กว"�น$อย่กว"� y?y?
≤≤ <=<= x <= yx <= y x x น$อย่กว"�หร อน$อย่กว"�หร อเท่"�ก�บเท่"�ก�บ y?y?
ต�วอย่"�ง: น�พจน�ตรรกศ�สั่ตร�double x = 4.0; Expression Expression ValueValuex < 5.0 ___________x > 5.0 ___________x <= 5.0 ___________5.0 == x ___________x != 5.0 ___________
truefalse
truefalsetrue
ต�วอย่"�งdouble n1 = 78.0;double n1 = 78.0;double n2 = 80.0;double n2 = 80.0;
n1 < n2 __________ n1 >= n2 __________ (n1 + 35) > n2 __________ Math.Abs(n1-n2) <= 0.001 __________ n1 == n2 __________ n1 != n2 __________
truefalsetruefalsefalsetrue
ก�รผสั่มีน�พจน�ตรรกศ�สั่ตร� ต�วดำ าเนิ�นิการตรรกศาสตร�ต�วดำ าเนิ�นิการตรรกศาสตร�
&&&& - - แล์ะ แล์ะ (AND) (AND) |||| - - หร�อ หร�อ (OR)(OR) !! – – ไมี� ไมี� (NOT)(NOT)
ต�วอย่�างต�วอย่�าง (x > 12) && (x < 20)(x > 12) && (x < 20)
เป็%นิจร�งถ้'า เป็%นิจร�งถ้'า xx มี�คำ�าอย่(�ระหว�าง มี�คำ�าอย่(�ระหว�าง 12 12 แล์ะ แล์ะ 2020 ถ้'า ถ้'า xx เท่�าก�บ เท่�าก�บ 10 10 แล์'วนิ�พจนิ�ตรรกศาสตร�แล์'วนิ�พจนิ�ตรรกศาสตร�
ข'างต'นิเป็%นิเท่*จข'างต'นิเป็%นิเท่*จ ถ้'า ถ้'า xx เท่�าก�บ เท่�าก�บ 15 15 แล์'วนิ�พจนิ�ตรรกศาสตร�แล์'วนิ�พจนิ�ตรรกศาสตร�
ข'างต'นิเป็%นิจร�งข'างต'นิเป็%นิจร�ง
ลั��ดั�บก�รประมีวลัผลั(Precedence Rules)1.1. ( ) ( ) วงเล์*บวงเล์*บ2.2. *, / , % *, / , % 3.3. + –+ –4 .4 . <, >, <=, >= <, >, <=, >=5.5. ==, !===, !=6.6. !!7.7. && && 8.8. ││ ││ 9.9. ถ้'าล์ าดำ�บเท่�าก�นิให'ป็ระมีวล์ผล์จากซ้'าย่ไป็ถ้'าล์ าดำ�บเท่�าก�นิให'ป็ระมีวล์ผล์จากซ้'าย่ไป็
ขวาขวา
คำ��สั่��งแบบมี�เง �อนไข if...elseif...else switch...caseswitch...case
คำ��สั่��ง if
if (if (conditioncondition) ) statement;statement;
if (if (conditioncondition) ) statement;statement;
C# SyntaxC# Syntax
STARTSTARTSTARTSTART
ENDENDENDEND
conditionconditionconditionconditiontrue
falseStatementStatementStatementStatement
FlowchartFlowchart
อน)ญ�ตให$มี�ไดั$เพ�ย่งหน+�งคำ��สั่��งเท่"�น��น
ก�รผสั่มีคำ��สั่��ง เราสามีารถ้สร'างคำ าเราสามีารถ้สร'างคำ า
ส��งไดำ'มีากกว�าหนิ/�งส��งไดำ'มีากกว�าหนิ/�งคำ าส��งโดำย่การใส�คำ าคำ าส��งโดำย่การใส�คำ า
ส��งไว'ในิ ส��งไว'ในิ {{......}}
if (if (conditioncondition) ) {{ statement1; statement1; statement2;statement2; ::}}
if (if (conditioncondition) ) {{ statement1; statement1; statement2;statement2; ::}}
C# SyntaxC# Syntax
STARTSTARTSTARTSTART
ENDENDENDEND
StatementStatementStatementStatement
conditionconditionconditionconditiontrue
false
StatementStatementStatementStatement
FlowchartFlowchart
ต�วอย่"�ง: คำ��สั่��ง if อ�านิต�วเล์ขหนิ/�งจ านิวนิ แล์ะหาว�า อ�านิต�วเล์ขหนิ/�งจ านิวนิ แล์ะหาว�า
ต�วเล์ขนิ�0นิเป็%นิจ านิวนิคำ�� หร�อจ านิวนิ ต�วเล์ขนิ�0นิเป็%นิจ านิวนิคำ�� หร�อจ านิวนิคำ(�คำ(�
usingusing System; System;classclass EvenOrOdd { EvenOrOdd { staticstatic voidvoid MainMain() {() { intint n = int.Parse(Console. n = int.Parse(Console.ReadLineReadLine());()); ifif (n % 2 == 0) (n % 2 == 0) Console.Console.WriteLineWriteLine("{0} is even", n);("{0} is even", n); ifif (n % 2 == 1) (n % 2 == 1) Console.Console.WriteLineWriteLine("{0} is odd", n);("{0} is odd", n); }}}}
usingusing System; System;classclass EvenOrOdd { EvenOrOdd { staticstatic voidvoid MainMain() {() { intint n = int.Parse(Console. n = int.Parse(Console.ReadLineReadLine());()); ifif (n % 2 == 0) (n % 2 == 0) Console.Console.WriteLineWriteLine("{0} is even", n);("{0} is even", n); ifif (n % 2 == 1) (n % 2 == 1) Console.Console.WriteLineWriteLine("{0} is odd", n);("{0} is odd", n); }}}}
n%2==0n%2==0
n%2==1n%2==1
Print Print "even""even"
Print Print "odd""odd"
ENDEND
false
true
false
true
คำ��สั่��ง if…else…
if (if (conditioncondition) ) statementstatementtt;;elseelse statementstatementff;;
if (if (conditioncondition) ) statementstatementtt;;elseelse statementstatementff;;
C# SyntaxC# Syntax
STARTSTARTSTARTSTART
ENDENDENDEND
StatementStatementffStatementStatementff
conditionconditionconditionconditiontrue false
StatementStatementttStatementStatementtt
FlowchartFlowchart
ต�วอย่"�ง: คำ��สั่��ง if...else อ�านิต�วเล์ขหนิ/�งจ านิวนิ แล์ะหาว�า อ�านิต�วเล์ขหนิ/�งจ านิวนิ แล์ะหาว�า
ต�วเล์ขนิ�0นิเป็%นิจ านิวนิคำ�� หร�อจ านิวนิ ต�วเล์ขนิ�0นิเป็%นิจ านิวนิคำ�� หร�อจ านิวนิคำ(�คำ(�usingusing System; System;
classclass EvenOrOdd { EvenOrOdd { staticstatic voidvoid MainMain() {() { intint n = int.Parse(Console. n = int.Parse(Console.ReadLineReadLine());()); ifif (n % 2 == 0) (n % 2 == 0) Console.Console.WriteLineWriteLine("{0} is even", n);("{0} is even", n); elseelse Console.Console.WriteLineWriteLine("{0} is odd", n);("{0} is odd", n); }}}}
usingusing System; System;classclass EvenOrOdd { EvenOrOdd { staticstatic voidvoid MainMain() {() { intint n = int.Parse(Console. n = int.Parse(Console.ReadLineReadLine());()); ifif (n % 2 == 0) (n % 2 == 0) Console.Console.WriteLineWriteLine("{0} is even", n);("{0} is even", n); elseelse Console.Console.WriteLineWriteLine("{0} is odd", n);("{0} is odd", n); }}}}
n%2==0n%2==0
eveneven
ENDEND
falsetrue
oddodd
if หลั�ย่ชั้��น
condition1condition1
condition2condition2 condition3condition3
stmt2astmt2a stmt2bstmt2b stmt3astmt3a
true false
true false
true
false
ต�วอย่"�ง: if หลั�ย่ชั้��น อ�านิต�วเล์ขหนิ/�งจ านิวนิ แล์ะหาว�าต�วเล์ขนิ�0นิ อ�านิต�วเล์ขหนิ/�งจ านิวนิ แล์ะหาว�าต�วเล์ขนิ�0นิ
เป็%นิคำ�าบวก คำ�าล์บ หร�อคำ�าศ(นิย่� เป็%นิคำ�าบวก คำ�าล์บ หร�อคำ�าศ(นิย่�
n > 0n > 0
n < 0n < 0
Print Print "positive""positive"
true false
Read nRead n
Print Print "negative""negative" Print Print "zero""zero"
ENDEND
true false
usingusing System; System;classclass Sign { Sign { staticstatic voidvoid MainMain() {() { intint n = int.Parse(Console. n = int.Parse(Console.ReadLineReadLine());()); ifif (n > 0) (n > 0) Console.Console.WriteLineWriteLine("{0} is positive", n);("{0} is positive", n); else else {{ ifif (n < 0) (n < 0) Console.Console.WriteLineWriteLine("{0} is negative", n);("{0} is negative", n); elseelse Console.Console.WriteLineWriteLine("{0} is zero", n);("{0} is zero", n); }} }}}}
usingusing System; System;classclass Sign { Sign { staticstatic voidvoid MainMain() {() { intint n = int.Parse(Console. n = int.Parse(Console.ReadLineReadLine());()); ifif (n > 0) (n > 0) Console.Console.WriteLineWriteLine("{0} is positive", n);("{0} is positive", n); else else {{ ifif (n < 0) (n < 0) Console.Console.WriteLineWriteLine("{0} is negative", n);("{0} is negative", n); elseelse Console.Console.WriteLineWriteLine("{0} is zero", n);("{0} is zero", n); }} }}}}
ต�วอย่"�ง: โปรแกรมี C#
ก�รก��หนดัหลั�ย่เง �อนไข
false
Action1;Action1;x==1x==1
Action2;Action2;x==2x==2
Action3;Action3;x==3x==3
Action4;Action4;x==4x==4
true
true
true
true
false
false
false
Default_Action;Default_Action;
ก�รก��หนดัหลั�ย่เง �อนไขก�บ คำ��สั่��ง if
if if (x==1) (x==1) Action1;Action1;else if else if (x==2)(x==2) Action2;Action2;else if else if (x==3)(x==3) Action3;Action3;else if else if (x==4) (x==4) Action4;Action4;elseelse Default_Action;Default_Action;
if if (x==1) (x==1) Action1;Action1;else if else if (x==2)(x==2) Action2;Action2;else if else if (x==3)(x==3) Action3;Action3;else if else if (x==4) (x==4) Action4;Action4;elseelse Default_Action;Default_Action;
false
Action1;Action1;x==1x==1
Action2;Action2;x==2x==2
Action3;Action3;x==3x==3
Action4;Action4;x==4x==4
true
true
true
true
false
false
false
Default_Action;Default_Action;
ก�รใชั้$คำ��สั่��ง switch…case
switch switch (x)(x) { { case case 11: : Action1;Action1; break;break; case case 22: : Action2;Action2; break;break; case case 33: : Action3;Action3; break;break; case case 44: : Action4;Action4; break;break; default: default:
Default_Action;Default_Action; break;break;}}
false
Action1;Action1;x==1x==1
Action2;Action2;x==2x==2
Action3;Action3;x==3x==3
Action4;Action4;x==4x==4
true
true
true
true
false
false
false
Default_Action;Default_Action;
ไวย่�กรณ์� switch…case Syntax:Syntax:
<expression><expression> ต'องเป็%นิป็ระเภท่ต'องเป็%นิป็ระเภท่ ข'อมี(ล์ ข'อมี(ล์ intint, , charchar หร�อ หร�อ stringstring
switch (<expression>){ case <constant-expression>: <statements>; break;
default: <statements>; break;}
แบบฝึ/กห�ดั คำ านิวนิคำ�าใช'จ�าย่โท่รศ�พท่�โดำย่คำ�ดำตามีคำ านิวนิคำ�าใช'จ�าย่โท่รศ�พท่�โดำย่คำ�ดำตามี
เวล์าท่��ใช'เวล์าท่��ใช'Operator: LEMONOperator: LEMON
อ�นพ)ท่: ประเภท่โปรโมีชั้��น (promotion type)
จ��นวนเวลั�ท่��ใชั้$ (usage time)
เอ�ท่�พ)ท่: คำ"�ใชั้$จ"�ย่ (payment price)
ProgramProgram
Promotion TypePromotion Type
Usage timeUsage timePayment pricePayment price
สั่ร)ป นิ�พจนิ�ตรรกศาสตร�นิ�พจนิ�ตรรกศาสตร� คำ าส��งแบบมี�เง��อนิไขคำ าส��งแบบมี�เง��อนิไข
if...else...if...else... switch-caseswitch-case
if…else…if…else…
Condition ProblemsCondition Problems
switchswitch