CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer...
Transcript of CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer...
CS111: PROGRAMMING
LANGUAGE II
Lecture 1(c): Java Basics (II) Computer Science
Department
Lecture Contents
Computer Science Department
Java basics (part II)
Conditions
Loops
Methods
Conditions & Branching
Computer Science Department
Conditional Statements
A conditional statement lets us choose which
statement will be executed next
Conditional statements give us the power to make
basic decisions
Java's conditional statements:
the if and if-else statements
the conditional operator
the switch statement
Computer Science Department
The if Statement
The if statement has the following syntax:
if is a Java
reserved word
The condition must be a boolean expression.
e.g., a boolean variable, a == b, a <= b.
It must evaluate to either true or false.
If the condition is true,
this statement is executed.
if ( condition )
statement1;
else
statement2;
If the condition is false,
this statement is executed. Computer Science Department
•Several statements can be grouped together into a block statement
•A block is delimited by braces ( { … } )
Logic of an if-else statement
true false
condition
evaluated
Statement 1 Statement 2
Computer Science Department
Relational operators
Computer Science Department
Logical Operators
Computer Science Department
Loops & Iterations
Computer Science Department
Loop Statements
while statement
do statement
for statement
while ( condition )
statement;
do
{
statement list;
} while ( condition );
for ( initialization ; condition ; increment )
statement;
Computer Science Department
while ( condition )
statement;
While loops
Computer Science Department
Example
Look at
the code
and
describe
the
output !!
// set initial value of month so that the while condition
// below is false initially
int month = -1;
while (month < 1 || month > 12)
{
System.out.print( “Enter a month (1 to 12): “);
month = scan.nextInt();
}
System.out.print( “Enter a month (1 to 12): “);
int month = scan.nextInt();
while (month < 1 || month > 12)
{
System.out.println( month + “ is not a valid month.” );
System.out.print( “Enter a month (1 to 12): “);
month = scan.nextInt();
}
Computer Science Department
do { statement; } while ( condition );
do loops
Computer Science Department
Example
What is
the
difference
here??
Computer Science Department
int month; // no need to initialize month
do
{
System.out.print( “Enter a month(1 to 12): “);
month = scan.nextInt();
} while (month < 1 || month > 12);
// beginning of the next statement
for ( initialization ; condition ; increment )
statement;
for loops
Computer Science Department
Example
int sum = 0;
for (int counter = 1; counter <= max; counter++)
sum += counter;
// beginning of the next statement
counter++
Establish initial value of control variable.
Determine if final value of control
variable has been
reached.
counter <= max sum+= counter
true
false
int counter = 1
Body of loop (this may be
multiple statements)
Increment the
control variable.
Computer Science Department
Methods
A method:
groups a sequence of statement
takes input, performs actions, and produces output
In Java, each method is defined within specific class
Computer Science Department
Method Declaration: Header
A method declaration begins with a method header
method
name
return
type
parameter list
The parameter list specifies the type and name of each parameter
The name of a parameter in the method declaration is called a formal argument
public class MyClass
{
static int min ( int num1, int num2 ) …
properties Computer Science Department
Method Declaration: Body
The header is followed by the method body:
static int min(int num1, int num2)
{
int minValue = num1 < num2 ? num1 : num2;
return minValue;
}
class MyClass
{ …
…
}
Computer Science Department
The return Statement
The return type of a method indicates the type of
value that the method sends back to the calling
location
A method that does not return a value has a void
return type
The return statement specifies the value that will be
returned
Its expression must conform to the return type
Computer Science Department
Calling a Method
Each time a method is called, the values of the actual arguments in the invocation are assigned to the formal arguments
static int min (int num1, int num2)
{
int minValue = (num1 < num2 ? num1 : num2);
return minValue;
}
int num = min(2, 3);
Computer Science Department
Method Call Stack
A method can call another method, who can call
another method, …
min(num1, num2, num3)
println()
…println(…)
min(1, 2, 3);
main
Computer Science Department
Text Book:
Chap 4
Chap 5
That’s all for today…..
Computer Science Department