if Single-Selection StatementChú ý : 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ộ
Trang 1Chapter 5 Decisions, List, Conditions And Loops
Programming In
C Shap
Trang 3if 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 }
Trang 4if else Double-Selection Statement
print “R ớt” false
Flowchart the If else Stucture – Lượt đồ câu điều kiện if else
Trang 5if else Double-Selection Statement
Trang 6Nested 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]
Trang 7• Greater Than or Equal To >=
• Less Than or Equal to <=
Trang 8• Negative numbers are always less than
positive numbers
• Strings can be compared also (don't forget to
enclose the strings in quotes) for ASCII Chart,
case matters!
– JOAN is less than JOHN
– HOPE is less than HOPELESS
– Joan does not equal JOAN
Comparison Tips
Trang 9radSenior.Checked = True;
Trang 11ToUpper and ToLower Methods
• Use ToUpper and ToLower methods of the
String class to convert strings for
Trang 12Compound 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
Trang 13radMale not checked, txtAge less than 21 False
radMale checked, txtAge 21 or greater False
radMale checked, txtAge less than 21 True
Trang 14radJunior.Value=False, radSenior.Value=True True
radJunior.Value=True, radSenior.Value=False True
radJunior.Value=False, radSenior.Value=False False
Trang 16switch 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
Trang 17switch Multiple-Selection Statement
If expression< >values in any
of the preceding constant lists
Trang 18Flocharting Switch Multiple Selection Structure
Case a: Actions a break;
Case b: Actions b break;
Case n: Action n break;
Trang 19SwitchCase - Numeric Value
Trang 20Switch Case - String Value Example
Trang 21Capturing 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
Trang 22//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
Trang 23Debugging
• Debug Menu
• Debug Toolbar
• Toggle BreakPoints on/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
Trang 24Debugging (cont.)
• Output Window
• Locals Window
• Autos Window
Trang 25Debug Menu and Toolbar
Trang 26Toggle Breakpoints On/Off by clicking
Trang 27Viewing Current Values
During Program Execution
Place mouse pointer over variable or property to view current value
Trang 28Locals Window
Trang 29Autos Window
Trang 30Cấu trúc lặp
Khi nào dùng vòng lặp?
Cấu trúc điều khiển lặp cho phép thực hiện
lặp đi lặp lại một hành động nào đó.
Trang 31Cấu trúc lặp While
Cấu trúc điều khiển lặp While Cho phép
thực hiện lặp đi lặp lại một hành động
trong lúc điều kiện thực hiện còn đúng.
true
false
i = i + 1
i < n
Trang 32Ví Dụ Cấu trúc lặp While
private void btnTong1_Click(object sender, EventArgs e)
{
int n=Convert.ToInt32 ( txtN.Text);
int i=1; string strList=""; long lngTong=0;
Trang 33Ví Dụ Cấu trúc lặp While
Richtexbox (rtxt)
Trang 34Cấu trúc lặp Do While
Tương tự While nhưng cấu trúc lặp Do While
kiểm tra điều kiện lặp sau
Trang 36Cấu trúc lặp For biết trước số lần
Thông thường, ta sử dụng vòng lặp biết trước số lần lặp
Syntax
for(<Biểu thức 1> <Biểu thức 2> <Biểu thức 3>)
giá trị khởi động.
trong vòng for.
Trang 37Ví dụ: lặp For
private void btnTong1_Click(object sender,
EventArgs e)
{
int n=Convert.ToInt32 ( txtN.Text);
int i=1; string strList=""; long lngTong=0;
for (i=1;i<=n;i++) {
lngTong+=i;
strList += i.ToString() + "+ ";
} rtxtKQ.Text = "Tổng dãy" + strList + "= "
+ lngTong;
}
Trang 38Lệnh Break
Chú ý: trong các vòng lặp nếu gặp câu lệnh
break thì lập tức thóat khỏi vònh lặp vô điều
kiện
Trang 39ListBoxes & ComboBoxes
• Provide a list for the user to select from
• Various styles, choose based on
– Amount of data to be displayed
– Space available
– User's ability to add to the list
Trang 40Types of List Controls
– List may allow for user to add new items
– List may "drop down" to display items in list
Trang 41List Controls Visually
List
Boxes
DropdownCombo Box
SimpleCombo BoxDropdownList Box
Trang 42Items Collection
• List of items in a ListBox or ComboBox is a
collection
• Collections are objects that have properties
and methods that allow you to
– Add items
– Remove items
– Refer to individual items
– Count items
Trang 43Index Property
• Zero based value used to reference
individual items in the collection
• Position of an item in the list
Trang 44SelectedIndex Property
• Use to identify currently selected item in the
list
• If no item is selected ListIndex = -1
• Use to select an item in the list
Trang 45Ví dụ List và Combo
Trang 46Code cho List và Combo
private void frmListCoBan_Load(object sender, EventArgs e)
Trang 47Code cho List và Combo
private void btnThem_Click(object sender, EventArgs e)
Trang 48Code cho List và Combo
private void btnXoa_Click(object sender,
EventArgs e)
{
if (lstDSSV.SelectedIndex != -1)
lstDSSV.Items.RemoveAt(lstDSSV.SelectedIn dex);
Trang 49Code cho List và Combo
private void btnXoaHet_Click(object sender,
EventArgs e)
{ lstDSSV.Items.Clear();
}
Trang 50Tạo Động Trong Lúc RunTime
Trang 51Tạo Động Button RunTime
for (int i = 1; i <= n; i++)
{
Button btn = new Button();
btn.Text = rnd.Next(2, 102).ToString();
Trang 52Tạo Động Event Cho Button
private void cacbtn_Click (object sender, EventArgs e)