Chapter 5 Decisions,List, Conditions And Loops

52 312 0
Chapter 5 Decisions,List, Conditions And Loops

Đ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

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 ) { . . . } [...]...ToUpper and ToLower Methods • Use ToUpper and ToLower methods of the String class to convert strings for comparisons txtGender.Text contains Male If (txtGender.Text.ToUpper = "MALE" ) { } 4- 11 © 2009 Compound Conditions • Join conditions using Logical Operators – Or (||) either true evaluates to true – And (&&) both true evaluates to true – Not (!) reverses... false and false will evaluate true 4- 12 © 2009 Compound Conditions Example # 1 if (radMale.Checked == True) && (CInt(txtAge.Text) < 21 ) { mintMinorMaleCount + = mintMinorMaleCount } radMale not checked txtAge greater than 21 radMale not checked, txtAge less than 21 radMale checked, txtAge 21 or greater radMale checked, txtAge less than 21 4- 13 False False False False True © 2009 Compound Conditions. .. (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false) e.Handled = true;// ép không nhận ký tự gõ vào } 4- 22 © 2009 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 4- 23 © 2009 Debugging (cont.) • Output Window... Over • View the values of properties, variables, mathematical expressions, and conditions 4- 23 © 2009 Debugging (cont.) • Output Window • Locals Window • Autos Window 4- 24 © 2009 Debug Menu and Toolbar 4- 25 © 2009 Breakpoints Toggle Breakpoints On/Off by clicking in Editor's gray left margin indicator 4- 26 © 2009 Viewing Current Values During Program Execution Place mouse pointer over variable... radSenior.Value=False 4- 14 True True True True False © 2009 Nested Ifs if (intTemp > 32 ) if (intTemp > 80 ) lblComment.Text = "Hot“; Else lblComment.Text="Moderate“; Else lblComment.Text="Freezing“; 4- 15 © 2009 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... Convert.ToInt32(txtN.Text); int i = 1; string strList = ""; long lngTong = 0; do { lngTong += i; strList += i.ToString() + "+ "; i++; } while (i

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

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan