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...
![Page 1: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/1.jpg)
CS111: PROGRAMMING
LANGUAGE II
Lecture 1(c): Java Basics (II) Computer Science
Department
![Page 2: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/2.jpg)
Lecture Contents
Computer Science Department
Java basics (part II)
Conditions
Loops
Methods
![Page 3: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/3.jpg)
Conditions & Branching
Computer Science Department
![Page 4: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/4.jpg)
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
![Page 5: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/5.jpg)
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
![Page 6: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/6.jpg)
•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
![Page 7: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/7.jpg)
Relational operators
Computer Science Department
![Page 8: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/8.jpg)
Logical Operators
Computer Science Department
![Page 9: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/9.jpg)
Loops & Iterations
Computer Science Department
![Page 10: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/10.jpg)
Loop Statements
while statement
do statement
for statement
while ( condition )
statement;
do
{
statement list;
} while ( condition );
for ( initialization ; condition ; increment )
statement;
Computer Science Department
![Page 11: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/11.jpg)
while ( condition )
statement;
While loops
Computer Science Department
![Page 12: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/12.jpg)
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
![Page 13: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/13.jpg)
do { statement; } while ( condition );
do loops
Computer Science Department
![Page 14: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/14.jpg)
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
![Page 15: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/15.jpg)
for ( initialization ; condition ; increment )
statement;
for loops
Computer Science Department
![Page 16: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/16.jpg)
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
![Page 17: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/17.jpg)
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
![Page 18: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/18.jpg)
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
![Page 19: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/19.jpg)
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
![Page 20: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/20.jpg)
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
![Page 21: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/21.jpg)
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
![Page 22: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/22.jpg)
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
![Page 23: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fd41feea2094b648d6e129e/html5/thumbnails/23.jpg)
Text Book:
Chap 4
Chap 5
That’s all for today…..
Computer Science Department