Docsity

Java Programming Fundamentals: Control Flow, Expressions, and Input/Output, Lecture notes of Java Programming

Partial preview of the text

Download Java Programming Fundamentals: Control Flow, Expressions, and Input/Output and more Lecture notes Java Programming in PDF only on Docsity!

Java Primer II

CMSC 202

Expressions

• An expression is a construct made up of

variables, operators, and method invocations,

that evaluates to a single value.

• For example:

int cadence = 0;

anArray[0] = 100;

System.out.println("Element 1 at index 0: " + anArray[0]);

int result = 1 + 2;

System.out.println(x == y? "equal" :"not equal");

Comment Types

  • End of line comment – ignores everything else on the line after the
  • Multi-line comment — must open with “/” and close with “/”
  • Javadoc comment — special version of multi-line comment that

starts with “/**”

  • Used by Java’s documentation tool

// compute the volume

/*

  • sort the array using
  • selection sort */

/**

  • Determines if the item is empty
  • @return true if empty, false otherwise */

If-Then Statement

• The if-then statement is the most basic of all

the control flow statements.

if (x == 2) System.out.println("x is 2"); System.out.println("Finished");

if x == 2: print "x is 2" print "Finished"

Python Java

Notes about Java’s if-then :

  • Conditional expression must be in parentheses
  • Conditional expression must result in a boolean value

Blocks

• A block is a group of zero or more statements

that are grouped together by delimiters.

• In Java, blocks are denoted by opening and

closing curly braces ‘{’ and ‘}’.

if(x == 2) { System.out.println("even"); System.out.println("prime"); } System.out.println("Done!");

Note:

  • It is generally considered a good practice to include the curly

braces even for single line statements.

Variable Scope

• That set of code statements in which the variable

is known to the compiler.

• Where a variable it can be referenced in your

program

• Limited to the code block in which the variable is

defined

• For example:

if(age >= 18) { boolean adult = true; } / couldn't use adult here /

If-Then-Else If-Then-Else Statement

• Again, very similar…

if(x < y) { System.out.println("x < y"); } else if (x > y) { System.out.println("x > y"); } else { System.out.println("x == y"); }

if x < y: print "x < y" elif x > y: print "x > y" else: print "x == y"

Python Java

Switch Statement

• Unlike if-then and if-then-else , the switch

statement allows for any number of possible

execution paths.

• Works with byte , short , char , and int primitive

data types.

– As well as enumerations (which we’ll cover later)

Switch Statement

switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("31 days"); break; case 4: case 6: case 9: case 11: System.out.println("30 days"); break; case 2: System.out.println("28 or 29 days"); break; default: System.err.println("Invalid month!"); break; }

Note:

  • Without a break statement, cases “fall through” to the next statement.

While Loops

• The while loop executes a block of statements

while a particular condition is true.

• Pretty much the same as Python…

int count = 0; while(count < 10) { System.out.println(count); count++; } System.out.println("Done!");

count = 0; while(count < 10): print count count += 1 print "Done!"

Python Java

For Loop

• The for statement provides a compact way to iterate

over a range of values.

• The initialization expression initializes the loop – it is

executed once, as the loop begins.

• When the termination expression evaluates to false,

the loop terminates.

• The increment expression is invoked after each

iteration through the loop.

for (initialization; termination; increment) { / ... statement(s) ... / }

For Loop

• The equivalent loop written as a for loop

– Counting from start value (zero) up to (excluding)

some number (10)

for(int count = 0; count < 10; count++) { System.out.println(count); } System.out.println("Done!");

for count in range(0, 10): print count print "Done!"

Python

Java

For Loop

• Iterating over the contents of an array

String[] items = new String[]{"foo","bar","baz"}; for (int i = 0; i < items.length; i++) { System.out.printf("%d: %s%n", i, items[i]); }

items = ["foo", "bar", "baz"] for i in range(len(items)): print "%d: %s" % (i, items[i])

Python

Java

For Each Loop

• Java also has a second form of the for loop known

as a “for each” or “enhanced for” loop.

• This is much more like Python’s for-in loop.

• The general form is:

• For now, we’ll assume that the collection is an

array (though there are other objects it can be,

which we’ll discuss later in the semester).

for ( : ) { / ... do something with item ... / }