Increment and Decrement OperatorsUnary increment operator (++)
Increment variable’s value by 1
Unary decrement operator (--)
Decrement variable’s value by 1
Preincrement / predecrement operator
Post-increment / post-decrement operator
Chapter 4 and 5 - Control Structures
OutlineChapter 4
Introduction Control Structures if Single-Selection Statement if else Selection Statement while Repetition Statement Assignment Operators Increment and Decrement Operators Primitive Types
Chapter 5
for Repetition Statement Examples Using the for Statement do…while Repetition Statement switch Multiple-Selection Statement break and continue Statements Logical Operators
Introduction
We learn about Control Structures
Control structures
Control Structures
Java has a sequence structure “built-in”
Java provides three selection structures
if
If…else
switch
Java provides three repetition structures
while
do…while
do
Each of these words is a Java keyword
Average1.java gradeCounter Line 21 1 // Fig. 4.7: Average1.java
2 // Class-average program with counter-controlled repetition.
3 import javax.swing.JOptionPane;
4
5 public class Average1 {
6
7 public static void main( String args[] )
8 {
9 int total; // sum of grades input by user
10 int gradeCounter; // number of grade to be entered next
11 int grade; // grade value
12 int average; // average of grades
13
14 String gradeString; // grade typed by user
15
16 // initialization phase
17 total = 0; // initialize total
18 gradeCounter = 1; // initialize loop counter
19
20 // processing phase
21 while ( gradeCounter <= 10 ) { // loop 10 times
22
23 // prompt for input and read grade from user
24 gradeString = JOptionPane.showInputDialog(
25 "Enter integer grade: " );
26
27 // conve
Declare variables; gradeCounter is the counter Continue looping as long as gradeCounter is less than or equal to 10
Average1.java 30 total = total + grade; // add grade to total
31 gradeCounter = gradeCounter + 1; // increment counter
32
33 } // end while
34
35 // termination phase
36 average = total / 10; // integer division
37
38 // display average of exam grades
39 JOptionPane.showMessageDialog( null, "Class average is " + average,
40 "Class Average", JOptionPane.INFORMATION_MESSAGE );
41
42 System.exit( 0 ); // terminate the program
43
44 } // end main
45
46 } // end class Average1
Average1.java
Average2.java 1 // Fig. 4.9: Average2.java
2 // Class-average program with sentinel-controlled repetition.
3 import java.text.DecimalFormat; // class to format numbers
4 import javax.swing.JOptionPane;
5
6 public class Average2 {
7
8 public static void main( String args[] )
9 {
10 int total; // sum of grades
11 int gradeCounter; // number of grades entered
12 int grade; // grade value
13
14 double average; // number with decimal point for average
15
16 String gradeString; // grade typed by user
17
18 // initialization phase
19 total = 0; // initialize total
20 gradeCounter = 0; // initialize loop counter
21
22 // processing phase
23 // get first grade from user
24 gradeString = JOptionPane.showInputDialog(
25 "Enter Integer Grade or -1 to Quit:" );
26
Average2.javaLine 31Line 45 30 // loop until sentinel value read from user
31 while ( grade != -1 ) {
32 total = total + grade; // add grade to total
33 gradeCounter = gradeCounter + 1; // increment counter
34
35 // get next grade from user
36 gradeString = JOptionPane.showInputDialog(
37 "Enter Integer Grade or -1 to Quit:" );
38
39 // convert gradeString to int
40 grade = Integer.parseInt( gradeString );
41
42 } // end while
43
44 // termination phase
45 DecimalFormat twoDigits = new DecimalFormat( "0.00" );
46
47 // if user entered at least one grade...
48 if ( gradeCounter != 0 ) {
49
50 // calculate average of all grades entered
51 average = (double) total / gradeCounter;
52
53
loop until gradeCounter equals sentinel value (-1) Format numbers to nearest hundredth
Average2.java 60 else // if no grades entered, output appropriate message
61 JOptionPane.showMessageDialog( null, "No grades were entered",
62 "Class Average", JOptionPane.INFORMATION_MESSAGE );
63
64 System.exit( 0 ); // terminate application
65
66 } // end main
67
68 } // end class Average2
Analysis.javaLine 19Line 29 1 // Fig. 4.11: Analysis.java
2 // Analysis of examination results.
3 import javax.swing.JOptionPane;
4
5 public class Analysis {
6
7 public static void main( String args[] )
8 {
9 // initializing variables in declarations
10 int passes = 0; // number of passes
11 int failures = 0; // number of failures
12 int studentCounter = 1; // student counter
13 int result; // one exam result
14
15 String input; // user-entered value
16 String output; // output string
17
18 // process 10 students using counter-controlled loop
19 while ( studentCounter <= 10 ) {
20
21 // prompt user for input and obtain value from user
22 input = JOptionPane.showInputDialog(
23 "Enter result (1 = pass, 2 = fail)" );
24
25 // convert
Loop until student counter is greater than 10 Nested control structure
Analysis.java 31
32 else // if result not 1, increment failures
33 failures = failures + 1;
34
35 // increment studentCounter so loop eventually terminates
36 studentCounter = studentCounter + 1;
37
38 } // end while
39
40 // termination phase; prepare and display results
41 output = "Passed: " + passes + "\nFailed: " + failures;
42
43 // determine whether more than 8 students passed
44 if ( passes > 8 )
45 output = output + "\nRaise Tuition";
46
47 JOptionPane.showMessageDialog( null, output,
48 "Analysis of Examination Results",
49 JOptionPane.INFORMATION_MESSAGE );
50
51 System.exit( 0 ); // terminate application
52
53 } // end main
54
55 } // end class Analysis
Assignment Operators
Assignment Operators
c = c + 3
can be written as
c += 3
Increment and Decrement Operators
Unary increment operator (++)
Increment variable’s value by 1
Unary decrement operator (--)
Decrement variable’s value by 1
Preincrement / predecrement operator
Post-increment / post-decrement operator
Increment.javaLine 13 postincrementLine 21 preincrement 1 // Fig. 4.14: Increment.java
2 // Preincrementing and postincrementing operators.
3
4 public class Increment {
5
6 public static void main( String args[] )
7 {
8 int c;
9
10 // demonstrate postincrement
11 c = 5; // assign 5 to c
12 System.out.println( c ); // print 5
13 System.out.println( c++ ); // print 5 then postincrement
14 System.out.println( c ); // print 6
15
16 System.out.println(); // skip a line
17
18 // demonstrate preincrement
19 c = 5; // assign 5 to c
20 System.out.println( c ); // print 5
21 System.out.println( ++c ); // preincrement then print 6
22 System.out.println( c ); // print 6
23
24 } // end main
25
26 } // end class Increment
556 566 Line 13 postincrements c Line 21 preincrements c
Primitive Types
Primitive types
“building blocks” for more complicated types
Java is strongly typed
All variables in a Java program must have a type
WhileCounter.javaLine 14Line 16Line 18 1 // Fig. 5.1: WhileCounter.java
2 // Counter-controlled repetition.
3 import java.awt.Graphics;
4
5 import javax.swing.JApplet;
6
7 public class WhileCounter extends JApplet {
8
9 // draw lines on applet’s background
10 public void paint( Graphics g )
11 {
12 super.paint( g ); // call paint method inherited from JApplet
13
14 int counter = 1; // initialization
15
16 while ( counter <= 10 ) { // repetition condition
17 g.drawLine( 10, 10, 250, counter * 10 );
18 ++counter; // increment
19
20 } // end while
21
22 } // end method paint
23
24 } // end class WhileCounter
Increment for counter Condition tests for counter’s final value Control-variable name is counter
Control-variable initial value is 1
ForCounter.javaLine 16int counter = 1;Line 16 counter <= 10;Line 16 counter++; 1 // Fig. 5.2: ForCounter.java
2 // Counter-controlled repetition with the for statement.
3 import java.awt.Graphics;
4
5 import javax.swing.JApplet;
6
7 public class ForCounter extends JApplet {
8
9 // draw lines on applet’s background
10 public void paint( Graphics g )
11 {
12 super.paint( g ); // call paint method inherited from JApplet
13
14 // for statement header includes initialization,
15 // repetition condition and increment
16 for ( int counter = 1; counter <= 10; counter++ )
17 g.drawLine( 10, 10, 250, counter * 10 );
18
19 } // end method paint
20
21 } // end class ForCounter
Condition tests for counter’s final value Control-variable name is counter
Control-variable initial value is 1 Increment for counter
Comments