lecture03-june10 - Lecture 3 Announcements Assignment 1 out...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 3 Announcements • Assignment 1 out by tomorrow • Design due Wed. in class (6) on paper • Program due Sunday submitted to BB (12) • Topics for today – Continue basic Java introduction •Chars •IO basics • Reference types • Classes and objects briefly • Strings Casting between char and Numeric Types Automatic conversions done as in C int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97; JAVA Control Statements Decision Making - selecting among alternative paths –Boolean values, variables and expressions (T/F) –Using if and if. ..else –Nested if Statements –Using switch Statements –Conditional Operators Iteration - bounded repetition of statement(s) –Looping: while , do, and for –Nested loops –Using break and continue Controlling the flow of the program’s logic All the usual statements exist as in C Loops — two basic patterns Count-controlled loop: • Number of iterations is determined before the loop starts. • Counts each iteration using a counter variable. • Stops when the desired number of iterations has been performed. for (int i = 1; i < 100; i++ ) { System.out.println (“the value of i is: ” + i }; Event-controlled loop: • Before each iteration, checks to see whether some event has occurred. • Continues until that event occurs. • Number of iterations not known beforehand. • The event signal is in the condition to be tested; and may change during an iteration. A boolean variable is often used to flag the signal, e.g. while(notSatisfied) //a boolean expression {// do other stuff } Multi-way Branching Technique switch ( expression ) { case value1 : // statements for this case break; case value2 : // statements for this case break; . . . default: // statements for default case } expression must be either integer or char - it is sometimes called a selector • case values must be literals •Can have as many cases as needed •break statements are optional •default case is optional •Semantics are that a case is found based on matching the expression to the values in top to bottom order switch Statement Example // Select a transportation vehicle based on the // # of additional passengers int numberOfPassengers = input.nextInt( ); switch (numberOfPassengers) { case 0: System.out.println(“Take the Harley”); break; case 1: System.out.println(“Take the Dune Buggy”); break; case 2: System.out.println(“Take the Car”); break; default: System.out.println(“Take the SUV”); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
switch Flowchart (with breaks) case1 case2 case3 case4 (default case) . . . selector value? switch Flowchart (without breaks) case1 case2 case3 case4 (default case) . . . selector
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

lecture03-june10 - Lecture 3 Announcements Assignment 1 out...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online