INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Lec3: Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
The University of Texas – Pan American
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
© 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 3 Control Statements.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1 Outline 4.1Introduction 4.2Algorithms 4.3Pseudocode 4.4Control.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved. 1.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Control Statements: Part1  if, if…else, switch 1.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 7 JavaScript: Control Statements, Part 1
Algorithm: procedure in terms of
Control Statements: Part 1
JavaScript: Control Statements I
Chapter 8 - JavaScript: Control Statements I
Chapter 4 – Control Structures Part 1
Chapter 4 Control Statements: Part I
Lecture 2: Logical Problems with Choices
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Structural Program Development: If, If-Else
Control Statements:.
Presentation transcript:

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I

Contents Introduction Primitive Data Type if statement while statement Operators

Introduction What is Algorithm  Any problem can be solved by executing a series of actions in a specific order.  A procedure for solving a problem is called an algorithm  actions to be executed  order in which these actions execute. 3

Introduction Before Writing a Program  Understand the problem  Think of an approach  Understand the building blocks  Use proven principals of structured programming 4

Introduction Flow Chart  It models the workflow of a portion of a portion of a software system. 5 print “Passed” Grade >= 60 true false

Introduction Flow Chart:  Rectangle: action  Circles: connectors  Arrow: execution flow.  Diamonds: decisions 6 print “Passed” Grade >= 60 true false

Introduction Pseudo Code  It is an informal language.  It is similar to everyday English.  It helps programmers develop algorithms without worrying about the language syntax. if grade is greater or equal to 60 print “Passed” else print “Failed”

Introduction Control Structure  Statements normally are executed one after the other in the order.  Transfer of control enables the programmer to change the execution order  Bohm and Jacopini showed that all programs could be written in three control structures.  Sequence Structure  Selection Structure: 1) if; 2) if-else; 3) switch  Repetition Structure: 1) while; 2) do-while; 3) for loop

Primitive Data Type Description  Java has many primitive data types  char, byte, short, int, long, float, double,  boolean  They are the building blocks for complicated types.  In Java, the primitive data types will be set to their default value.  boolean gets false  all other types are 0

Primitive Data Type

