lecture6.ppt

advertisement
CS110- Lecture 6
Feb 16, 2005

Announcements


Exam 1 is next week.
Exam 1 will cover:
Section 1.4 – 1.6, Chapter 2,
Section 3.1 – 3.5,
Section 5.1 – 5.4.
No lab next week.
 Project 1 is due on March 6 (Section 1 and
Section 2).
 Sample Exam is posted on course webpage
(different for Section 1 and Section 2)
http://www.cs.umb.edu/cs110/exams/section12/Sa
mpleExam1.html
•
6/30/2016
CS110-Spring 2005, Lecture 6
1
Agenda





Boolean Expressions
The if Statement
Comparing data
The switch statement
Random and Math Class
6/30/2016
CS110-Spring 2005, Lecture 6
2
Flow of Control


The order in which statements are
executed in a running program is called
the flow of control.
Unless specified the execution of a
program proceeds in a linear fashion
i.e. execution starts at first statement
and moves down one statement at a
time until the program is complete.
6/30/2016
CS110-Spring 2005, Lecture 6
3
Flow of Control-Method
invocation
public class StringLength
{
//Execution starts from main
public static void main(String[] args)
{
String name = “James”;
int i = name.length();
. . .
length()
}
}
{
i = length of String
return the length of string
}
Somewhere in String.java
6/30/2016
CS110-Spring 2005, Lecture 6
4
Boolean Expressions


We can alter the flow of control through
the code by using certain type of
statements : conditionals and loops.
The conditional statements:



if statement
if-else statement
switch statement
6/30/2016
These statements allow us
to decide which statement
to execute next. Each
decision is based on
Boolean Expression
(expression that evaluates
to true or false)
CS110-Spring 2005, Lecture 6
5
Boolean Expressions
public class StringLength
{
//Execution starts from main
public static void main(String[] args)
{
String name = “James”;
int i = name.length();
if( i > 10)
System.out.println(“Long Name”);
}
}
6/30/2016
CS110-Spring 2005, Lecture 6
6
Boolean Expressions


A loop or repetition statement allows us
to execute a programming statement
over and over again.
The loop statements:



while statement
do statement
for statement
6/30/2016
These statements are
based on a Boolean
Expression that determines
how many times the
statement is executed.
CS110-Spring 2005, Lecture 6
7
Boolean Expressions

Boolean expressions use

Equality operators ( == and !=)


Relational operators ( <, <=, >, >=)


Tests whether two values are equal or not
Let us decide relative ordering between values
Logical operators ( &&, ||, !)
6/30/2016
CS110-Spring 2005, Lecture 6
8
Equality and Relational
Operators
Examples:
1. if ( i == 5 )
System.out.println(“i equals 5”);
2. if ( i != 5 )
System.out.println(“i does not equal 5”);
3. if ( i > 5 )
System.out.println(“i is greater than 5”);
6/30/2016
CS110-Spring 2005, Lecture 6
9
Logical Operators

Logical NOT (!)

E.g.
if(!done) // done is boolean variable
System.out.println(“Not done”);

Logical AND (&&)


Result is true if both operands are true otherwise
false.
E.g.
if(done && (i > 5))// done is boolean variable
System.out.println(“Done”);
6/30/2016
CS110-Spring 2005, Lecture 6
10
Logical Operators

Logical OR (||)

Result is true if one or the other or both operands
are true otherwise false.

E.g.
if(done || (i > 5))// done is boolean variable
System.out.println(“Done”);

E.g.
int y = 0;
1. (y == 0)
2. (y != 0)
3. (y != 0)
4. (y == 0)
6/30/2016
||
||
&&
&&
(2/y
(2/y
(2/y
(2/y
==
==
==
==
0)
0)
0)
0)
//true
//Error
//false
//Error
CS110-Spring 2005, Lecture 6
11
The if statement
if(Boolean Expression)
statement
Boolean
Expression
Evaluated
true
false
Statement
6/30/2016
CS110-Spring 2005, Lecture 6
12
The if-else statement
public class StringLength
{
//Execution starts from main
public static void main(String[] args)
{
String name = “James”;
int i = name.length();
if( i > 10)
System.out.println(“Long Name”);
else
System.out.println(“Short Name”);
}
}
6/30/2016
CS110-Spring 2005, Lecture 6
13
The if-else statement
import java.util.Scanner;
public class MyClass
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println(“Enter an integer:”);
int num = input.nextInt();
if(num % 2 == 0)
System.out.println(num + “ is an even number”);
else
System.out.println(num + “ is an odd number”);
}
}
6/30/2016
CS110-Spring 2005, Lecture 6
14
Using Block statements



We may want to do more than one
thing as the result of evaluating a
boolean expression.
In Java we can replace any single
statement with a block statement.
A block statement is a collection of
statements enclosed in braces.
6/30/2016
CS110-Spring 2005, Lecture 6
15
Using Block Statement
public class StringLength
{
public static void main(String[] args)
{
String name = “James Gosling”;
if( name.length() > 10)
{
System.out.println(“Very Long Name”);
String newName = name.substring(0,5);
System.out.println(“New Name:”+newName);
}
else
System.out.println(“Short Name”);
}
}
6/30/2016
CS110-Spring 2005, Lecture 6
16
Nested if statements

The statement executed as the result of
an if statement could be another if
statement. This situation is called
nested if.
if( name.length() > 10)
{
System.out.println(“Very Long Name”);
if(name.startsWith(“J”))
System.out.println(“Name starts with J”);
}
6/30/2016
CS110-Spring 2005, Lecture 6
17
Comparing Data

Comparing Characters
String upperCaseName = name.toUpperCase();
if(upperCaseName.charAt(0) < ‘N’ )
System.out.println(“Name ” + name + “ should “
+ “come before my name in dictionary” );

Comparing Objects
if(name.equals(name2))
System.out.println(“Names are same” );
6/30/2016
CS110-Spring 2005, Lecture 6
18
The Switch Statement
switch(Expression)
{
case Expression1:
Block statement
case Expression2:
Block statement
.
.
.
default:
Block statement
The switch statement evaluates
an expression to determine a
value and then matches that
value with one of several possible
values. i.e. it will evaluate
Expression and then matches
the value with Expression1,
Expression2 so on. Execution
then transfers to the first
statement identified by the case
value that matches the result of
the Expression.
}
6/30/2016
CS110-Spring 2005, Lecture 6
19
Switch Statement
switch(month)
{
case 1:
System.out.println(“31 days”);
break;
case 2:
System.out.println(“28 or 29 days”);
break;
case 3:
System.out.println(“31 days”);
break;
If month is 1 this will print “31
........
days”. If month is 2 this will
}
print “28 or 29 days” and so on.
6/30/2016
CS110-Spring 2005, Lecture 6
20
Random Class



Need of Random numbers occurs frequently.
Games often use a random number to
represent the roll of a die etc.
Part of java.util package.

Random rand = new Random();
int num1 = rand.nextInt(); //return some int no.
int num2 = rand.nextInt(10); // return some int
// in the range 0 to 9
6/30/2016
CS110-Spring 2005, Lecture 6
21
Math Class



Provides a large number of basic
mathematical functions.
Methods are static i.e. they can be
invoked with the name of the class. No
need to create objects.
Part of java.lang package.

double num1 = Math.sqrt(25);
6/30/2016
CS110-Spring 2005, Lecture 6
22
Download