0% found this document useful (0 votes)
84 views

Lesson 4

This document discusses decision making and control structures in Java programs. It covers if, if/else, switch statements and how to construct boolean expressions. Examples are provided to demonstrate different selection and repetition structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Lesson 4

This document discusses decision making and control structures in Java programs. It covers if, if/else, switch statements and how to construct boolean expressions. Examples are provided to demonstrate different selection and repetition structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lesson 4

CSS 161A

Lesson #4 Decision Making


4.0 Introduction. Normally, statements in a program are executed sequentiallyone after the other. Often it is necessary to branch to different, nonsequential parts of a program. One can accomplish such jumps in a programs with unconditional goto branching. Transfer of control with goto statements often leads to incomprehensive spaghetti code this problem gave rise to structured, goto-less programming. Bohm and Jacopini proved that all programs can be written without goto statements and that, at most, only three control structures are needed for any program: (1) sequence structure, (2) selection structure, and (3) repetition structure. 4.0.1Java provides three types of selection structure: 4.0.1.1 The if selection structure (single). 4.0.1.2 The if/else selection structure (double). 4.0.1.3 The switch selection structure (multiple). 4.0.2 Java provides three types of repetition: 4.0.2.1 The while repetition structure. 4.0.2.2 The do/while repetition structure. 4.0.2.3 The for repetition structure. 4.1 The if Statement. The if Statement is used to choose among alternative courses of action in a program. The if structure is a single-entry/single-exit structure which determines course of action depending on a condition. Consider the pseudocode, If students grade is greater than or equal to 60 Print Passes or written in Java, if (grade >= 60) System.out.println(Passes); If the condition students grade is greater than or equal to 60 is true, then Passes is Printed; else the print statement is ignored. 4.1.1 Example #1: Testing a random integer for negativity. This program uses a random number generator from the Random class in the java.util package.
// Example 4.1 import java.util.Random; //Random class public class RandomExample { public static void main(String args[]) { Random random = new Random(); //generate a pseudo-random integer int n = random.nextInt(); System.out.println("n = " + n);

ECC

Niko ulevski

Lesson 4
if (n < 0) System.out.println("**** System.out.println("Goodbye."); } } n < 0");

CSS 161A

output:
run: n = 2037829948 Goodbye. BUILD SUCCESSFUL (total time: 1 second)

4.1.2 The if/else Selection Structure. The if/else selection structure allows the programmer to specify that a different action is to be performed when the condition is false. Consider the pseudocode, If students grade is greater than or equal to 60 Print Passes else Print Failed or written in Java, if (grade >= 60) System.out.println(Passes); else System.out.println(Failed); 4.1.2.1 The figure below represents the above code in a flowchart.

4.1.2.2 The above code could be written equivalently with Javas only ternary operator (? :) as follows:
System.out.println(grade >= 60 ? Passed : Failed);

4.1.2.3

Nested if/else structures are possible. For example,

if (grade >= 90) System.out.println(A); else if (grade >= 80)

ECC

Niko ulevski

Lesson 4
System.out.println(B); else if (grade >= 70) System.out.println(C); else if (grade >= 60) System.out.println(D); else System.out.println(F);

CSS 161A

4.1.3 Example #2: Testing two random numbers for their minimum.
// Example 4.2 import java.util.Random; public class RandomExample2 { public static void main(String args[]) { Random random = new Random(); int m = random.nextInt(); System.out.println("m = " + m); int n = random.nextInt(); System.out.println("n = " + n); if (m < n) System.out.println("The minimum is " + m); else System.out.println("The minimum is " + n); } }

output
run: m = 1722056782 n = 1422178696 The minimum is 1422178696 BUILD SUCCESSFUL (total time: 0 seconds)

4.1.4 Example #3: Social Security tax on wages.


