Lecture Java: Chapter 5
Số trang: 115
Loại file: pptx
Dung lượng: 951.55 KB
Lượt xem: 19
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Java: Chapter 5 focuses on boolean expressions, the if and if-else statements, comparing data, while loops, iterators, more drawing techniques, more GUI components.
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 5 Chapter 5Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.Conditionals and Loops• Now we will examine programming statements that allow us to: – make decisions – repeat processing steps in a loop• Chapter 5 focuses on: – boolean expressions – the if and if-else statements – comparing data – while loops – iterators – more drawing techniques – more GUI components Copyright © 2012 Pearson Education, Inc.Outline Boolean Expressions The if Statement Comparing Data The while Statement Iterators The ArrayList Class Determining Event Sources Check Boxes and Radio Buttons Copyright © 2012 Pearson Education, Inc.Flow of Control• Unless specified otherwise, the order of statement execution through a method is linear: one after another• Some programming statements allow us to make decisions and perform repetitions• These decisions are based on boolean expressions (also called conditions) that evaluate to true or false• The order of statement execution is called the flow of control Copyright © 2012 Pearson Education, Inc.Conditional Statements• A conditional statement lets us choose which statement will be executed next• They are sometimes called selection statements• Conditional statements give us the power to make basic decisions• The Java conditional statements are the: – if and if-else statement – switch statement• Well explore the switch statement in Chapter 6 Copyright © 2012 Pearson Education, Inc.Boolean Expressions• A condition often uses one of Javas equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than = greater than or equal to• Note the difference between the equality operator (==) and the assignment operator (=) Copyright © 2012 Pearson Education, Inc.Boolean Expressions• An if statement with its boolean condition: if (sum > MAX) delta = sum – MAX;• First, the condition is evaluated: the value of sum is either greater than the value of MAX, or it is not• If the condition is true, the assignment statement is executed; if it isnt, it is skipped• See Age.java Copyright © 2012 Pearson Education, Inc.//********************************************************************// Age.java Author: Lewis/Loftus//// Demonstrates the use of an if statement.//********************************************************************import java.util.Scanner;public class Age{ //----------------------------------------------------------------- // Reads the users age and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { final int MINOR = 21; Scanner scan = new Scanner (System.in); System.out.print (Enter your age: ); int age = scan.nextInt();continue Copyright © 2012 Pearson Education, Inc.continue System.out.println (You entered: + age); if (age < MINOR) System.out.println (Youth is a wonderful thing. Enjoy.); System.out.println (Age is a state of mind.); }} Copyright © 2012 Pearson Education, Inc. Sample Run Enter your age: 47 You entered: 47continue Age is a state of mind. System.out.println (You entered: + age); if (age < MINOR) System.out.println (Youth is a wonderful thing. Enjoy.); System.out.println (Age is a state of mind.); }} Another Sample Run Enter your age: 12 You entered: 12 Youth is a wonderful thing. Enjoy. Age is a state of mind. Copyright © 2012 Pearson Education, Inc.Logical Operators• Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR• They all take boolean operands and produce boolean results• Logical NOT is a unary operato ...
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 5 Chapter 5Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.Conditionals and Loops• Now we will examine programming statements that allow us to: – make decisions – repeat processing steps in a loop• Chapter 5 focuses on: – boolean expressions – the if and if-else statements – comparing data – while loops – iterators – more drawing techniques – more GUI components Copyright © 2012 Pearson Education, Inc.Outline Boolean Expressions The if Statement Comparing Data The while Statement Iterators The ArrayList Class Determining Event Sources Check Boxes and Radio Buttons Copyright © 2012 Pearson Education, Inc.Flow of Control• Unless specified otherwise, the order of statement execution through a method is linear: one after another• Some programming statements allow us to make decisions and perform repetitions• These decisions are based on boolean expressions (also called conditions) that evaluate to true or false• The order of statement execution is called the flow of control Copyright © 2012 Pearson Education, Inc.Conditional Statements• A conditional statement lets us choose which statement will be executed next• They are sometimes called selection statements• Conditional statements give us the power to make basic decisions• The Java conditional statements are the: – if and if-else statement – switch statement• Well explore the switch statement in Chapter 6 Copyright © 2012 Pearson Education, Inc.Boolean Expressions• A condition often uses one of Javas equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than = greater than or equal to• Note the difference between the equality operator (==) and the assignment operator (=) Copyright © 2012 Pearson Education, Inc.Boolean Expressions• An if statement with its boolean condition: if (sum > MAX) delta = sum – MAX;• First, the condition is evaluated: the value of sum is either greater than the value of MAX, or it is not• If the condition is true, the assignment statement is executed; if it isnt, it is skipped• See Age.java Copyright © 2012 Pearson Education, Inc.//********************************************************************// Age.java Author: Lewis/Loftus//// Demonstrates the use of an if statement.//********************************************************************import java.util.Scanner;public class Age{ //----------------------------------------------------------------- // Reads the users age and prints comments accordingly. //----------------------------------------------------------------- public static void main (String[] args) { final int MINOR = 21; Scanner scan = new Scanner (System.in); System.out.print (Enter your age: ); int age = scan.nextInt();continue Copyright © 2012 Pearson Education, Inc.continue System.out.println (You entered: + age); if (age < MINOR) System.out.println (Youth is a wonderful thing. Enjoy.); System.out.println (Age is a state of mind.); }} Copyright © 2012 Pearson Education, Inc. Sample Run Enter your age: 47 You entered: 47continue Age is a state of mind. System.out.println (You entered: + age); if (age < MINOR) System.out.println (Youth is a wonderful thing. Enjoy.); System.out.println (Age is a state of mind.); }} Another Sample Run Enter your age: 12 You entered: 12 Youth is a wonderful thing. Enjoy. Age is a state of mind. Copyright © 2012 Pearson Education, Inc.Logical Operators• Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR• They all take boolean operands and produce boolean results• Logical NOT is a unary operato ...
Tìm kiếm theo từ khóa liên quan:
Kỹ thuật lập trình Lecture Java Chapter 5 Lập trình Java Lập trình Mobile Ngôn ngữ lập trình Công nghệ thông tinGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 312 0 0 -
74 trang 295 0 0
-
96 trang 291 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 289 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 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 274 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 272 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 269 1 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 264 0 0