CISC 1115
Introduction to Programming Using Java
Conditionals


Reading from the Text

Chapter 3

Chapter 5

Conditionals-- the if statement

Relational Operators

Java's Relational Operators

Note: to compare Strings, use the equals method (of the String class), rather than ==. (There are other methods for greater, less, etc — we'll cover them later):
String s1, s2;
…
s1 = …
…
s2 = …
…
if (s1.equals(s2))	// NOT s1 == s2
	…
…

P03.1 — A Simple if

P03.1 SimpleIf

Write a program that accepts a name and midterm grade and issues a warning if the grade is below 70.



Notes
Sample Runs

Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:

Enter last name: Weiss
Enter first name: Gerald
Enter midterm: 67
Weiss, Gerald better shape up!

Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:

Enter last name: Arnow
Enter first name: David
Enter midterm: 75

P03.2 — A Simple if/else

P03.2 SimpleIfElse

Write a program that accepts a name, midterm, and final exam grades and prints out a pass/fail grade.



Notes
Sample Runs

Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:

Enter last name: Weiss
Enter first name: Gerald
Enter midterm: 55
Enter final: 61
Weiss, Gerald fails the course

Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:

Enter last name: Arnow
Enter first name: David
Enter midterm: 65
Enter final: 56
Arnow, David passes the course

More on Conditionals

Another Sidebar:The Empty Statement

;

Now that We Mentioned Loops … More about the for Loop

Conditionals Within Conditionals

We often need multiple or more sophisticated conditions in our decision-making.

Nesting Only Within the if Part

1. if (<condition1>)
2. 	if (<condition2>>)
3. 		statement

In order to drive, one must be a resident of New York, and be 18 or older. Write a conditional that prints "Can drive" to the screen under the proper conditions.

if (stateOfResidence.equals("NY"))
	if (age >= 18)
		System.out.println("Can drive);
Suppose we also want to print 'Can't drive'

Nesting Only Within the else Part

1. if (<condition1>)
2.	…
3. else
4.	if (<condition2>)
5. 		statement
  • Multiple if/else: Assigning letter grades

    P03.3 LetterGrader

    Write a program that prompts for a midterm and final, calculates the average and assigns a letter grade based upon the following table:
    Average Grade
    90-100 A
    80-89 B
    70-79 C
    60-69 D
    59 and below F

    
    
    
    

    Nested, Cascaded, Waterfall Versions of if Statements

    Nested

    if (…)
    	if (…)
    		…
    	else
    		…
    else
    	if (…)
    		…
    	else
    		…
    

    Cascaded

    if (…)
    	…
    else if (…)
    	…
    else if (…)
    	…
    else
    	…
    

    Waterfall

    if (…) …
    if (…) …
    if (…) …
    

    Maximum, Minimum of 2 Values; of 3 Values...

    Illustrated using Nested and Waterfall
    Program 3.3
    
    Using a sequence of nested if/else's, find the maximum of three integers
    
    Program 3.4
    
    Using a waterfall approach, find the maximum of three integers
    

    The switch Statement

    There are times you are comparing the same variable against several values

    Program

    Write a program that reads in a data file of books. The information for each book is: The legal values for category are: Print out the information for each book together with its expanded category

    Notice the logic of expanding from the single character to the expanded string:

    if (category == 'F')
    	 expanded = "Fiction";
    else if (category == 'B')
    	 expanded = "Biography";
    else if (category == 'N')
    	 expanded = "Non-fiction";
    else if (category == 'A')
    	 expanded = "Anthology";
    else if (category == 'S')
    	 expanded = "Short Stories";
    else if (category == 'J')
    	 expanded = "Juvenile";
    else 
    	 expanded = "Unknown category";
    
    It is often the case that we have such a form of conditional, i.e.,:
    if (var == some value)
       ...
    else if (var == a second valuevalue)
       ...
    else if (var == a third value)
       ...
    ...
    else
       ...
    

    This construct may be replaced with a switch statement:

    switch (var) {
    case some value:
       ...
    	 break;
    	 
    case a second value:
       ...
    	 break;
    
    case a third value:
       ...
    	 break;
    ...
    default:
       ...	 break;
    }	 
    
    How it works: Using a switch, the book category code then becomes:
    switch (category) {
    case 'F':
    	 expanded = "Fiction";
    	 break;
    case 'B':
    	 expanded = "Biography";
    	 break;
    case 'N':
    	 expanded = "Non-fiction";
    	 break;
    case 'A':
    	 expanded = "Anthology";
    	 break;
    case 'S':
    	 expanded = "Short Stories";
    	 break;
    case 'J':
    	 expanded = "Juvenile";
    	 break;
    default: 
    	 expanded = "Unknown category";
    	 break;
    }
    
    Note: The expression of the switch statement is limited to certain types, int, char, and string. Expressions of type double are not legal for 'switch'.

    Files used in Lecture 3

    Lab for Lecture 3