Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I."— Presentation transcript:

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

2 Contents Introduction Primitive Data Type if statement while statement Operators

3 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

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

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

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

7 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”

8 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

9 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

10 Primitive Data Type

11 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**/

12 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”);

13 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

14 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

15 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”);

16 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”);

17 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

18 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.

19 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

20 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 */

21 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 */

22 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

23 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.

24 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

25 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 */

26 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

27 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;

28 Operators Compound Assignment Operator 28

29 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

30 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 5 11 11 System.out.println( c++ ); // print 5 then postincrement 12 System.out.println( c ); // print 6 13 14 System.out.println(); // skip a line 15 16 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} 556 566556 566

31 www.themegallery.com


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

Similar presentations


Ads by Google