1 Introduction to Java Programming Lecture 7 Flow Control : Boolean expressions and the if statement.

Slides:



Advertisements
Similar presentations
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
Advertisements

Chapter 10 馬可夫鏈 緒言 如果讀者仔細觀察日常生活中所發生的 諸多事件,必然會發現有些事件的未來 發展或演變與該事件現階段的狀況全然 無關,這種事件稱為獨立試行過程 (process of independent trials) ;而另一些 事件則會受到該事件現階段的狀況影響。
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
Introduction to Java Programming Lecture 13 Classes I OO Programming.
建立使用案例敘述 --Use Case Narrative
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
Instructor: Ching-Chi Lin 林清池 助理教授
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
T-SQL 運算子介紹 11/29. 運算子的總類 指定運算子 算術運算子 比較運算子 邏輯運算子 位元運算子 字串連結運算子 單一運算元運算子.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
五小專案 黃詩晴 章乃云. 目錄 計算機 智慧盤 拼圖 記憶大挑戰 數學題庫 心得 參考文獻.
亂數產生器安全性評估 之統計測試 SEC HW7 姓名:翁玉芬 學號:
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
例外處理. 內 容 大 綱 例外的產生 捕捉例外 丟出例外 例外的產生 (1/4) 一般常見的執行時期錯誤包括整數除法分母為 0(divide by zero) ,陣列索引值越限 (array index out of range) ,輸入輸出錯誤 (I/O error) 、檔案 不存在或無法存取.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
MATLAB 程式設計 第 11 章 多維陣列 多維陣列的定義 在 MATLAB 的資料型態中,向量可視為 一維陣列,矩陣可視二維陣列,對於維 度 (Dimensions) 超過 1 的陣列則均可視 為「多維陣列」 (Multidimesional Arrays , 簡稱 N-D Arrays)
 Pearson Education, Inc. All rights reserved break and continue Statements Break and continue statements – Alter flow of control break.
JAVA 程式設計與資料結構 第十章 GUI Introdution III. File Chooser  File Chooser 是一個選擇檔案的圖形介面, 無論我們是要存檔還是要開啟檔案,使 用這個物件都會讓我們覺得容易且舒適。
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
Introduction to Java Programming Lecture 15 Objects and Classes.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出與繼續迴圈 6-5 跳出與繼續迴圈 6-6 錯誤處理 6-6 錯誤處理.
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
類別函式庫. Java Bean 一個單獨的 AWT 元件都是一個 bean. Java Bean 也有一個 bean 的 class 可繼承, 但是並不強 迫一定要繼承. Bean 的結構 Properties 屬性 : 描述 bean 的屬性 如顏色, 大小,label 等. 透過一對 methods.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
第 5 章 選擇控制項與條件敘述 5-1 結構化程式設計 5-1 結構化程式設計 5-2 關係與邏輯運算子 5-2 關係與邏輯運算子 5-3 簡單的條件敘述 5-3 簡單的條件敘述 5-4 選擇控制項 5-4 選擇控制項 5-5 巢狀條件敘述 5-5 巢狀條件敘述 5-6 多選一條件敘述 5-6 多選一條件敘述.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Management Abstracts Retrieval System; MARS 檢索操作.
: A-Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10930: A-Sequence 解題者:陳盈村 解題日期: 2008 年 5 月 30 日 題意: A-Sequence 需符合以下的條件, 1 ≤ a.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
第 9 章 TSR 程式基本教練. 本章提要 TSR 程式 以熱鍵 (Hot key) 叫用 TSR 程式 Clock 中斷 int 08h 、 int 1ch DOS reentrant 的問題 有用的 TSR 程式.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
Chapter 3 Entropy : An Additional Balance Equation
Introduction to Java Programming Lecture 9 Flow Control : while do-while and for loops II.
Linguistics phonetic symbols. 先下載 IPA 字型檔案,執行安裝。 由於這個程式的字型目錄設定錯誤, 所以等重新開機時就會發現字型消失。 所以必須根據以下步驟來讓 Windows 加入 IPA 字型。
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
計算機概論 第6章 數位邏輯設計.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
國立清華大學資訊工程學系 資訊工程系 2009/11/03P-1 Quiz & Solution 09810CS_ Computer Systems & Application Fall.
Introduction to Java Programming Lecture 8 Flow Control : while do-while and for loops.
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出迴圈 6-5 跳出迴圈 6-6 VB.NET 的錯誤處理 6-6 VB.NET.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/30 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH7.1~CH /12/26.
: Wine trading in Gergovia ★★☆☆☆ 題組: Contest Volumes with Online Judge 題號: 11054: Wine trading in Gergovia 解題者:劉洙愷 解題日期: 2008 年 2 月 29 日 題意:在 Gergovia.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
第 5 章 選擇控制項與條件敘述 5-1 結構化程式設計 5-1 結構化程式設計 5-2 關係與邏輯運算子 5-2 關係與邏輯運算子 5-3 簡單的條件敘述 5-3 簡單的條件敘述 5-4 選擇控制項 5-4 選擇控制項 5-5 巢狀條件敘述 5-5 巢狀條件敘述 5-6 多選一條件敘述 5-6 多選一條件敘述.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
7.4 程式範例 在螢幕上寫出“Hello” 的程式 (參考 code/pep-p200.odc).
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
數字系統與資料表示法 教師: 陳炯勳 數系轉換 r進制數字 稱為 base r或 radix r 有r個計數符號,計數順序逢r歸零(進位) A n A n - 1 ‥‥A 2 A 1 A 0 ﹒A -1 A -2 ‥‥A -m 其中A n 及A.
Microsoft Excel.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
VHDL語法(3).
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Presentation transcript:

1 Introduction to Java Programming Lecture 7 Flow Control : Boolean expressions and the if statement

2 Boolean expressions 布林運算式 (Boolean expressions) 由條件及邏輯的運算 所組成 條件運算式 < is less than > is greater than <= is less than or equal to >= is greater than or equal to == is equal to != is not equal to NOTE: == is not the same as = in Java! 條件運算式是二元運算,計算的結果是 true 或 false 的布林值 (boolean)

3 條件運算式 Examples: int x = 15; int y = 100; System.out.println(x < y); System.out.println(x <= y); System.out.println(x > y); System.out.println(x >= y); System.out.println(x == y); System.out.println(x != y); boolean z = (x - y < 0);

4 邏輯運算式 邏輯運算式 &&logical AND || logical OR ! NOT 邏輯運算式計算一個或數個布林值,計算的結果也是個布 林值 Examples: int x = 15; int y = 100; boolean a = (x > y); boolean b = (x != y); System.out.println(a && b); System.out.println(a || b); System.out.println(!a);

5 The if statement Syntax: if ( boolean_expression ) statement ; if ( boolean_expression ) { statement_list... } 如果 boolean_expression( 布林運算式 ) 的計算結果是 true , statement 就會被執行,否則就跳過、不執行

6 The if statement Example: int age = 20; if ( age > 18 ) System.out.println(" 超過 18 歲,請買全票 "); if ( age 12 ) { System.out.println(" 學生 "); System.out.println(" 可以買學生票 "); }

7 The if-else statement Syntax: if ( boolean_expression ) statement1 ; else statement2 ; 或 if ( boolean_expression ) { statement_list1 } else { statement_list2 } 如果 boolean_expression( 布林運算式 ) 的計算結果是 true , statement 1 就會被執行,否則就執行 statement 2

8 The if-else statement Example: String courseName = “Java Programming”; // 列印前 10 個字元. if (courseName.length() > 10 ) System.out.println(courseName.substring(0,10)); else System.out.println(courseName);

9 The if-else statement 下面的程式有什麼邏輯錯誤 if ( age <= 12 ) System.out.println(“ 孩童 "); else System.out.println(“ 不是孩童 "); System.out.println(“ 必須買全票 ");

10 The if - else if - else statement if ( boolean_expression1 ) { statement1 } else if ( boolean_expression2 ) { statement2 } else{ statement3 } 如果 boolean_expression1 的計算結果是 true , statement1 就會被執行,否則就檢查 boolean_expression2 ,結果是 true 就執行 statement2 , 否則就執行 statement 3 。 只有一個 statement 會被執行。

11 Java Provides a shortcut if/else operator: The conditional expression This is Java’s only ternary operator (i.e. it takes three operands) ( 唯一的三元運算子,可用來代替 if-else) y = ( x > 0 ) ? 1 : -1 The conditional Statement. True Option False Option ? short- cut operator colon

12 Conditional expression y = ( x > 0 ) ? 1 : -1 is equivalent to if (x > 0) y = 1; else y = -1; The syntax of conditional expression: (boolean_expression) ? exp1 : exp2

13 Conditional Operator if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); is equivalent to System.out.println( (num % 2 == 0)? num + “is even” : num + “is odd”);

14 switch statement 如果所要執行的指令是根據數個 int 或 char 的變數來定, 我們可以使用 switch 來代替一連串的 if - else if … else. Example: switch (year) { case 7 : annualInterestRate = 7.25; break; case 15 : annualInterestRate = 8.50; break; case 30 : annualInterestRate = 9.0; break; default : System.out.println( "Wrong number of years, enter 7, 15, or 30"); }