//Example #3 Social Security Tax on wages import java.util.Scanner; public class SocialSecurity { // Calculate and print the Social Security // Self-Employment Tax on wages (to be read in). // Author: Deborah R. Klapper, November 6, l994 static final double MAXIMUM_WAGE = 57600, // maximum wage subject to tax TAX_RATE = 0.124; // tax rate on wages less than // the maximum public static void main (String[] args) { double wages, // amount of wages subject to tax tax; // amount of tax Scanner keyboard = new Scanner(System.in); System.out.print( "Your wages subject to Social Security Tax are: $"); wages = keyboard.nextInt(); if (wages <= MAXIMUM_WAGE) tax = TAX_RATE * wages; else tax = TAX_RATE * MAXIMUM_WAGE;

// maximum allowable tax

ECC

Niko ulevski

Lesson 4
System.out.println("Your Social Security Tax is: $" + tax); } }

CSS 161A

output
run: Your wages subject to Social Security Tax are: $5053 Your Social Security Tax is: $626.572 BUILD SUCCESSFUL (total time: 10 seconds)

4.1.5 The dangling else problem. Indenting is a worthwhile practice, but white space is neglected by the compiler. It is important to note that the Java compiler always associates an else with the previous if unless told to do otherwise by the placement of the braces ({}). For example,
if (x > 5) if (y > 5) System.out.println(x and y are > 5); else System.out.println(x is <= 5);

appear that the else is associated with the first ifit is not! It really is
if (x > 5) if (y > 5) System.out.println(x and y are > 5); else System.out.println(x is <= 5);

To force the precedence with the second if use braces a s follows,


if (x > 5) { if (y > 5) System.out.println(x and y are > 5); } else System.out.println(x is <= 5);

4.2 Constructing and Analyzing Boolean Expressions. Java provides operators that may be used to form more complex conditions by combining logical operators. These logical operators are: 4.2.1 && (logical AND)short circuit logic. 4.2.2 & (boolean logical AND)evaluates both operands. 4.2.3 || (logical OR) short circuit logic. 4.2.4 | (boolean logical OR)evaluates both operands. 4.2.5 ^ (boolean logical exclusive OR)evaluates both operands. 4.2.6 != (logical NOT). 4.2.7 Example #4: Day of the Year.
//Example #4: Day of the Year import java.util.Scanner; public class DayOfYear { public static void main (String[] args) { int dayNumber, year, month, day; Scanner keyboard = new Scanner(System.in); System.out.print("Enter year: "); year=keyboard.nextInt();

ECC

Niko ulevski

Lesson 4
System.out.print("Enter month: "); month=keyboard.nextInt(); System.out.print("Enter day: "); day=keyboard.nextInt(); // Calculate dayNumber assuming all months have 31 days dayNumber = (month - 1) * 31 + day; // Correct for months beyond February if (month > 2) { // Assume non-leap year if ((month == 3) || (month == 4)) dayNumber = dayNumber - 3; else if ((month == 5) || (month == 6)) dayNumber = dayNumber - 4; else if ((month == 7) || (month == 8) || (month == 9)) dayNumber = dayNumber - 5; else if ((month == 10) || (month == 11)) dayNumber = dayNumber - 6; else if (month == 12) dayNumber = dayNumber - 7; if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) // Correct for leap year dayNumber = dayNumber + 1; } System.out.println("Day number " + dayNumber); } }

CSS 161A

output
run: Enter year: 2010 Enter month: 4 Enter day: 21 Day number 111 BUILD SUCCESSFUL (total time: 13 seconds)

4.2.8 Example #5. //LogicalOperators.java


//Example #5. LogicalOperators.java // Demonstrating the logical operators import java.awt.Graphics; import java.applet.Applet; public class LogicalOperators extends Applet { public void paint( Graphics g ) { g.drawString( "Logical AND (&&)", 10, 25 ); g.drawString( "F && F: " + ( false && false ), 10, 40 ); g.drawString( "F && T: " + ( false && true ), 10, 55 ); g.drawString( "T && F: " + ( true && false ), 10, 70 ); g.drawString( "T && T: " + ( true && true ), 10, 85 ); g.drawString( g.drawString( g.drawString( g.drawString( g.drawString( "Logical "F || F: "F || T: "T || F: "T || T: OR (||)", 215, 25 ); " + ( false || false ), 215, 40 ); " + ( false || true ), 215, 55 ); " + ( true || false ), 215, 70 ); " + ( true || true ), 215, 85 );

