C PGM
C PGM
ASSISTANT PROFESSOR
DEPT OF CSE
KIT-KALAIGNARKARUNANIDHI
INSTITUTE OF TECHNOLOGY-CBE.
Conditions and Loops
• The if Statement
The if Statement
• The Java if statement has the following syntax:
if (boolean-condition)
statement;
• If the Boolean condition is true, the statement
is executed; if it is false, the statement is
skipped
• This provides basic decision making capabilities
Tempreture
class Temperature {
static final int THRESHOLD = 65;
condition
true
statement
Boolean Expressions
• The condition of an if statement must evaluate to a
true or false result
• Java Operator
has several equality
Meaningand relational operators:
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
if (boolean-condition){
statement1;
statement2;
…
}
Example - Temperature2
class Temperature2 {
static final int THRESHOLD = 65;
if (condition)
statement1;
else
statement2;
true
condition
statement1 statement2
Nested If statements
• Since an “If” statement is a statement, it can appear inside another
if statement.
if (condition1)
if (condition2)
statement;
if (condition1)
statement1;
else if (condition2)
statement2;
Nested If Example
// Reads 2 integers and compares them
class CompareExample {
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
int a = input.requestInt(“First number:”);
int b = input.requestInt(“Second number:”);
if (a != b){
if (a > b)
System.out.println(a+” is greater”);
else
System.out.println(b+” is greater”);
}else
System.out.println(“the numbers are equal”);
}
}
Checking your Input
• When requesting input from the user, keep in mind that the input may be
invalid.
• It is good practice to check the validity of user input
int numberOfItems =
input.requestInt(“Enter number of items:”);
if (numberOfItems < 0) {
System.out.println(
“Number of items must be positive!”);
} else {
double price = numberOfItems * ITEM_PRICE;
System.out.println(“The total price is:“ +price);
}
Conditions and
Loops
• Boolean Expressions
Logical Operators
• Boolean expressions may be combined using logical
operators
• There are three logical operators
Operator in Java:
Operation
! Logical NOT
&& Logical AND
|| Logical OR
a b a || b
false false false
false true true
true false true
true true true
Logical Operators
• Logical operators are used to form more complex
logical expressions
if (a<1 || a%2!=0)
System.out.println(
“The input should be an even even number!”);
boolean b, c;
b = (x > 17);
• Boolean variables are Boolean expressions
c = b && (x<60);
if (c)
System.out.println(“x is in range”);
Example - RightTriangle
// Receives the length of the edges of a triangle
// and determine if this is a right triangle
class RightTriangle {
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
float a = input.requestInt(“Edge1:”);
float b = input.requestInt(“Edge2:”);
float c = input.requestInt(“Hypotenuse:”);
boolean test = a*a+b*b == c*c;
if (test)
System.out.println(“It’s a right triangle”);
else
System.out.println(“It’s not a right
triangle”);
}
}
conditions and loops
condition
true
statement
Example - Counter
// Counts from 1 to 5
class Counter {
static final int LIMIT = 5;
• More conditionals
The Conditional Operator
• Java has a conditional operator that evaluates a
Boolean condition that determines which of two
expressions is evaluated
• The result of the chosen expression is the result of the
entire conditional operator
• Its syntax is:
condition ? expression1 :
expression2
• If the condition is true, expression1 is evaluated; if it is
false, expression2 is evaluated
The Conditional Operator
• It is similar to an if-else statement, except that
it is an expression that returns a value
int max = (a > b) ? a : b;
• For example:
Shorthand Operators
Shorthand Operators
• Many operations are very commonly used
x = x + 1;
sum = sum + x;
is equivalent to
The Increment and Decrement Operators
is equivalent to
Assignment Operators
• There are many such assignment operators,
always written as op= , such as:
Operator Example Equivalent to
+= x+=y x = x + y
-= x-=y x = x - y
*= x*=y x = x * y
/= x/=y x = x / y
%= x%=y x = x % y
Assignment Operators
• The right hand side of an assignment operator
can be a complete expression
• The entire right-hand expression is evaluated
first, /=
result then combined with the additional
total-MIN;
operation
result = result / (total-MIN);
• Therefore
result /= total-MIN;
is equivalent to
conditions and loops
More Repetition
More Repetition Constructs
• In addition to while loops, Java has two
other constructs used to perform repetition:
– the do statement
– the for statement
statement
true
condition
false
The for Statement
• Many loops have a common pattern, captured
by the for statement
• The syntax of the for loop is
for (intialization; condition;
increment)
statement;
• This is equivalent to
initialization;
while (condition) {
The for Statement: examples
• Examples:
for (int count=1; count < 75; count++) {
System.out.println (count);
}
false
condition
true
statement
increment
Multiplication Table Example
class MultiplicationTable {
public static void main(String[] args){
for(int j=1 ; j <= 10 ; j++) {
for(int k=1 ; k <= 10 ; k++)
System.out.print(j*k);
System.out.println();
}
}
}
The break and continue
statements
• The break statement, which we used with
switch statements, can also be used inside a
loop
• When the break statement is executed,
control jumps to the statement after the loop
(the condition is not evaluated again)
• A similar construct, the continue
statement, can also be executed in a loop
• When the continue statement is executed,
control jumps to the end of the loop and the
Break and Continue Example
class AvgExample2 {
public static void main(String[] args){
InputRequestor in = new InputRequestor();
double x, sum = 0; count = 0;
while(true){
x = in.RequestDouble();
if (x == 0)
break;
if (x < 0) {
System.out.println(“Only positive numbers!”);
continue;
}
sum += x ;
count ++ ;
} // continued on next page
Break and Continue Example (2)
System.out.println(“The average is “+sum/count);
}
}
Why do
class Mystery {
We Need Indentation?
public static void main(String[] args) {
InputRequestor in = new InputRequestor();
int dimension =
in.requestInt(“Please enter the dimension”);