INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II.

Slides:



Advertisements
Similar presentations
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Advertisements

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Chapter 05 (Part III) Control Statements: Part II.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 – C Program Control
Control Statements: Part 2
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 5: Control Structures II
JavaScript: Control Statements (II)
MSIS 655 Advanced Business Applications Programming
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 2
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 6 Control Statements: Part 2
Chapter 8 JavaScript: Control Statements, Part 2
Control Statements:.
Presentation transcript:

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II

Contents Introduction for Repetition Statement d0-while Repetition Statement switch Multiple-Selection Statement break/continue Statement Logical Operator Triangle Example

Introduction Essentials of Counter-Controlled Repetition  Control Variable  Initial Value for Control Variable  Increment  Loop-Continuation Condition

for Repetition Statement Syntax  Expression 1: Initialization  Expression 2: Loop-continuation Condition  Expression 3: Incrementing/Decrementing 4 for(expression1; expression2; expression3) statement; for(expression1; expression2; expression3){ statement1; statement2; }/* End of for-loop */

for Repetition Statement Syntax Example for (int counter = 1; counter <= 5; counter++ ) Initial value of control variableIncrement of control variable Control variablenameFinal value of control variable keyword Loop-continuation condition counter++ Establish initial value of control variable. Determine if final value of control variable has been reached. counter <= 5 statements true false int counter = 1 Body of loop (this may be multiple statements) Increment the control variable.

for Repetition Statement Remarks  The variable scope of the control variables are only in the for statement.  If the loop-continuation condition is initially false, it does not execute the for loop. count=0; for(int count=0; count < 10; count++){ ……… }/* End of for-loop */

for Repetition Statement Remarks  All expressions in for statement is optional.  If the loop-continuation condition is omitted, Java creates an infinite loop.  You can omit increment expression, if the program calculates the increment in the loop’s body. for(; ;){ ……… }/* End of for-loop */ Infinite Loop for(int i=0;i<10 ;){ i=i+5; }/* End of for-loop */

for Repetition Statement Examples  for(int i=1; i <=100; i++)  for(int i=100; i>1; i--)  for(int i=7; i <=77; i+=7)  for(int i=20; i >= 2; i-=2) Vary the control variable from 1 to 100 in increments of 1. Vary the control variable from 100 to 1 in decrements of 1 Vary the control variable from 7 to 77 in increments of 7 Vary the control variable from 20 to 2 in decrements of 2

d0-while Repetition Statement Description  It tests the loop-continuation condition after executing the body.  The body always executes at least once. true false action(s) condition Initialization; do{ Statement; Increment; }while(Loop-continuation Condition);

d0-while Repetition Statement count=0; do{ System.out.printf(“%d “, counter); count++; }while(count <=5); /* End of do-while loop */

switch Multiple-Selection Statement Syntax switch(constant expression){ case 1: break; case 2: break; …… default: break; }/* End of switch */

switch Multiple-Selection Statement Description  It is the statement to perform different actions based on the constant expression values.  Each action is associated with the value  Constant Integral Expression  Constant String Expression.  It contains a sequence of case labels and an optional default. 12

switch Multiple-Selection Statement Description  The break statement is used to exit the switch statement. switch(constant expression){ case 1: break; case 2: break; …… }/* End of switch */ switch(constant expression){ case 1: case 2: break; …… }/* End of switch */

switch Multiple-Selection Statement public void ShowGradeLetter(int grade){ switch(grade/10){ case 10: case 9: System.out.println(“Rank: A”); break; case 8: System.out.println(“Rank: B”); break; case 7: System.out.println(“Rank: C”); break; default: System.out.println(“Rank: D”); break; }/* End of switch */ }/* End of ShowGradeLetter */

break/continue Statement break Statement  The break statement is used in a while, for, do…while, and switch.  It causes immediate exit from the statement.  Execution continues with the first statement after the control statement. for (int i = 1; i <= 10; i++) { if (i == 5) break; System.out.printf(“%d\n", i); }/* End of if-condition */ System.out.println();

break/continue Statement continue Statement  The break statement is used in a while, for, do…while, and switch.  It skips the remaining statements in the loop.  It proceeds with the next iteration of loop. for (int i = 1; i <= 10; i++) { if (i == 5) continue; System.out.printf(“%d\n", i); }/* End of if-condition */ System.out.println();

Logical Operator Description  It enables you to form more complex conditions. Operators  Conditional AND (&&)  Conditional OR (||)  Logical NOT (!)  Boolean Logical exclusive OR (^) 17 (grade >= 70 && grade <= 90)(grade >= 70 || grade <= 60)

Logical Operator && : Condition AND || : Condition OR 18 expression1expression2 expression1 && expression2 false truefalse truefalse true expression1expression2 expression1 || expression2 false true falsetrue

Logical Operator ^ : Exclusive OR  True Conditions: One of its operand is true and the other is false. ! : Negation 19 expression1expression2 expression1 ^ expression2 false true falsetrue false expression ! expression falsetrue Truefalse

Logical Operator Short-Circuit Evaluation  The parts of an expression are evaluated only until it’s condition value is known.  It will have different result if the expression has side effect. 20 (gender ==female) && (age >=65) (gender ==female) && (++age >=65) (gender ==female) || (age >=65) (gender ==female) || (++age >=65)

Logical Operator Short-Circuit Evaluation (gender ==female) && (++age >=65) genderage beforeage after male60 female6061 (gender ==female) || (++age >=65) genderage beforeage after male6061 female60

Triangle Example Requirements  Design a class that can display triangle patterns.  The layer of triangle and character in the pattern can be specified by the user. Layer = 5 Character = ‘*’ Layer = 4 Character = ‘a’

Triangle Example Design  Fields  int m_iLayer: the layer of the triangle  char m_chTriChar: the char used for triangle pattern.  Methods  Triangle(int, int): constructor  ShowTriangle(void): the function to draw triangle.

Triangle Example public class Triangle { private int m_iLayer; private char m_chTriChar; /* constructor */ public Triangle(int layer, char ch){ m_iLayer = layer; m_chTriChar = ch; }/* End of constructor */ /* show the triangle pattern */ public void ShowTriangle(){ ………… }/* ENd of ShowTriangle */ }/* End of Triangle */

Triangle Example public class Triangle { private int m_iLayer; private char m_chTriChar; public Triangle(int layer, char ch){ …… }/* End of constructor */ public void ShowTriangle(){ for(int i=0; i < m_iLayer; i++){ for(int j=0; j <= i; j++){ System.out.printf("%c",m_chTriChar); }/* End of for-loop (j) */ System.out.printf("\n"); }/* End of for-loop (i) */ }/* ENd of ShowTriangle */ }/* End of Triangle */

Triangle Example public class TriangleTest { public static void main(String args[]){ Triangle triangle1 = new Triangle(6, ‘b'); triangle1.ShowTriangle(); Triangle triangle2 = new Triangle(10, ‘#’); triangle2.ShowTriangle(); }/* End of main */ }/* End of TriangleTest */ a aa aaa aaaa aaaaa aaaaaa # ## ### #### ##### ###### ####### ######## ######### ##########