Top Banner
CS111: PROGRAMMING LANGUAGE II Lecture 1(c): Java Basics (II) Computer Science Department
23

CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators

Aug 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS111: PROGRAMMING LANGUAGE II€¦ · Java basics (part II) ... Statement 1 Statement 2 Computer Science Department . Relational operators Computer Science Department . Logical Operators

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

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

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

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

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

•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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Text Book:

Chap 4

Chap 5

That’s all for today…..

Computer Science Department