g.drawString( "Boolean logical AND (&)", 10, 115 ); g.drawString( "F & F: " + ( false & false ), 10, 130 );

ECC

Niko ulevski

Lesson 4
g.drawString( "F & T: " + ( false & true ), 10, 145 ); g.drawString( "T & F: " + ( true & false ), 10, 160 ); g.drawString( "T & T: " + ( true & true ), 10, 175 ); g.drawString( "Boolean logical inclusive OR (|)", 215, 115 ); g.drawString( "F | F: " + ( false | false ), 215, 130 ); g.drawString( "F | T: " + ( false | true ), 215, 145 ); g.drawString( "T | F: " + ( true | false ), 215, 160 ); g.drawString( "T | T: " + ( true | true ), 215, 175 ); g.drawString( "Boolean logical exclusive OR (^)", 10, 205 ); g.drawString( "F ^ F: " + ( false ^ false ), 10, 220 ); g.drawString( "F ^ T: " + ( false ^ true ), 10, 235 ); g.drawString( "T ^ F: " + ( true ^ false ), 10, 250 ); g.drawString( "T ^ T: " + ( true ^ true ), 10, 265 ); g.drawString( "Logical 215, 205 g.drawString( "!F: " + g.drawString( "!T: " + } } NOT (!)", ); ( !false ), 215, 220 ); ( !true ), 215, 235 );

CSS 161A

4.2.8 Example #6: Combining Several Boolean Expressions


// Example 4.6 Combining Several Boolean Expressions import java.util.Random; public class BooleanExpressions { public static void main(String args[]) { Random random = new Random(); float t = random.nextFloat(); System.out.println("t = " + t); if (t < 0.25 || t >= 0.75) System.out.println("Either t < 0.25 or t >= 0.75");

ECC

Niko ulevski

Lesson 4
else System.out.println("0.25 <= t < 0.75"); } }

CSS 161A

output
run: t = 0.8852026 Either t < 0.25 or t >= 0.75 BUILD SUCCESSFUL (total time: 1 second)

4.3 The switch Statement. Java uses the switch statement to make a decision based on the value of an integer-valued variable or expression. It is similar to the selection statement, if else. 4.3.1 The switch syntax.

switch (expression) { case const1: stmt-seq1; case const2: stmt-seq2; case const3: stmt-seq3; default: stmt-seqN; } 4.3.2 Example #7. Case with randomness.
// Example 7 Case with randomness import java.util.Random; public class RandomCase { public static void main(String[] args) { Random random = new Random(); int n = random.nextInt(); System.out.println("n = " + n); n %= 3; n += 2; System.out.println("n = " + n); switch (n) //Whats wrong with the following code? { case 0: System.out.println("This is case 0."); case 1: System.out.println("This is case 1."); case 2: System.out.println("This is case 2."); case 3: System.out.println("This is case 3."); default: System.out.println("This is the default case."); } } }

output
run: n = -800487061 n = 1

ECC

Niko ulevski

Lesson 4
This is case 1. This is case 2. This is case 3. This is the default case. BUILD SUCCESSFUL (total time: 0 seconds)

CSS 161A

4.4 Algorithms and Pseudocode. Any computing problem can be solved by executing a series of actions in a specific order. A procedure for solving a problem in terms of (1) the actions to be executed, and (2) the order in which these actions are to be executed is called an algorithm. 4.4.1 Pseudocode is an artificial and informal language that helps programmers develop algorithms. The pseudocode we present here is particularly useful for developing algorithms that will be converted to structured portions of Java programs. Pseudocode is similar to everyday English; it is convenient and user-friendly although it is not an actual computer programming language. 4.4.2 Pseudocode programs are not actually executed on computers. Rather, they help the programmer "think out" a program before attempting to write it in a programming language such as Java. 4.4.3 Pseudocode normally describes only executable statements--the actions that are performed when the program is converted from pseudocode to Java and is run.
Java keywords. Java Keywords abstract boolean break byte catch char class continue do double else extends final finally float for implements import instanceof int long native new null private protected public return static super switch synchronized throw throws transient true void volatile while Keywords that are reserved but not used by Java const goto case default false if interface package short this try

