Download Lecture 4, Decisions and Loops

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 4
CS140
Dick Steflik
Reading Keyboard Input
• Import java.util.Scanner
– A simple text scanner which can parse primitive types and strings
using regular expressions.
• Can be used to extract tokens from strings, System.in, files, Streams….
• http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
Import java.util.Scanner;
// System.in is the keyboard; create a Scanner for the keyboard
Scanner in = new Scanner(System.in);
// print a prompt
System.out.print("Enter an integer");
int myInteger = in.nextInt();
Output
• System.out.print( )
• System.out.println( )
• System.out.printf("formatting string", v0,v1…)
http://docs.oracle.com/javase/tutorial/java/data/numberformat.html
Decisions
•
•
•
This should be review
Since C, C++ and Java are all syntactically very similar the if statement should
be familiar.
The general format is:
// in its simplest form
if (some predicate to evaluate to true or false)
// a statement (simple or complex)
{ … };
-or// slightly more complex
if (some predicate to evaluate to true or false)
// a statement (simple or complex)
{ … };
else
// a statement (simple or complex)
{ … };
Statements
• A simple statement ends with a ";" character can stand on their own
• Compound statements (sequences of simple statements) must be
enclosed between "{ }" characters.
• As a matter of coding style if the then (assumed) part of an if or the else
part of an if is a simple statement surround the statement with "{ }"; its
safer in the long run
if (a == b)
{ c = 5; }
else
{ c = 6;}
Decisions (more)
• Note: the ";" belongs as the terminator of the simple statement, not the
compound statement
if (a == b)
{ c = 5;
d = d * c + a;
}
else
{ c = 6;
d = d * c + b;
}
Decisions (more)
• We can also use if to make ladders/filters/demultoplexers:
if (expression)
{ statement }
else if (expression)
{ statement }
else if ( expression )
{ statement }
else if (expression)
{ statement }
else // optional
{ statement }
Decisions (more)
• if is used to make a 1 of 2 decision; if true do this else do that
• another way to accomplish what an if ladder does is to use the switch
statement:
switch (value) {
case 1: statement ; break;
case 2: statement ; break;
case 3: statement ; break;
default: statement; break;
}
• is equivalent to:
if (value ==1) statement ;
else if (value == 2) statement ;
else if (value == 3) statement;
else statement;
Strings
• Something to note about using Strings in the predicate of an if statement
•
– == can be used to test primitives for equality but not objects
– Strings are immutable objects, objects must have their own methods for doing
comparisons
The String class has a method called equals that takes a string as its argument and
compares the value of the String to that argument and returns true or false:
Scanner in = new Scanner(System.in);
String name = in.next();
String dick = "Dick Steflik";
if ( name.equals(dick)
// it must be Dick
else
// it isn't Dick
Loops
• Definite iteration; we know how many times
we want to execute the body
– for
• Indefinite iteration, we don't know how many
times we want to execute the body
– while – may never execute the body of the loop
– do – will always execute the body of the loop at
least once
for
• general form is:
for ( initialization ; termination ; increment)
{ … }
ex:
for (i=0 ; i < 10 ; i++)
System.out.print( i + " ");
// prints : 0 1 2 3 4 5 6 7 8 9
for ( i=0 ; i < 10 ; i = i + 2)
System.out.print(I + " ");
// prints: 0 2 4 6 8
while
•
something in the body of the loop or something happening before entering the loop
must terminate the loop :
while (predicate) { body of loop}
ex
•
•
int v = 10;
while (v != 0)
{
System.out.print(v + " ");
v--;
}
// print: 10 9 8 7 6 5 4 3 2 1
In this case the variable v is called a sentinal value, it is being watched to know when to
terminate the loop
note that if the loop were entered with v set to 0 then the body would never be
executed
do
• in some languages this construct is called a do/until loop
do {body of loop} while (some predicate)
• ex:
int v = 0
do
{
System.out.print( v + " ")
v=v+1;
}
while ( v != 10)
// prints : 0 1 2 3 4 5 6 7 8 9 10
• note : if v was > 10 at the beginning of the loop we would have a never
terminating loop