Lecture Java: Chapter 6
Số trang: 62
Loại file: pptx
Dung lượng: 828.94 KB
Lượt xem: 17
Lượt tải: 0
Xem trước 7 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Java: Chapter 6 focuses on the switch statement, the conditional operator, the do loop, the for loop, drawing with the aid of conditionals and loops, dialog boxes.
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 6 Chapter 6More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.More Conditionals and Loops• Now we can fill in some additional details regarding Java conditional and repetition statements• Chapter 6 focuses on: – the switch statement – the conditional operator – the do loop – the for loop – drawing with the aid of conditionals and loops – dialog boxes Copyright © 2012 Pearson Education, Inc.Outline The switch Statement The Conditional Operator The do Statement The for Statement Drawing with Loops and Conditionals Dialog Boxes Copyright © 2012 Pearson Education, Inc.The switch Statement• The switch statement provides another way to decide which statement to execute next• The switch statement evaluates an expression, then attempts to match the result to one of several possible cases• Each case contains a value and a list of statements• The flow of control transfers to statement associated with the first case value that matches Copyright © 2012 Pearson Education, Inc.The switch Statement• The general syntax of a switch statement is: switch switch ( expression ) and { case case value1 : are statement-list1 reserved case value2 : words statement-list2 case value3 : statement-list3 If expression case ... matches value2, control jumps } to here Copyright © 2012 Pearson Education, Inc.The switch Statement• Often a break statement is used as the last statement in each cases statement list• A break statement causes control to transfer to the end of the switch statement• If a break statement is not used, the flow of control will continue into the next case• Sometimes this may be appropriate, but often we want to execute only the statements associated with one case Copyright © 2012 Pearson Education, Inc.The switch Statement• An example of a switch statement: switch (option) { case A: aCount++; break; case B: bCount++; break; case C: cCount++; break; } Copyright © 2012 Pearson Education, Inc.The switch Statement• A switch statement can have an optional default case• The default case has no associated value and simply uses the reserved word default• If the default case is present, control will transfer to it if no other case value matches• If there is no default case, and no other value matches, control falls through to the statement after the switch Copyright © 2012 Pearson Education, Inc.The switch Statement• The type of a switch expression must be integers, characters, or enumerated types• As of Java 7, a switch can also be used with strings• You cannot use a switch with floating point values• The implicit boolean condition in a switch statement is equality• You cannot perform relational checks with a switch statement• See GradeReport.java Copyright © 2012 Pearson Education, Inc.//********************************************************************// GradeReport.java Author: Lewis/Loftus//// Demonstrates the use of a switch statement.//********************************************************************import java.util.Scanner;public class GradeReport{ //----------------------------------------------------------------- // Reads a grade from the user and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { int grade, category; Scanner scan = new Scanner (System.in); System.out.print (Enter a numeric grade (0 to 100): ); grade = scan.nextInt(); category = grade / 10; System.out.print (That grade is );continue Copyright © 2012 Pearson Education, Inc.continue switch (category) { case 10: System.out.println (a perfect score. Well done.); break; case 9: System.out.println (well above average. Excellent.); break; case 8: System.out.println (above average. Nice job.); break; case 7: System.out.println (average.); break; case 6: System.out.println (below average. You should see the); System.out.println (instructor to clarify the material + presented in class.); break; default: System.out.println (not passing.); } }} Copyright © 2012 Pearson Education, Inc.continue Sample Run switch (category) Enter a numeric grade (0 ...
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 6 Chapter 6More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.More Conditionals and Loops• Now we can fill in some additional details regarding Java conditional and repetition statements• Chapter 6 focuses on: – the switch statement – the conditional operator – the do loop – the for loop – drawing with the aid of conditionals and loops – dialog boxes Copyright © 2012 Pearson Education, Inc.Outline The switch Statement The Conditional Operator The do Statement The for Statement Drawing with Loops and Conditionals Dialog Boxes Copyright © 2012 Pearson Education, Inc.The switch Statement• The switch statement provides another way to decide which statement to execute next• The switch statement evaluates an expression, then attempts to match the result to one of several possible cases• Each case contains a value and a list of statements• The flow of control transfers to statement associated with the first case value that matches Copyright © 2012 Pearson Education, Inc.The switch Statement• The general syntax of a switch statement is: switch switch ( expression ) and { case case value1 : are statement-list1 reserved case value2 : words statement-list2 case value3 : statement-list3 If expression case ... matches value2, control jumps } to here Copyright © 2012 Pearson Education, Inc.The switch Statement• Often a break statement is used as the last statement in each cases statement list• A break statement causes control to transfer to the end of the switch statement• If a break statement is not used, the flow of control will continue into the next case• Sometimes this may be appropriate, but often we want to execute only the statements associated with one case Copyright © 2012 Pearson Education, Inc.The switch Statement• An example of a switch statement: switch (option) { case A: aCount++; break; case B: bCount++; break; case C: cCount++; break; } Copyright © 2012 Pearson Education, Inc.The switch Statement• A switch statement can have an optional default case• The default case has no associated value and simply uses the reserved word default• If the default case is present, control will transfer to it if no other case value matches• If there is no default case, and no other value matches, control falls through to the statement after the switch Copyright © 2012 Pearson Education, Inc.The switch Statement• The type of a switch expression must be integers, characters, or enumerated types• As of Java 7, a switch can also be used with strings• You cannot use a switch with floating point values• The implicit boolean condition in a switch statement is equality• You cannot perform relational checks with a switch statement• See GradeReport.java Copyright © 2012 Pearson Education, Inc.//********************************************************************// GradeReport.java Author: Lewis/Loftus//// Demonstrates the use of a switch statement.//********************************************************************import java.util.Scanner;public class GradeReport{ //----------------------------------------------------------------- // Reads a grade from the user and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { int grade, category; Scanner scan = new Scanner (System.in); System.out.print (Enter a numeric grade (0 to 100): ); grade = scan.nextInt(); category = grade / 10; System.out.print (That grade is );continue Copyright © 2012 Pearson Education, Inc.continue switch (category) { case 10: System.out.println (a perfect score. Well done.); break; case 9: System.out.println (well above average. Excellent.); break; case 8: System.out.println (above average. Nice job.); break; case 7: System.out.println (average.); break; case 6: System.out.println (below average. You should see the); System.out.println (instructor to clarify the material + presented in class.); break; default: System.out.println (not passing.); } }} Copyright © 2012 Pearson Education, Inc.continue Sample Run switch (category) Enter a numeric grade (0 ...
Tìm kiếm theo từ khóa liên quan:
Kỹ thuật lập trình Lecture Java Chapter 6 Lập trình Java Lập trình Mobile Ngôn ngữ lập trình Công nghệ thông tinTài liệu có liên quan:
-
52 trang 468 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 367 0 0 -
96 trang 334 0 0
-
74 trang 329 0 0
-
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 321 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 321 1 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 316 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 310 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 304 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 302 0 0