Tải bản đầy đủ (.pdf) (52 trang)

Chapter 5 Decisions,List, Conditions And Loops

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (387.63 KB, 52 trang )

Chapter 5
Decisions,List,
Conditions And Loops
Programming In
C Shap
© 2009
4-2
if Single-Selection Statement
If (condition)
statements to perform if condition=true
Vídụ:
if ( grade >= 60 )
statements to perform if condition=true
Giátrị kiểu bool
(true/false)
© 2009
4-3
if Single-Selection Statement
Chúý: Sau câu lệnh if chỉ thực hiện được 1 câu lệnh nếu muốn từ 2 câu
lệnh trở lên ta phải bao bọc giữa {} vídụ:
Cúpháp cấu trúc điều khiển if khi cómột câu lệnh thực hiện
If (<Biểu thức điều liện>)
Câu lệnh thực hiện
Cúpháp cấu trúc điều khiển if khi có2 câu lệnh trở lên
If (<Biểu thức điều liện>)
{
Câu lệnh thực hiện
Câu lệnh thực hiện
}
© 2009
4-4


if else Double-Selection Statement
if ( grade >=5 )
statements to perform if condition=true
Else
statements to perform if conditions= false
print “Đạt”
Điểm>=5
true
print “Rớt”
false
Flowchart the If else Stucture – Lượt đồ câu điều kiện if else
© 2009
4-5
if else Double-Selection Statement
if (condition)
statements to perform if condition=true
else
statements to perform if conditions= false
© 2009
4-6
Nested if else Statements
If (condition)
statements to perform if
condition=true
[ElseIf (condition) Then
statements to perform if
1st condition=false and
2nd condition=true]
[Else
statements to perform if

both conditions= false]
if ( grade >= 90 )
Console.WriteLine( "A" );
else
if ( grade >= 80 )
Console.WriteLine( "B" );
else
Console.WriteLine( "C" );
© 2009
4-7
Relational Operators
for building Conditions
• Greater Than >
• Less Than <
• Equal To ==
• Not Equal To !=
• Greater Than or Equal To >=
• Less Than or Equal to <=
© 2009
4-8
• Negative numbers are always less than
positive numbers
• Strings can be compared also (don't forget to
enclose the strings in quotes) forASCII Chart,
case matters!
– JOAN is less than JOHN
– HOPE is less than HOPELESS
– Joan does not equal JOAN
• Numbers are always less than letters
– 300ZX is less than Porsche

Comparison Tips
© 2009
4-9
If Example #1
intCredits=CInt(txtCredits.Text)
if (intCredits < 32 )
radFreshman.Checked = True;
elseif (intCredits < 64 )
radSophomore.Checked = True;
elseif (intCredits < 96 )
radJunior.Checked = True ;
else
radSenior.Checked = True;
© 2009
4-10
If Example # 2
if (blnSuccessfulOperation = =True )
. . .
}
is equivalent to
?? Nên viết viết theo cách dưới sinh viên
diễn giải
If (blnSuccessfulOperation )
{
. . .
}
© 2009
4-11
ToUpperand ToLowerMethods
• Use ToUpperand ToLowermethods of the

String class to convert strings for
comparisons
txtGender.Text contains Male
If (txtGender.Text.ToUpper = "MALE" )
{
. . .
}
© 2009
4-12
Compound Conditions
• Join conditions using Logical Operators
– Or (||)either true evaluates to true
– And (&&) both true evaluates to true
– Not (!)reverses the condition, so that
true will evaluate false and
false will evaluate true
© 2009
4-13
Compound Conditions
Example # 1
if (radMale.Checked == True) && (CInt(txtAge.Text) < 21 )
{
mintMinorMaleCount + = mintMinorMaleCount
}
radMale not checked False
txtAge greater than 21 False
radMale not checked, txtAge less than 21 False
radMale checked, txtAge 21 or greater False
radMale checked, txtAge less than 21 True
© 2009

4-14
Compound Conditions
Example # 2
if (radJunior.Checked == True) && (radSenior.Checked = True )
mintUpperClassCount + = mintUpperClassCount
Diễn Giải Nếu:
radJunior.Value=True True
radSenior.Value=True True
radJunior.Value=False, radSenior.Value=True True
radJunior.Value=True, radSenior.Value=False True
radJunior.Value=False, radSenior.Value=False False
© 2009
4-15
if (intTemp > 32 )
if (intTemp > 80 )
lblComment.Text = "Hot“;
Else
lblComment.Text="Moderate“;
Else
lblComment.Text="Freezing“;
Nested Ifs
© 2009
4-16
switch Multiple-Selection Statement
• Cấu trúc điều khiển Switch với nhiều lựa
chọn là phương án được thay thế cho cấu
trúc điều khiển if lồng nhau.
• Cấu trúc Switch sẽ chọn lựa ra một giải
pháp hợp lítừcác giải pháp được đưa ra.
• Cấu trúc này được sử dụng khi cócùng một

yêu cầu màcónhiều chọn lưa
© 2009
4-17
switch Multiple-Selection Statement
switch (<Biểu Thức>)
{
Case Giátrị 1 :
[ code to run]
break;
Case Giátrị 2 :
[ code to run]
break;
[default ]
[code to run]]
}
If expression=value in
constant list
If expression< >values in any
of the preceding constant lists
© 2009
4-18
Flocharting Switch Multiple Selection Structure.
Case a:
Actions a break;
Case b:
Actions b break;
Case n:
Action n break;
default
break;

true
true
true
true
false
false
false
© 2009
4-19
SwitchCase-Numeric Value
Example 1
switch ( intScore)
{
case 100:
lblMsg.Text="Excellent Score";
break;
case 99:
lblMsg.Text="Very Good";
break;
case 79:
lblMsg.Text="Excellent Score";
break;
default:
lblMsg.Text="Improvement Needed";
break;
}
© 2009
4-20
SwitchCase -String Value Example
switch (txtTeam.Text.ToUpper())

{
case "TIGERS":
(Code for Tigers)
break;
case "LEOPARDS" :
(Code for Leopards)
break;
}
© 2009
4-21
Capturing User Response to
Message Box
string strMessage;
DialogResult dlgResult;
strMessage = "Clear current order figures?";
dlgResult = MessageBox.Show(strMessage, "Clear",
MessageBoxButtons.YesNo , MessageBoxIcon.Question );
if (dlgResult == DialogResult.Yes)
//Code to clear the order
© 2009
4-22
Input Validation
• Check to see if valid values were entered by user
in TextBox
private void txtHeA_KeyPress(object sender, KeyPressEventArgs e)
{
//char.IsDigit(e.KeyChar) kiểm tra cóphải số không
//char.IsControl(e.KeyChar) kiểm tra phíp ConTrol như Backspace, Shifh
if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) ==
false)

e.Handled = true;// ép không nhận ký tự gõ vào
}
© 2009
4-23
Debugging
• Debug Menu
• Debug Toolbar
• Toggle BreakPointson/off by clicking
Editor's gray left margin indicator
• Step through Code, Step Into, Step Over
• View the values of properties, variables,
mathematical expressions, and conditions
© 2009
4-24
Debugging (cont.)
• Output Window
• Locals Window
• Autos Window
© 2009
4-25
Debug Menu and Toolbar

×