4.5 Using Conditionals in Applets. The idea in this section is to provide a way of distinguishing two events in order to perform the appropriate action. 4.5.1 Temperature example revisited.
ECC 8 Niko ulevski

Lesson 4
import java.applet.*; import java.awt.*; import java.awt.event.*; public class ConvertApplet extends Applet implements ActionListener { // Declare and allocate Components TextField tFahr = new TextField(9), tCent = new TextField(9); public void init() { // Arrange Component layout add(new Label("Fahrenheit")); add(tFahr); add(new Label("Centigrade")); add(tCent); // Register Component Listeners tFahr.addActionListener(this); tCent.addActionListener(this); } public void actionPerformed (ActionEvent e) { // Respond to Action Events: // 1. tFahr TextField // 2. tCent TextField double fahr, cent; if (e.getSource()==tFahr) { fahr = new Double(tFahr.getText()).doubleValue(); cent = 5.0 * (fahr - 32.0) / 9.0; tCent.setText(cent + ""); } else { cent = new Double(tCent.getText()).doubleValue(); fahr = 9.0 * cent / 5.0 + 32.0; tFahr.setText(fahr + ""); } } }

CSS 161A

4.5.2 Class Exercise: Write a Java application program which prompts the user for numerical month and year (both integers) and outputs the number of days in the month. For example, if the input is 2 and 2000, the output should be: Number of days = 29.
// Leap Year Example import java.util.Scanner; public class DaysinMonth { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter year: "); int year=keyboard.nextInt(); System.out.print("Enter month: "); int month = keyboard.nextInt(); int numDays = 0;

ECC

Niko ulevski

Lesson 4
switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; break; } System.out.println("Year " + year + ", month " + month + " has " + numDays + " days."); } }

CSS 161A

output
run: Enter year: 2008 Enter month: 2 Year 2008, month 2 has 29 days. BUILD SUCCESSFUL (total time: 10 seconds)

4.7 The while Repetition Structure. A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true. The pseudocode statement
While there are more items on my shopping list Purchase next item and cross it off my list

describes the repetition that occurs during a shopping trip. The condition, "there are more items on my shopping list" may be true or false. If it is true, then the action, "Purchase next item and cross it off my list" is performed. This action will be performed repeatedly while the condition remains true (shopping trouble!). 4.7.1 An example. As an example of a while structure, consider a program segment designed to find the first power of 2 larger than 1000. Suppose int variable product has been initialized to 2. When the following while structure finishes executing, product contains the result:
int product = 2; while ( product <= 1000 ) ECC 10 Niko ulevski

Lesson 4 product = 2 * product;

CSS 161A

4.7.2 When the while structure is entered, product is 2. Variable product is repeatedly multiplied by 2, taking on the values 4, 8, 16, 32, 64, 128, 256, 512 and 1024 successively. When product becomes 1024, the condition product <= 1000 in the while structure becomes false. This terminates the repetition with 1024 as product's final value. Execution continues with the next statement after the while. [Note: If a while structure's condition is initially false the body statement(s) will never be performed.] 4.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition). To illustrate how algorithms are developed, we solve several variations of a class-averaging problem. Consider the following problem statement:
A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. Pseudocode algorithm that uses counter-controlled repetition to solve the class-average problem. Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average
// Fig. 4.7: Average1.java // Class average program with counter-controlled repetition import javax.swing.JOptionPane; public class Average1 { public static void main( String args[] ) { int total, // sum of grades gradeCounter, // number of grades entered gradeValue, // grade value average; // average of all grades String grade; // grade typed by user // Initialization Phase total = 0; // clear total gradeCounter = 1; // prepare to loop // Processing Phase while ( gradeCounter <= 10 ) { // loop 10 times

// prompt for input and read grade from user grade = JOptionPane.showInputDialog( "Enter integer grade: " ); // convert grade from a String to an integer gradeValue = Integer.parseInt( grade ); // add gradeValue to total

ECC

11

Niko ulevski

