1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 5 Decisions,List, Conditions And Loops

52 313 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Decisions, List, Conditions And Loops
Thể loại Chương
Năm xuất bản 2009
Định dạng
Số trang 52
Dung lượng 387,63 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Chapter 5 Decisions, List, Conditions And Loops

Programming In

C Shap

Trang 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 }

Trang 4

if else Double-Selection Statement

print “R ớt” false

Flowchart the If else Stucture – Lượt đồ câu điều kiện if else

Trang 5

if else Double-Selection Statement

Trang 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]

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 9

radSenior.Checked = True;

Trang 11

ToUpper and ToLower Methods

• Use ToUpper and ToLower methods of the

String class to convert strings for

Trang 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

Trang 13

radMale not checked, txtAge less than 21 False

radMale checked, txtAge 21 or greater False

radMale checked, txtAge less than 21 True

Trang 14

radJunior.Value=False, radSenior.Value=True True

radJunior.Value=True, radSenior.Value=False True

radJunior.Value=False, radSenior.Value=False False

Trang 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

Trang 17

switch Multiple-Selection Statement

If expression< >values in any

of the preceding constant lists

Trang 18

Flocharting Switch Multiple Selection Structure

Case a: Actions a break;

Case b: Actions b break;

Case n: Action n break;

Trang 19

SwitchCase - Numeric Value

Trang 20

Switch Case - String Value Example

Trang 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

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 23

Debugging

• 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 24

Debugging (cont.)

• Output Window

• Locals Window

• Autos Window

Trang 25

Debug Menu and Toolbar

Trang 26

Toggle Breakpoints On/Off by clicking

Trang 27

Viewing Current Values

During Program Execution

Place mouse pointer over variable or property to view current value

Trang 28

Locals Window

Trang 29

Autos Window

Trang 30

Cấ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 31

Cấ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 32

Ví 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 33

Ví Dụ Cấu trúc lặp While

Richtexbox (rtxt)

Trang 34

Cấ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 36

Cấ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 37

Ví 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 38

Lệ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 39

ListBoxes & 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 40

Types of List Controls

– List may allow for user to add new items

– List may "drop down" to display items in list

Trang 41

List Controls Visually

List

Boxes

DropdownCombo Box

SimpleCombo BoxDropdownList Box

Trang 42

Items 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 43

Index Property

• Zero based value used to reference

individual items in the collection

• Position of an item in the list

Trang 44

SelectedIndex 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 45

Ví dụ List và Combo

Trang 46

Code cho List và Combo

private void frmListCoBan_Load(object sender, EventArgs e)

Trang 47

Code cho List và Combo

private void btnThem_Click(object sender, EventArgs e)

Trang 48

Code cho List và Combo

private void btnXoa_Click(object sender,

EventArgs e)

{

if (lstDSSV.SelectedIndex != -1)

lstDSSV.Items.RemoveAt(lstDSSV.SelectedIn dex);

Trang 49

Code cho List và Combo

private void btnXoaHet_Click(object sender,

EventArgs e)

{ lstDSSV.Items.Clear();

}

Trang 50

Tạo Động Trong Lúc RunTime

Trang 51

Tạo Động Button RunTime

for (int i = 1; i <= n; i++)

{

Button btn = new Button();

btn.Text = rnd.Next(2, 102).ToString();

Trang 52

Tạo Động Event Cho Button

private void cacbtn_Click (object sender, EventArgs e)

Ngày đăng: 13/05/2014, 11:31

TỪ KHÓA LIÊN QUAN

w