instructor: shih-shinh huang windows programming using java chapter4: control statements part i

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

Upload: herbert-barrett

Post on 18-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

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.

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

Type Size in bits

Values Standard

boolean 8 true or false

char 16 ’\u0000’ to ’\uFFFF’ (ISO Unicode character set)

byte 8 –128 to +127

short 16 –32,768 to +32,767

int 32 –2,147,483,648 to +2,147,483,647

long 64 –9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

float 32 –3.40292347E+38 to +3.40292347E+38

(IEEE 754 floating point)

double 64

–1.79769313486231570E+308 to +1.79769313486231570E+308

(IEEE 754 floating point)

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 ({)

if( expression){

statement1 ;

statement2;

}/* End of if-condition**/

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 casesif( expression)

statement1 ;

else if(expression)

statement2;

else

statement 3

14

if Statement

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”

false true

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

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.

19

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

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:100Enter Grade:50Enter Grade:50Enter Grade:100Enter Grade:50

Total of all grades is: 350Class 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:100Enter Grade:50Enter Grade:-1

Total of all grades is: 150Class average is: 75

Welcome to the grade book for Java!Enter Grade:100Enter Grade:50Enter Grade:30Enter Grade:70Enter Grade:60Enter Grade:-1

Total of all grades is: 310Class 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;

variable = variable operator expression;

variable operator = expression;

28

Operators

Compound Assignment Operator

Assignment operator

Sample expression

Explanation Assigns

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;

+= c += 7 c = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g

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

556 566

www.themegallery.com