Lesson 4
total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; } // Termination Phase average = total / 10; // perform integer division

CSS 161A

// display average of exam grades JOptionPane.showMessageDialog( null, "Class average is " + average, "Class Average", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // terminate the program

output 4.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition). Let us generalize the class-average problem. Consider the following problem:
Develop a class-averaging program that will process an arbitrary number of grades each time the program is run.

In the first class-average example, the number of grades (10) was known in advance. In this example, no indication is given of how many grades are to be entered. The program must process an arbitrary number of grades. How can the program determine when to stop the input of grades? How will it know when to calculate and print the class average? 4.9.1 We begin with a pseudocode representation of the top:
Determine the class average for the quiz

We divide the top into a series of smaller tasks and list these in the order in which they need to be performed. This results in the following first refinement.
Initialize variables Input, sum up and count the quiz grades Calculate and print the class average

4.9.2 The pseudocode statement


Input, sum up and count the quiz grades

requires a repetition structure (i.e., a loop) that successively inputs each grade. The second refinement of the preceding pseudocode statement is then
Input the first grade (possibly the sentinel) While the user has not as yet entered the sentinel Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel)

4.9.3 The pseudocode statement


Calculate and print the class average

may be refined as follows:


If the counter is not equal to zero Set the average to the total divided by the counter Print the average ECC 12 Niko ulevski

Lesson 4 else Print "No grades were entered"


1) 2) 3) 4) 5) 6) 7) 8) 9) 10) 11) 12) 13) 14) 15) 16) 17) 18) 19) 20) 21) 22) 23) 24) 25) 26) 27) 28) 29) 30) 31) 32) 33) 34) 35) 36) 37) 38) 39) 40) 41) 42) 43) 44) 45) 46) 47) 48) 49) 50) 51) 52) 53) 54) 55) 56) 57) 58) 59) 60) 61) // Fig. 4.9: Average2.java // Class average program with sentinel-controlled repetition import javax.swing.JOptionPane; import java.text.DecimalFormat; public class Average2 { public static void main( { int gradeCounter, // gradeValue, // total; // double average; // String input; // String args[] ) number of grades entered grade value sum of grades average of all grades grade typed by user

CSS 161A

// Initialization phase total = 0; // clear total gradeCounter = 0; // prepare to loop // Processing phase // prompt for input and read grade from user input = JOptionPane.showInputDialog( "Enter Integer Grade, -1 to Quit:" ); // convert grade from a String to an integer gradeValue = Integer.parseInt( input ); while ( gradeValue != -1 ) { // add gradeValue to total total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; // prompt for input and read grade from user input = JOptionPane.showInputDialog( "Enter Integer Grade, -1 to Quit:" ); // convert grade from a String to an integer gradeValue = Integer.parseInt( input ); } // Termination phase DecimalFormat twoDigits = new DecimalFormat( "0.00" ); if ( gradeCounter != 0 ) { average = (double) total / gradeCounter; // display average of exam grades JOptionPane.showMessageDialog( null, "Class average is " + twoDigits.format( average ), "Class Average", JOptionPane.INFORMATION_MESSAGE ); } else JOptionPane.showMessageDialog( null, "No grades were entered", "Class Average", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // terminate the program

4.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures). We will once again formulate the algorithm using
ECC 13 Niko ulevski

Lesson 4

CSS 161A

pseudocode and top-down, stepwise refinement, and we will write a corresponding Java program. Consider the following problem statement: A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, several of the students who completed this course took the licensing examination. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam and a 2 if the student failed. 4.10.1 Let us proceed with top-down, stepwise refinement. We begin with a pseudocode
representation of the top: Analyze exam results and decide if tuition should be raised Our first refinement is Initialize variables Input the ten exam grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised

4.10.2 The pseudocode statement


Input the ten quiz grades and count passes and failures

requires a loop that successively inputs the result of each exam. The refinement of the preceding pseudocode statement is then
While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter

4.10.3 Pseudocode for examination-results problem.


Initialize passes to zero Initialize failures to zero Initialize student to one While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes else Add one to failures Add one to student counter Print the number of passes Print the number of failures If more than eight students passed Print "Raise tuition" ECC 14 Niko ulevski

