The if-statement
Download
Report
Transcript The if-statement
The if-statement
The if-statement in Java
• What is an if-statement:
• The if-statement is a conditional statement
• The statement is executed only if a given condition is
satisfied
The if-statement in Java (cont.)
• Purpose of an if-statement:
• An if-statement makes sure that the correct condition is
satisfied before a certain action is performed
Syntax and meaning of the if-statement
• Syntax of the if-statement:
if ( CONDITION )
ONE-statement
Syntax and meaning of the if-statement
(cont.)
• Explanation:
• The keyword if announces (to the Java compiler) that we started
an if-statement
• A conditional clause ( CONDITION ) follows the keyword if
• This is the condition of the if-statement
• Following the condition clause, you can write (only) one
statement
• This statement will only be executed if the condition is true
The condition clause of the if-statement
• The condition clause of the if-statement is an expression
that evaluates to true or false
• Expressions that evaluates to true or false are known in
Computer Science as:
• Boolean expressions
The condition clause of the if-statement
(cont.)
• We will learn more about Boolean expressions later
Here, we will first use a few simple Boolean expressions to
learn about the if-statement
The condition clause of the if-statement
(cont.)
• A simple Boolean expression:
• The expression
a<0
will return
• true if the variable a contains a negative value
• false otherwise (i.e., if the variable a contains
zero or a positive value)
Example if-statement: find absolute value
• Consider the following Java program:
import java.util.Scanner;
public class If1
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a;
System.out.print("Enter an integer value: ");
a = in.nextInt();
// Read in an integer from keyboard
System.out.print("Input value = ");
System.out.println(a);
if ( a < 0 )
a = -a;
// If-statement
// Negate a ONLY if a < 0
System.out.print("It's absolute value = ");
System.out.println(a);
}
}
Example if-statement: find absolute value
(cont.)
• Example outputs:
cheung@HOME2(40)> java If1
Enter an integer value: -4
Input value = -4
It's absolute value = 4
cheung@HOME2(41)> java If1
Enter an integer value: 4
Input value = 4
It's absolute value = 4
Example if-statement: find absolute value
(cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/I
f1.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac If1.java
• To run:
java If1
How is the if-statement executed ?
• Instead of using words, we will use 2 representation
techniques to illustrate how an if-statement is executed
How is the if-statement executed ? (cont.)
• Representation techniques used:
• Flow chart:
• A flow chart is a diagram that represents a
computer algorithm.
• It uses a rectangular box to represent an
assignment statement
• It uses a diamond box to represent a condition
• Lines are used to indicate the flow of the steps in
the algorithm
How is the if-statement executed ? (cont.)
• Structure diagram
• A structure diagram is another type of diagram
to represent a computer algorithm.
• It uses different shapes of boxes to represent
assignment statements and conditions
• The structure diagram does not use lines
Flow chart of if-statement
• Pseudo code of the program:
a = read input from keyboard;
if ( a < 0 )
a = -a;
Print a;
Flow chart of if-statement (cont.)
• Flow chart of the program:
Flow chart of if-statement (cont.)
• If a negative number (e.g., -4) is entered in the input, the
program flow will be as follows:
Flow chart of if-statement (cont.)
• Explanation:
• Because the condition (test) a < 0 is satisfied, the
statement a = −a; is executed
• a is updated to −a
(in example, with 4)
• The execution continues with the subsequent
statement (which prints a) The value 4 is printed.
Flow chart of if-statement (cont.)
• If a non-negative number (e.g., 4) is entered in the input,
the program flow will be as follows:
Flow chart of if-statement (cont.)
• Explanation:
• Because the condition (test) a < 0 is not satisfied,
the statement a = −a; is not executed
• a retains the value 4 !!!
• The execution continues with the subsequent
statement (which prints a) The value 4 is printed.
Structure diagram of if-statement
• Pseudo code of the program:
a = read input from keyboard;
if ( a < 0 )
a = -a;
Print a;
Structure diagram of if-statement (cont.)
• Structure diagram of the program:
Structure diagram of if-statement (cont.)
• If a negative number (e.g., -4) is entered in the input, the
program flow will be as follows:
Structure diagram of if-statement (cont.)
• Explanation:
• Because the condition (test) a < 0 is satisfied, the
statement a = −a; is executed
• a is updated to −a
(in example, with 4)
• The execution continues with the subsequent
statement (which prints a)
The value 4 is printed.
Structure diagram of if-statement (cont.)
• If a non-negative number (e.g., 4) is entered in the input,
the program flow will be as follows:
Structure diagram of if-statement (cont.)
• Explanation:
• Because the condition (test) a < 0 is not satisfied,
the statement a = −a; is not executed
• a retains the value 4 !!!
• The execution continues with the subsequent
statement (which prints a)
The value 4 is printed.
Structure diagram of if-statement (cont.)
• Note:
• I will use the structure diagram technique to illustrate
some of the algorithms discussed later in this course.
Computer jargon
• Terminology:
• If-condition = the condition clause in the if-statement
• Then-part = the statement(s) that are executed when the
if-condition is true
Schematically:
Multiple statements in the "then"-part of
the if-statement
• Previously discussed: syntax of the if-statement:
if ( CONDITION )
ONE-statement
Multiple statements in the "then"-part of
the if-statement (cont.)
• Notes:
• You can only use one statement in the then-part
• Fact:
• It is very common to use multiple statements in the
then-part of an if-statement.
Multiple statements in the "then"-part of
the if-statement (cont.)
• Previously discussed: block
(http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/0
3/intro-java.html#block)
• Block:
• Block = a pair of "{" and "}" braces that
groups components in a Java program together
Multiple statements in the "then"-part of
the if-statement (cont.)
• Example:
Multiple statements in the "then"-part of
the if-statement (cont.)
• Statement block: multiple statements considered as one
statement:
• Statement block:
• Statement block = multiple statements grouped
together by braces { .... }
Multiple statements in the "then"-part of
the if-statement (cont.)
• Example:
Multiple statements in the "then"-part of
the if-statement (cont.)
• Java language fact:
• A statement block in Java is considered as one
(single) statement
Statement block are commonly used as the then-body of ifstatements
Example if-statement: using a statement
block in the then-body
• Previously discussed: Java program that implements the
a,b,c-formula
import java.util.Scanner; // Import Scanner class (contains methods
// for reading keyboard input)
public class Abc2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
Example if-statement: using a statement
block in the then-body (cont.)
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
Example if-statement: using a statement
block in the then-body (cont.)
• Shortcoming:
• The program does not work when b*b - 4*a*c < 0
Schematically:
Example if-statement: using a statement
block in the then-body (cont.)
• Java program:
import java.util.Scanner;
public class If2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
Example if-statement: using a statement
block in the then-body (cont.)
x1 = x2 = 0;
// Default solution
/* -----------------------------------------------------Only compute solution if determinant b*b - 4*a*c >= 0
------------------------------------------------------ */
if ( b*b - 4*a*c >= 0 )
{
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
}
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
Example if-statement: using a statement
block in the then-body (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/I
f2.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac If2.java
• To run:
java If2
Some commonly made mistakes in ifstatements
• Common error 1: bogus semicolon after the if-condition
• Example:
import java.util.Scanner;
public class If3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Some commonly made mistakes in ifstatements (cont.)
int a;
System.out.print("Enter an integer value: ");
a = in.nextInt();
// Read in an integer from keyboard
System.out.print("Input value = ");
System.out.println(a);
if ( a < 0 ) ;
a = -a;
// Bogus ;
System.out.print("It's absolute value = ");
System.out.println(a);
}
}
Some commonly made mistakes in ifstatements (cont.)
• Structure diagram of this erroneous program:
Some commonly made mistakes in ifstatements (cont.)
• Notes:
• The then-part contains no statement !!
• The statement a = -a; is not part of the then-part !!!
Therefore, the statement a = -a; is always executed.
Some commonly made mistakes in ifstatements (cont.)
• Example output:
Enter an integer value: 4
Input value = 4
It's absolute value = -4 (Because a = -a; is always executed)
Some commonly made mistakes in ifstatements (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/I
f3.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac If3.java
• To run:
java If3
Some commonly made mistakes in ifstatements (cont.)
• Common error 2: forgetting to use statement block in the
then-part
• Example:
import java.util.Scanner;
public class If2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
Some commonly made mistakes in ifstatements (cont.)
x1 = x2 = 0;
// Default solution
/* -----------------------------------------------------Only compute solution if determinant b*b - 4*a*c >= 0
------------------------------------------------------ */
if ( b*b - 4*a*c >= 0 )
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
Some commonly made mistakes in ifstatements (cont.)
• Structure diagram of this erroneous program:
Some commonly made mistakes in ifstatements (cont.)
• Notes:
• The then-part contains only the first of the 2 statements !!
(Remember: the syntax rule says that the then-part consists
of only one statement !!)
• The second assignment statement is not part of the thenpart !!!
(Therefore, the second assignment statement is always
executed - and it will cause an error)
Some commonly made mistakes in ifstatements (cont.)
• Example output: (use: a = 1, b = 1, c = 1)
x1 = 0.0
x2 = NaN
<----- x2 was computed with sqrt(neg. value)
Some commonly made mistakes in ifstatements (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/I
f4.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac If4.java
• To run:
java If4