More on Decisions
Download
Report
Transcript More on Decisions
More on Decisions
Overview
The selection Operator
Grouping of Statements
Multiple branches (if else if)
Switch Statement
1
The Selection Operator
Java provides a selection operator which may be used
sometimes to simplify if – else statements. The syntax is:
variable assignment condition ? value1 : value2;
Interpretation:
The condition is first evaluated. If it results in true, the
variable on the left is assigned value1; If the result is false,
variable is assigned value2.
Example:
String status;
double passMark = 65, studentMark = 70;
status = studentMark >= passMark? "pass" : "fail";
The last line in the above example is obviously shorter than
the following if-else version:
if (studentMark >= passMark)
status = "pass" ;
else
status = "fail" ;
Although the selection operator is convenient, it makes
programs less readable, thus we encourage you to always use
if-else instead.
2
Grouping of statements
The syntax of if statement specifies a single
statement in each branch. However, there are
situations where we would like to have more than
one statement in a branch.
For example, suppose we wish to improve the
quadratic example so that it checks that the
equation has real roots before computing the root.
Obviously, the branch of the if statement where the
roots are computed involves more than one
statement.
To solve this problem, Java allows several
statements to be grouped together into a block
statement using braces { …}
A block of statement can be used wherever a
statement is allowed in the Java syntax.
The example on the next slide demonstrate this.
3
Grouping of statements
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class QuadraticEquation2 {
public static void main(String[] args) throws
IOException{
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
double a, b, c, root1, root2, disc;
System.out.print("Enter value of a: ");
a= Integer.parseInt(stdin.readLine());
System.out.print("Enter value of b: ");
b= Integer.parseInt(stdin.readLine());
System.out.print("Enter value of c: ");
c= Integer.parseInt(stdin.readLine());
disc = b*b - 4*a*c;
if (disc >= 0) {
root1 = (-b + Math.sqrt(disc))/(2*a);
root2 = (-b - Math.sqrt(disc))/(2*a);
System.out.println("The roots are:
"+root1+" and "+root2);
} else
System.out.println("Equation has no real roots");
}
}
4
if else if statement
To achieve multiple branches, several if
statements can be nested as shown in the
following example..
Example :
int testscore = ... ;
char grade;
if (testscore >= 90)
grade = 'A';
else if (testscore >= 80)
grade = 'B';
else if (testscore >= 70)
grade = 'C';
else if (testscore >= 60)
grade = 'D';
else
grade = 'F';
5
if else if statement
Example:
import java.io.*;
class NestedIf {
public static void main(String[]args) throws
java.io.IOException {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("enter a number: ");
int number=Integer.parseInt(stdin.readLine());
if ( number > 0 )
System.out.println("it is positive");
else if ( number < 0 )
System.out.println("it is negative");
else
System.out.println("it is 0");
}
}
6
switch statement
The Switch statement can also be used to handle
multiple branches. The Syntax is:
switch (controllingExpression) {
case value1 :
Statements;
break;
case value2 :
Statements;
break;
...
default :
Statements;
}
Where value1, value2, etc are integer constants and
controllingExpression is an expression that
evaluates to integer value.
7
switch statement
Interpretation
» The controllingexpression is evaluated and its result is
compared against value1, value2, etc.
» The statement of the matching value is executed
» If there is no break, execution continues in the next
branch until a break is found or until end of the switch
statement.
» The default branch is optional. If there is no matching
branch and there is a default branch, it is executed.
Example:
import java.io.IOException;
import java.io. InputStreamReader;
import java.io.BufferedReader;
class DayOfWeek {
public static void main(String[] args) throws
IOException {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(“Enter a number [1 .. 7]: );
String input=stdin.readLine();
8
switch statement Example (cont’d)
int dayNumber=Integer.parseInt(input);
switch (dayNumber) {
case 1: System.out.println("Saturday");
break;
case 2:
System.out.println("Sunday");
break;
case 3:
System.out.println("Monday");
break;
case 4:
System.out.println("Tuesday");
break;
case 5: System.out.println("Wednesday");
break;
case 6:
System.out.println("Thursday");
break;
case 7:
System.out.println("Friday");
break;}
default:
System.out.println(“Inavlid input");
}
}
}
9
switch statement Example-without break
import java.io.*;
class DayOfWeek {
public static void main(String[] args) throws
java.io.IOException {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String input=stdin.readLine();
System.out.print(“Enter a number [1…7]: “);
int dayNumber=Integer.parseInt(input);
switch (dayNumber) {
case 1: System.out.println("Saturday");
case 2: System.out.println("Sunday");
case 3: System.out.println("Monday");
case 4: System.out.println("Tuesday");
case 5: System.out.println("Wednesday");
case 6: System.out.println("Thursday");
case 7: System.out.println("Friday");
}
}
}
What will be the output, if the input is 3 ?
10