Lesson 4
1) 2) 3) 4) 5) 6) 7) 8) 9) 10) 11) 12) 13) 14) 15) 16) 17) 18) 19) 20) 21) 22) 23) 24) 25) 26) 27) 28) 29) 30) 31) 32) 33) 34) 35) 36) 37) 38) 39) 40) 41) 42) 43) // Fig. 4.11: Analysis.java // Analysis of examination results. import javax.swing.JOptionPane; public class Analysis { public static void main( String args[] ) { // initializing variables in declarations int passes = 0, // number of passes failures = 0, // number of failures student = 1, // student counter result; // one exam result String input, // user-entered value output; // output string // process 10 students; counter-controlled loop while ( student <= 10 ) { input = JOptionPane.showInputDialog( "Enter result (1=pass,2=fail)" ); result = Integer.parseInt( input ); if ( result == 1 ) passes = passes + 1; else failures = failures + 1; student = student + 1; } // termination phase output = "Passed: " + passes + "\nFailed: " + failures; if( passes > 8 ) output = output + "\nRaise Tuition"; JOptionPane.showMessageDialog( null, output, "Analysis of Examination Results", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } }

CSS 161A

4.11 Assignment Operators. Java provides several assignment operators for abbreviating assignment expressions. Arithmetic assignment operators.
Assignment operator += -= *= /= %= Sample expression c += 7 d -= 4 e *= 5 f /= 3 g %= 9 Explanation Assigns

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; c = c + 7 d = d - 4 e = e * 5 f = f / 3 g = g % 9 10 to c 1 to d 20 to e 2 to f 3 to g

ECC

15

Niko ulevski

Lesson 4

CSS 161A

4.12 Increment and Decrement Operators. Java provides the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig. 4.13.
The increment and decrement operators. Operator Called ++ preincrement Sample expression ++a Explanation Increment a by 1, then use the new value of a in the expression in which a resides. Use the current value of a in the expression in which a resides, then increment a by 1. Decrement b by 1, then use the new value of b in the expression in which b resides. Use the current value of b in the expression in which b resides, then decrement b by 1.

++

postincrement a++

--

predecrement

--b

--

postdecrement b--

// Fig. 4.14 Increment.java // Preincrementing and postincrementing public class Increment { public static void main( String args[] ) { int c; c = 5; System.out.println( c ); // print 5 System.out.println( c++ ); // print 5 then postincrement System.out.println( c ); // print 6 System.out.println(); // skip a line

c = 5; System.out.println( c ); // print 5 System.out.println( ++c ); // preincrement then print 6 System.out.println( c ); // print 6 } }

output
run: 5 5 6 5 6 6 BUILD SUCCESSFUL (total time: 1 second)

4.12.1 The chart in Fig. 4.15 shows the precedence and associativity of the operators introduced to this point.
ECC 16 Niko ulevski

Lesson 4 Precedence and associativity of the operators discussed so far. Operators () ++ -++ -- + - (type)

CSS 161A

* / % + < <= > >= == != ?: = += -= *= /= %=

Associativity left to right right to left right to left left to right left to right left to right left to right right to left right to left

Type parentheses unary postfix unary multiplicative additive relational equality conditional assignment

4.13 Primitive Data Types. The table in Fig. 4.16 lists the primitive data types in Java. The primitive types are the building blocks for more complicated types. Like its predecessor languages C and C++, Java requires all variables to have a type before they can be used in a program. For this reason, Java is referred to as a strongly typed language.
The Java primitive data types. Type boolean char byte short int long Size in Values bits 8 true or false 16 \u0000 to \uFFFF 8 16 32 64 128 to +127 32,768 to +32,767 2,147,483,648 to +2,147,483,647 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 3.40292347E+38 to +3.40292347E+38 1.79769313486231570E+308 to +1.79769313486231570E+308 Standard

(ISO Unicode character set)

float double

32 64

(IEEE 754 floating point) (IEEE 754 floating point)

ECC

17

Niko ulevski

You might also like