Java Unit II - 05-8 Decision Looping

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Sri Vasavi College, Erode

Self-Finance Wing

Department of Computer Science

Online class for the Academic Year 2020-2021


Core 3: JAVA PROGRAMMING

Class 11
Unit II
On 05/08/2020 at 10:45 a.m to 11:30 a.m

Handled by
Ms. S. Anusuya, M.C.A.,M.Phil.,
Assistant Professor, Dept. of Computer Science
Java Programming,
05/08/2020, 10:45 A.M Ms. S. Anusuya, Asst. Professor in Computer 2
Science
Java Programming,
3
05/08/2020, 10:45 A.M Ms. S. Anusuya, Asst. Professor in Computer
Science
The while statement

• A while statement has the following syntax:


while (condition)
statement;
• If the condition is true, the statement is
executed; then the condition is evaluated
again
• The statement is executed over and over
until the condition becomes false
• If the condition of a while statement is
false initially, the statement is never
executed
• Therefore, we say that a while statement
executes zero or more times
While statement flow
diagram
while (condition)
statement;

condition

true

statement
Example - Counter
// Counts from 1 to 5
class Counter {
static final int LIMIT = 5;

public static void main(String[]


args) {
int count = 1;
while (count <= LIMIT) {
System.out.println(count);
count = count + 1;
}
System.out.println(“done”);
}
}
Examples - Factors
// Gets an integer and prints its
factors
class FactorsExample {
public static void main(String[]
args) {
InputRequestor input = new
InputRequestor();
int a = input.requestInt(“Enter a
number:”);
int i = 1;
System.out.println(“The divisors of
“+a+” are:”);
while (i <= a) {
if (a%i == 0) {
System.out.println(i);
}
i = i + 1;
}
}
}
Infinite Loops

• The body of a while loop must


eventually make the condition
false
• If not, it is an infinite loop, which
will execute until the user
interrupts the program
• This is a common type of logical
error -- always double check that
your loops will terminate
normally
Example - Forever
// This program contains an infinite
loop
class Forever {
static final int LIMIT = 25;

public static void main(String[]


args) {
int count = 1;
while (count <= LIMIT) {
System.out.println(count);
count = count - 1;
}
}
}
More Repetition
Constructs
• In addition to while loops,
Java has two other constructs
used to perform repetition:
– the do statement
– the for statement

• Each loop type has its own unique


characteristics
• You must choose which loop type
to use in each situation
The do Statement

• The do statement has the following


syntax:
do
statement
while (condition);
• The statement is executed until the
condition becomes false
• It is similar to a while statement,
except that its termination
condition is evaluated after the loop
body
The do Statement

• The key difference between a do loop


and a while loop is that the body of
the do loop will execute at least once

• If the condition of a while loop is


false initially, the body of the loop is
never executed

• Another way to put this is that a


while loop will execute zero or more
times and a do loop will execute one
or more times
Do Statement Example
// Gets an integer and prints its factors
class AvgExample {
public static void main(String[] args){
InputRequestor input = new
InputRequestor();
double x, sum=0, count=-1;
do {
x = input.RequestDouble(“Next
number:”);
sum += x;
count++;
} while (x != 0);
// 0 is a flag indicating end of
input
System.out.println(“The average
is “+sum/count);
}
}
The do Statement flow
diagram

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) {
statement;
increment;
}
The for Statement:
examples
• Examples:

for (int count=1; count < 75; count+


+) {
System.out.println (count);
}

for (int num=1; num <= max; num =


num * 2) {
System.out.println (“Next power of
2: “ + num);
}
The for Statement

• The initialization is always


performed once
• The condition of a for statement is
tested prior to executing the loop
body (like in the while statement)
• Therefore, a for loop will execute
zero or more times
• For loops are well suited for cases
where the number of iterations is
known beforehand
• The increment is executed after
each iteration of the loop
Omitting parts in a for
Statement
• Each expression in the header of
a for loop is optional
– If the initialization is left out, no
initialization is performed
– If the condition is left out, it is
always considered to be true, and
therefore makes an infinite loop
– If the increment is left out, no
increment operation is performed
• Both semi-colons are always
required
for (;;) {// an infinite loop
System.out.println (“beep”);
}
// compute a value count
for (; count < max ; count ++ ) {
System.out.println (count);
The for Statement flow
diagram
initialization

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 condition is evaluated
Break and Continue
class AvgExample2 {
Example
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);
}
}
Thank You....

Any Queries?…….

Java Programming,
25
05/08/2020, 10:45 A.M Ms. S. Anusuya, Asst. Professor in Computer
Science

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy