Windows Programming Using Java Chapter 4 Control Statements





















![while Statement public class Grade. Book. Test { public static void main(String args[]){ Grade. while Statement public class Grade. Book. Test { public static void main(String args[]){ Grade.](https://slidetodoc.com/presentation_image_h2/9e02f5df335a014ef680852cb0f56566/image-22.jpg)









- Slides: 31

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

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 Think the problem 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 software system. true Grade >= 60 print “Passed” false 5

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

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. gets false � all other types are 0 � boolean

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 if( expression){ statement 1 ; statement 2; }/* End of if-condition**/ entry/exit point Structures with many lines of code need braces ({) 11

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

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 if( expression) statement 1 ; else if(expression) statement 2; else statement 3 13

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

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) System. out. printf(grade>=0 ? “Passed”: “Failed”); 16

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. Endless Loop int product = 3; int a = 3; Int b = 1; while(product <=100) product = 3* product; while(a <=100) b = 3* b; 17

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

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

while Statement import java. util. Scanner; public class Grade. Book { ……… public void Determine. Average(){ ……… Scanner input = new Scanner(System. in); while(count < 5){ System. out. print("Enter Grade: "); grade = input. next. Int(); total += grade; count ++; }/* End of while-loop */ average = total / count; System. out. printf("n. Total of all grades is: %dn", total); System. out. printf("Class average is: %d", average); }/* End of Determine. Average */ }/* End of Grade. Book */
![while Statement public class Grade Book Test public static void mainString args Grade while Statement public class Grade. Book. Test { public static void main(String args[]){ Grade.](https://slidetodoc.com/presentation_image_h2/9e02f5df335a014ef680852cb0f56566/image-22.jpg)
while Statement public class Grade. Book. Test { public static void main(String args[]){ Grade. Book java. Grade. Book = new Grade. Book("Java"); java. Grade. Book. Display. Message(); java. Grade. Book. Determine. Average(); }/* End of main */ }/* End of Grade. Book. Test */ Welcome to the grade book for Java! 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 Determine. Average(){ 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. next. Int(); while(grade != -1){ total += grade; count ++; System. out. print("Enter Grade: "); grade = input. next. Int(); }/* End of while-loop */ average = total / count; System. out. printf("n. Total of all grades is: %dn", total); System. out. printf("Class average is: %d", average); }/* End of Determine. Average */

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. variable = variable operator expression; variable operator = expression; Example �c = c+3; c += 3; �c = c*5; c *= 5; 27

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 4 public 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 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} 5 5 6 6

www. themegallery. com