Docsity

Java Primer II: Expressions, Statements, and Control Flow, Exercises of Java Programming

Partial preview of the text

Download Java Primer II: Expressions, Statements, and Control Flow and more Exercises 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 invoca@ons,

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 aRer the
  • Mul@-­‐line comment — must open with “/” and close with “/”
  • Javadoc comment — special version of mul@-­‐line comment that

starts with “/**”

  • Used by Java’s documenta@on 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 :

  • Expression must be in parenthesis
  • Expression must result in a boolean value

Blocks

• A block is a group of zero or more statements

that are grouped together into a group

• 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’s generally considered a good prac@ce 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 it can be referenced in your program

• Limited to the code block in which the variable is

defined

• A code block is a set of code enclosed inside of curly

braces

• 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

execu@on paths

• Works with byte , short , char , and int primi@ve

data types

– As well as enumera@ons (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; } Notes:

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

While Loops

• The while loop executes a block of statements

while a par@cular condi@on is true

• Preiy 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 ini/aliza/on expression ini@alizes the loop – it’s

executed once, as the loop begins.

• When the termina/on expression evaluates to false,

the loop terminates

• The increment expression is invoked aRer each

itera@on through the loop

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

For Loop

• The equivalent loop wriien as a for loop

– Coun@ng 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

• Itera@ng 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 collec@on is an

array (though there are other objects it can be

which we’ll discuss later in the semester)

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