Docsity

Control Flow and Conditionals - Object Oriented Program I | CMSC 131, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 131 Spring 2007
Jan Plane (adapted from Bonnie Dorr)
Lecture Set #5:
If Statements
Last time:
1.
Variables and types
2.
Expressions in Java
3.
User input with Scanner objects
This set:
1.
if statements
CMSC 131 Spring 2007
Jan Plane (adapted from Bonnie Dorr)
1
Control Flow and Conditionals
Control flow: the order in which statements
are executed
General rule: top to bottom
Conditional statements permit control flow to
be dependent on (true/false) conditions
if
if-else
CMSC 131 Spring 2007
Jan Plane (adapted from Bonnie Dorr)
2
The if Statement
Form:
if ( <boolean-expression> ){
<statement>
}
Example:
if (inchesOfSnow > 7) {
System.out.println( “Go home” );
}
The println statement is executed only if the
variable “inchesOfSnow” is greate r than 7
Otherwise, it is skipped
boolean expression =
a condition: true or false
statement
pf3
pf4
pf5

Partial preview of the text

Download Control Flow and Conditionals - Object Oriented Program I | CMSC 131 and more Study notes Computer Science in PDF only on Docsity!

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

Lecture Set #5:

If Statements

Last time:

1. Variables and types

2. Expressions in Java

3. User input with Scanner objects

This set:

1. if statements

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 1

Control Flow and Conditionals

 Control flow: the order in which statements

are executed

General rule: top to bottom

 Conditional statements permit control flow to

be dependent on (true/false) conditions

 if

 if-else

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 2

The if Statement

 Form:

if ( <boolean-expression> ){

 Example:

if (inchesOfSnow > 7) {

System.out.println( “Go home” );

 The println statement is executed only if the

variable “inchesOfSnow” is greater than 7

 Otherwise, it is skipped

boolean expression =

a condition: true or false

statement

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

3

Example 6

public class Example6 {

public static void main(String[] args) {

Scanner sc = new Scanner(System. in );

System. out .print("Enter an integer: ");

int i = sc.nextInt();

if (i < 0){

System. out .println("That was a negative number!");

System. out .println("The number was " + i);

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 4

The if-else Statement

 Form:

if ( ){

<statements 1>;

}else{

<statements 2>;

 Example:

if (inchesOfSnow > 7) { System.out.println(“Go home”); } else { System.out.println(“Go to school”); }

 If “inchesOfSnow” > 7, the first println statement is executed and the

second is skipped

 Otherwise (i.e. inchesOfSnow ≤ 7), the first println statement is skipped

and the second is executed

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 5

Indentation Convention for if-

else

The if-else class of statements should have

the following form:

 if ( condition ) {

statements ;

}

 if ( condition ) {

statements ;

} else {

statements ;

}

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

9

Indentation Conventions for

Blocks

 Either

if (…) {

statement 1;

statement 2;

 Or

if (…)

statement 1;

statement 2;

This is what we will use

(Sun code convention)

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 10

See Sun Code Conventions on

Resource Page!

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 11

Java and White Space

 You can add:

 carriage returns

 spaces

 tabs

wherever you want in Java

 Properly used, this makes your program

easier to read and understand

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr)

12

Example 7

public class Example7 {

public static void main(String[] args) {

Scanner sc = new Scanner(System. in );

System. out .print("Enter an integer: ");

int i = sc.nextInt();

if (i < 0) {

System. out .println("That was a negative number!");

} else {

System. out .println("That was a non-negative number!");

System. out .println("The number was: " + i);

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 13

Logical Operators

Used for forming more complex conditions.

 “and” &&

if ( temp >= 97 && temp <= 99 ) { System.out.println( “Patient is healthy” ); }

 “or” ||

if ( months >= 3 || miles >= 3000 ) { System.out.println( “Change your oil” ); }

 “not”:!

if (! phone.equals( “301-555-1212” ) ) { System.out.println( “Sorry, wrong number” ); }

CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 14

Example 8

public class Example8 {

public static void main(String[] args) { Scanner sc = new Scanner(System. in );

System. out .print("Enter an integer: "); int i = sc.nextInt();

if (i < 0) { System. out .println("That was a negative number!"); System. out .println("I prefer positive ones, so I'll fix it..."); i = -i; } else { System. out .println("That was a positive number!"); System. out .println("That makes me happy."); } System. out .println("The number is now " + i); } }