if Statement if Description  It enables a program making a selection.  The chooses is based on expression that evaluates to a bool type  True: perform an action  False: skip the action  Single entry/exit point  Structures with many lines of code need braces ({) 11 if( expression){ statement1 ; statement2; }/* End of if-condition**/

if Statement 12 print “Passed” Grade >= 60 true false if grade is greater or equal to 60 print “Passed” if(grade >= 60) System.out.println(“Passed”);

if Statement if/else Description  Alternate courses can be taken when the statement is false  There are two choices rather than one action  Nested structures can test many cases 13 if( expression) statement1 ; else if(expression) statement2; else statement 3

if Statement 14 if grade is greater or equal to 60 print “Passed” else print “Failed” if(grade >= 60) System.out.println(“Passed”); else System.out.println(“Failed”); Grade >= 60 print “Passed”print “Failed” falsetrue

if Statement if(grade >= 90) System.out.println(“A”); else if (grade >=80) System.out.println(“B”); else if (grade >=70) System.out.println(“C”); else if (grade >= 60) System.out.println(“D”); else System.out.println(“E”);

if Statement Conditional Operators(?)  It is a Java ternary operator (three operands)  Similar to an if/else structure  Syntax:  (boolean value ? if true : if false) 16 System.out.printf(grade>=0 ? “Passed”: “Failed”);

while Statement Description  It is a repetition structure  It continues while statement is true  It ends when statement is false  The code in the body of code must alter the conditional state. 17 int product = 3; while(product <=100) product = 3* product; int a = 3; Int b = 1; while(a <=100) b = 3* b; Endless Loop

while Statement Counter-Controlled Repetition  The number of repetitions is known before the loop begins executing.  This technique uses a variable called a counter to control the number of repetition times.  It is called definite repetition. Example: A class of 5 students took a quiz. The grades for this quiz are available. Determine the class average on the quiz.

while Statement Counter-Controlled Repetition  Pseudo Code 19 1.Set total to zero 2.Set count to zero 3.while count is less than or equal to 5 1.Prompt user to enter the next grade 2.Add the grade to total 3.Increase the count 4.Set the average to the total divided by count 5.Print the result

while Statement import java.util.Scanner; public class GradeBook { ……… public void DetermineAverage(){ int total = 0; int count = 0; int average = 0; int grade = 0; Scanner input = new Scanner(System.in); while(count < 5){ ………… }/* End of while-loop */ ……… }/* End of DetermineAverage */ }/* End of GradeBook */

while Statement import java.util.Scanner; public class GradeBook { ……… public void DetermineAverage(){ ……… Scanner input = new Scanner(System.in); while(count < 5){ System.out.print("Enter Grade:"); grade = input.nextInt(); total += grade; count ++; }/* End of while-loop */ average = total / count; System.out.printf("\nTotal of all grades is: %d\n", total); System.out.printf("Class average is: %d", average); }/* End of DetermineAverage */ }/* End of GradeBook */

while Statement public class GradeBookTest { public static void main(String args[]){ GradeBook javaGradeBook = new GradeBook("Java"); javaGradeBook.DisplayMessage(); javaGradeBook.DetermineAverage(); }/* End of main */ }/* End of GradeBookTest */ Welcome to the grade book for Java! Enter Grade:100 Enter Grade:50 Enter Grade:100 Enter Grade:50 Total of all grades is: 350 Class average is: 70

while Statement Sentinel-Controlled Repetition  It is used in case of that the number of repetition is unknown.  The solution is to use a special value called sentinel value to indicate the end of data entry.  It is called indefinite repetition. Example: Develop a class-averaging program that processes for an arbitrary number of students each time it is run.

while Statement Counter-Controlled Repetition  Pseudo Code 1.Set total to zero 2.Set count to zero 3.Input the first grade 4.While user has not yet entered he sentinel 1.Add the grade to total 2.Increase the count 3.Prompt user to enter the next grade 5.Set the average to the total divided by count 6.Print the result

while Statement public void DetermineAverage(){ int total = 0; int count = 0; int average = 0; int grade = 0; Scanner input = new Scanner(System.in); System.out.print("Enter Grade:"); grade = input.nextInt(); while(grade != -1){ total += grade; count ++; System.out.print("Enter Grade:"); grade = input.nextInt(); }/* End of while-loop */ average = total / count; System.out.printf("\nTotal of all grades is: %d\n", total); System.out.printf("Class average is: %d", average); }/* End of DetermineAverage */

while Statement Welcome to the grade book for Java! Enter Grade:100 Enter Grade:50 Enter Grade:-1 Total of all grades is: 150 Class average is: 75 Welcome to the grade book for Java! Enter Grade:100 Enter Grade:50 Enter Grade:30 Enter Grade:70 Enter Grade:60 Enter Grade:-1 Total of all grades is: 310 Class average is: 62

Operators Compound Assignment Operator  It is the abbreviation of the assignment expressions.  Example  c = c+3;  c += 3;  c = c*5;  c *= 5; 27 variable = variable operator expression; variable operator = expression;

Operators Compound Assignment Operator 28

Operators Increment ++ / Decrement –  ++: add one to the variable  -- : subtract one from the variable  x = x+1;  x++;  y = y – 1;  y--; Prefix vs. Postfix  x++ or x-- : perform an action first and then add to or subtract one from the value  ++x or --x: add to or subtract one from the value and then perform an action

Operators 1// Fig. 4.14: Increment.java 2// Preincrementing and postincrementing 3 4public class Increment { 5 public static void main( String args[] ) 6 { 7 int c; 8 9 c = 5; 10 System.out.println( c ); // print System.out.println( c++ ); // print 5 then postincrement 12 System.out.println( c ); // print System.out.println(); // skip a line c = 5; 17 System.out.println( c ); // print 5 18 System.out.println( ++c ); // preincrement then print 6 19 System.out.println( c ); // print 6 20 } 21}