Mundu Mira Kooperatiba Elkartea Urola Kostako Mankomunitatearekin elkarlaneanmundu-mira-kooperatiba
Urola Kostako Mankomunitateak barne eta kanpo komunikaziorako duen estrategia sendotzeko komunikaziorako aholkularitza teknikoaren zerbitzua eskainiko du MUNDU MIRA KOOPERATIBAk, beti ere, prentsa bulegoarekin batera elkarlanean, ekimen komunikatibo osatuagoa lortu ahal izateko.
1. ใบความรูที่ 3.2
โครงสราง if และ if...else
โครงสราง if เปนโครงสรางที่ใชควบคุมการทํางานของคําสั่งอื่น ๆ ภายใตเงื่อนไข (condition) ที่
กําหนด การใชงานนั้นมีสองรูปแบบคราว ๆ ไดแก
• รูปแบบที่ 1: โครงสราง if
จากการใชงานดานลาง คําสั่ง statement จะถูกเรียกทํางานก็ตอเมือนิพจนทางตรรกศาสตรที่
่
กําหนดเปน condition มีคาเปนจริง
if (condition)
statement; // executed if the condition is true
เนื่องจากโครงสรางขางตนอนุญาตใหเรากําหนดเงื่อนไขใหกับคําสั่งเพียงคําสั่งเดียวเทานั้น อยางไร
ก็ตาม หากมีคาสั่งมากกวาหนึ่งภายใตเงื่อนไขเดียวกัน คําสั่งเหลานี้สามารถถูกจัดกลุมใหเปน
ํ
เสมือนคําสั่งเดียวไดโดยการครอบคําสั่งทั้งหมดดวยวงเล็บปกกา ({...})
if (condition) {
statement1; // executed if the condition is true
statement2; // executed if the condition is true
statement3; // executed if the condition is true
:
}
• รูปแบบที่ 2: โครงสราง if...else
คําสั่ง statement1 จะถูกเรียกทํางานเมื่อนิพจนในตําแหนง condition มีคาเปนจริง หาก
นิพจนดังกลาวมีคาเปนเท็จ คําสั่ง statement2 จะถูกเรียกทํางานแทน
if (condition)
statement1; //executed if the condition is true
else
statement2; //executed if the condition is false
และเชนเคย เราสามารถใชงานโครงสราง if...else รวมกับวงเล็บปกกาหากมีคําสั่งที่ตองการ
ใหทํางานภายใตเงื่อนไขมากกวาหนึ่ง
if (condition) {
statementT1; //executed if the condition is true
statementT2; //executed if the condition is true
}
else {
statementF1; //executed if the condition is false
statementF2; //executed if the condition is false
}
สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี 53
2. ตัวอยางที่ 3.1 รหัสจําลอง (pseudo-code) ดานลางอธิบายขั้นตอนวิธีสําหรับใหโปรแกรมพิมพคําวา
Passed หากคะแนนของนักเรียนมีคามากกวาหรือเทากับ 60 ไมเชนนั้นใหพิมพคาวา Failed
ํ
if student's score is greater than or equal to 60
Print "Passed"
otherwise
Print "Failed"
เราสามารถนํารหัสจําลองขางตนมาเขียนเปนโปรแกรมภาษา C# ไดดงนี้ (แสดงเพียงสวนสําคัญเทานั้น)
ั
if (score >= 60)
Console.WriteLine("Passed");
else
Console.WriteLine("Failed");
เนื่องจากคียเวิรด else เปนตัวกําหนดใหการพิมพคําวา Failed ทํางานเมื่อเงื่อนไข score >= 60
เปนเท็จ ดังนันหากเราแทนที่ else ดวยคําสั่ง if และใชเงื่อนไขที่ตรงขามกันคือ score < 60
้
โปรแกรมก็จะมีการทํางานเหมือนกับโปรแกรมขางบนทุกประการ
if (score >= 60)
Console.WriteLine("Passed");
if (score < 60)
Console.WriteLine("Failed");
ตัวอยางที่ 3.2 โปรแกรมตอไปนี้จะรอรับตัวเลขจากผูใชและใหคําตอบวาตัวเลขนั้น ๆ เปนเลขคู (even) หรือ
เลขคี่ (odd)
• ใชรูปแบบ if
using System;
class OddOrEven {
static void Main() {
int N;
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
if (N%2 == 0)
Console.WriteLine("{0} is even", N); //true
if (N%2 != 0)
Console.WriteLine("{0} is odd", N); //true
}
}
54 สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี
3. • ใชรูปแบบ if...else
using System;
class OddOrEven {
static void Main() {
int N;
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
if (N%2 == 0)
Console.WriteLine("{0} is even", N); //true
else
Console.WriteLine("{0} is odd", N); //false
}
}
ตัวอยางที่ 3.3 บริษัทโทรศัพทมือถือแหงหนึ่งเสนอโปรโมชั่นใหกับลูกคาโดยมีการคํานวณคาธรรมเนียม
การใชงานดังนี้
• สองนาทีแรก คิดนาทีละหาบาท
• นาทีถัดมาคิดนาทีละสองบาท
โปรแกรมดานลางจะรับคาจํานวนนาทีจากผูใช และคํานวณคาธรรมเนียมการใชงาน นอกจากนี้
ภายในโปรแกรมยังมีคอมเมนตกํากับเอาไวหลายจุดเพื่ออธิบายการทํางานของโปรแกรมในสวนตาง ๆ
using System;
class Cellphone {
static void Main() {
// Step 1: Take the number of minutes input
Console.Write("Enter the number of minutes: ");
int minutes = int.Parse(Console.ReadLine());
// Step 2: Split the number of minutes into two parts,
// the first two and the remaining
int first, remaining;
if (minutes > 2) {
// Step 2.1: If the call takes more than two minutes,
// then the first two minutes is used entirely and the
// remaining is minutes subtracted by two
first = 2;
remaining = minutes - 2;
}
else {
// Step 2.2: If the call takes less than 2 minutes,
// these minutes are considered part of the first two
first = minutes;
remaining = 0;
}
// Step 3: Compute the fee based on the number of minutes
// during the first two minutes, and the number of minutes
// after the first two minutes
int fee = (first*5) + (remaining*2);
Console.WriteLine("The air time fee is {0} baht.", fee);
}
}
สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี 55
4. ตัวอยางผลการทํางาน
Enter the number of minutes: 1
The air time fee is 5 baht.
Enter the number of minutes: 5
The air time fee is 16 baht.
56 สถาบันสงเสริมการสอนวิทยาศาสตรและเทคโนโลยี