Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while...
Transcript of Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while...
![Page 1: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/1.jpg)
Loops (While and For)
CSE 1310 – Introduction to Computers and Programming
Vassilis Athitsos
University of Texas at Arlington
1
![Page 2: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/2.jpg)
Motivation for Loops - First Example
2
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter the radius: ");
double radius = in.nextDouble();
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.pow(radius, 2);
System.out.printf("The circumference is %.2f.\n", circumference);
System.out.printf("The area is %.2f.\n", area);
}
}
• We have written a program for calculating the area and circumference of a circle. – Problem: we need to re-run the program every time we want to
compute values for a new radius.
– The user should be able to keep entering values, as long as they want.
![Page 3: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/3.jpg)
Motivation - A Second Example
• Suppose we want to write programs that ask the user to input an integer N and then do one (or more) of the following:
– Print out all integers between 0 and N.
– Figure out if N is a prime number.
– Print out all prime numbers between 1 and N.
– …
• The elements of Java that we have covered so far are not sufficient for writing such programs.
• What is missing: the ability to repeat some instructions as many times as we want.
![Page 4: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/4.jpg)
Example of a while loop: Printing Numbers from 1 to N
4
• Write a program that:
– Asks the user to enter an integer N.
– Prints all integers from 1 to N.
![Page 5: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/5.jpg)
5
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
![Page 6: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/6.jpg)
while loops
• A while loop is defined as follows:
while (condition)
{
line 1
line 2
…
line n
}
• condition is a boolean expression (that can be equal to true or false).
• Line 1, line 2, …, line n are called the body of the while loop. 6
![Page 7: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/7.jpg)
while loops
• A while loop is defined as follows:
while (condition)
{
line 1
line 2
…
line n
}
• Meaning: as long as condition is true, keep executing the body of the loop (lines 1, …, n).
7
![Page 8: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/8.jpg)
while loop execution while (condition)
{
line 1
line 2
…
line n
}
first line after loop
• This is how a while loop gets executed: – Step 1: evaluate condition.
– Step 2: If condition is false, go to the first line after the loop.
– Step 3: If condition is true, execute the body of the while loop, and go back to step 1. 8
![Page 9: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/9.jpg)
9
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
![Page 10: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/10.jpg)
10
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
What is the condition for this while loop?
![Page 11: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/11.jpg)
11
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
What is the condition for this while loop? i <= N
![Page 12: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/12.jpg)
12
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
What is the body of this while loop?
![Page 13: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/13.jpg)
13
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
What is the body of this while loop? The lines shown in red on this slide. (Everything between the curly braces under the while line).
![Page 14: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/14.jpg)
14
Example of a while loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
i = i+1;
}
System.out.printf("done with the while loop.\n");
}
}
Please enter an integer: 5
1
2
3
4
5
done with the while loop.
![Page 15: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/15.jpg)
15
Common Bug: Infinite Loop
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
}
System.out.printf("done with the while loop.\n");
}
}
What is wrong with this code?
![Page 16: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/16.jpg)
16
Common Bug: Infinite Loop
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
}
System.out.printf("done with the while loop.\n");
}
}
What is wrong with this code? We do not change the value of i inside the loop. Thus, i will always be 1, and the loop (and program) will never terminate. This is called an infinite loop.
![Page 17: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/17.jpg)
17
Common Bug: Infinite Loop
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = 1;
while (i <= N)
{
System.out.printf("%d\n", i);
}
System.out.printf("done with the while loop.\n");
}
}
To quit an infinite loop in NetBeans: Select Run->Stop Build/Run If you do not do that, you will not be able to run your (corrected) program again.
![Page 18: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/18.jpg)
Designing a while loop
• When you design a while loop, you need to make sure that the loop will terminate exactly when needed, not before, and not after.
• You will need to define a boolean condition, that determines exactly when to stay in the loop and when to exit.
• You need to update variables within the body of the loop, as needed, to make sure that the boolean condition will evaluate to the right thing.
18
![Page 19: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/19.jpg)
Example of a while loop: Determining if an Integer N is Prime
19
• Write a program that:
– Asks the user to enter an integer N.
– Prints whether N is prime.
![Page 20: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/20.jpg)
Example of a while loop: Determining if an Integer N is Prime
20
• Write a program that:
– Asks the user to enter an integer N.
– Prints whether N is prime.
• Strategy for determining if N is prime:
![Page 21: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/21.jpg)
Example of a while loop: Determining if an Integer N is Prime
21
• Write a program that:
– Asks the user to enter an integer N.
– Prints whether N is prime.
• Strategy for determining if N is prime:
– For every number K between 2 and N-1, check if K divides N.
![Page 22: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/22.jpg)
Example of a while loop: Determining if an Integer N is Prime
22
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
Are we done?
![Page 23: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/23.jpg)
Example of a while loop: Determining if an Integer N is Prime
23
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
Are we done? No, because the code does not have anything for figuring out if N is prime. However, writing code like this is a very useful strategy: • Start with parts of the
code that need to be there for sure.
• Then, start adding pieces that are missing.
![Page 24: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/24.jpg)
Example of a while loop: Determining if an Integer N is Prime
24
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
VERY IMPORTANT TIP:
(you should be doing it throughout this course):
If there is a place in your code where you need some information that you don't have:
• Create a variable.
• Write code so that this variable has the information you need, at the point where you need it.
![Page 25: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/25.jpg)
Example of a while loop: Determining if an Integer N is Prime
25
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
Here, the information we need is:
![Page 26: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/26.jpg)
Example of a while loop: Determining if an Integer N is Prime
26
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
??? N_is_prime;
if (N_is_prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
Here, the information we need is: is N prime? So, we need to create a variable. Let's call it N_is_prime What is the type?
![Page 27: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/27.jpg)
Example of a while loop: Determining if an Integer N is Prime
27
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
??? N_is_prime;
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
ANOTHER IMPORTANT TIP:
(you should be doing it throughout this course):
To figure out what type a variable should be: Think about all possible values that this variable should be able to take, to handle all cases that you care about.
![Page 28: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/28.jpg)
Example of a while loop: Determining if an Integer N is Prime
28
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean N_is_prime;
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
N_is_prime can take values true or false. Therefore, N_is_prime should be of type boolean.
![Page 29: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/29.jpg)
Example of a while loop: Determining if an Integer N is Prime
29
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean N_is_prime;
// CODE NEEDED HERE.
if (N is prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d is not prime.\n", N);
}
}
}
N_is_prime can take values true or false. Therefore, N_is_prime should be of type boolean. Next: writing code to make sure N_is_prime has the right value where we need it.
![Page 30: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/30.jpg)
30
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N:
");
int N = in.nextInt();
boolean N_is_prime = true;
int i = 2;
while (i < N)
{
if (N % i == 0) {
N_is_prime = false;
}
i++;
}
if (N_is_prime) {
System.out.printf("%d is prime.\n",
N);
}
else {
System.out.printf("%d is not
prime.\n", N);
}
}
}
• Key elements of the solution: – Initial value of N_is_prime:
should it be true or false?
– In the loop, when and how do we change the value of N_is_prime?
• This is the classic smoking gun problem (we will see MANY such problems). – To prove that N is prime, we
must make sure that NO i divides N.
– To prove that N is NOT prime, it is sufficient to find ONE i that divides N.
– If we find an i that divides N, we call that i the SMOKING GUN: i is the proof that N is not prime.
![Page 31: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/31.jpg)
31
• When a boolean value depends on a smoking gun: – Initialize the boolean variable
to the value it should get if we find no smoking gun.
– Do a loop, where you test all possible smoking guns. If you find a smoking gun, flip the value of the boolean variable.
• Mishandling smoking gun problems is (unfortunately) a very common mistake in this course.
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N:
");
int N = in.nextInt();
boolean N_is_prime = true;
int i = 2;
while (i < N)
{
if (N % i == 0) {
N_is_prime = false;
}
i++;
}
if (N_is_prime) {
System.out.printf("%d is prime.\n",
N);
}
else {
System.out.printf("%d is not
prime.\n", N);
}
}
}
![Page 32: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/32.jpg)
32
• A classic mistake in smoking gun problems: – Setting the Boolean variable
at every iteration.
• Why is this a mistake?
// This code is incorrect
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean N_is_prime = true;
int i = 2;
while (i < N)
{
if (N % i == 0) {
N_is_prime = false;
}
else {
N_is_prime = true;
}
i++;
}
if (N_is_prime) {
System.out.printf("%d is prime.\n", N);
}
else {
System.out.printf("%d not prime.\n", N);
}
}
} // This code is incorrect
![Page 33: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/33.jpg)
33
• A classic mistake in smoking gun problems: – Setting the Boolean variable
at every iteration.
• Effectively, this makes the entire loop useless. – At the end, the Boolean
variable will be set at the last iteration.
– The calculations of all previous iterations are ignored.
• The code on the left is incorrect, illustrates this classic mistake. – N_is_prime is set to true
every time N % i != 0.
// This code is incorrect
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean N_is_prime = true;
int i = 2;
while (i < N)
{
if (N % i == 0) {
N_is_prime = false;
}
else {
N_is_prime = true;
}
i++;
}
if (N_is_prime) {
System.out.printf("%d is prime.\n", N);
}
else {
System.out.printf("%d not prime.\n", N);
}
}
} // This code is incorrect
![Page 34: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/34.jpg)
34
• Another classic mistake in smoking gun problems: – Declaring the Boolean
variable within the body of the loop.
• Why is this a mistake?
// This code is incorrect
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
int i = 2;
while (i < N)
{
boolean N_is_prime = true;
if (N % i == 0)
{
N_is_prime = false;
}
i++;
}
if (N_is_prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d not prime.\n", N);
}
}
} // This code is incorrect
![Page 35: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/35.jpg)
35
• Another classic mistake in smoking gun problems: – Declaring the Boolean
variable within the body of the loop.
• If you make that mistake, Java will give you an error here: – If your variable has been
declared inside the loop, then it is not defined outside the loop.
• The code on the left is incorrect, illustrates this classic mistake.
// This code is incorrect
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
int i = 2;
while (i < N)
{
boolean N_is_prime = true;
if (N % i == 0)
{
N_is_prime = false;
}
i++;
}
if (N_is_prime)
{
System.out.printf("%d is prime.\n", N);
}
else
{
System.out.printf("%d not prime.\n", N);
}
}
} // This code is incorrect
![Page 36: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/36.jpg)
Example of a for loop: Printing Numbers from 1 to N
36
• Write a program that:
– Asks the user to enter an integer N.
– Prints all integers from 1 to N.
![Page 37: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/37.jpg)
37
Example of a for loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 1; i <= N; i++)
{
System.out.printf("%d\n", i);
}
System.out.printf("done with the for loop.\n");
}
}
![Page 38: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/38.jpg)
for loops
• A for loop can be defined as follows (note: this definition will be extended when we talk about lists).
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
• Line 1, line 2, …, line n are called the body of the for loop.
38
![Page 39: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/39.jpg)
for loops
• A for loop can be defined as follows (note: this definition will be extended when we talk about lists).
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
• The condition is a boolean expression, that typically compares var to some value.
• E.g.: var <= N. 39
![Page 40: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/40.jpg)
for loops
• A for loop can be defined as follows (note: this definition will be extended when we talk about lists).
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
• The update typically changes the value of var.
• Most common case: var++.
• Another example: var = var - 3 40
![Page 41: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/41.jpg)
for loop execution
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• This is how a for loop gets executed: – Step 1: var = init_value;
– Step 2: If condition is false, go to first line after the loop .
– Step 3: execute the body of the loop (lines 1 to n).
– Step 4: execute the update, and go to step 2.
41
![Page 42: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/42.jpg)
42
Example of a for loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 1; i <= N; i++)
{
System.out.printf("%d\n", i);
}
System.out.printf("done with the for loop.\n");
}
}
What is the condition for this for loop? What is the update for this for loop? What is the body for this for loop?
![Page 43: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/43.jpg)
43
Example of a for loop: Printing Numbers from 1 to N
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 1; i <= N; i++)
{
System.out.printf("%d\n", i);
}
System.out.printf("done with the for loop.\n");
}
}
What is the condition for this for loop? i <= N What is the update for this for loop? i++ What is the body for this for loop? The printf line.
![Page 44: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/44.jpg)
Update Without ++: An Example
44
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 0; i <= N; i += 13)
{
System.out.printf("%d\n", i);
}
System.out.printf("printed all numbers between 0 and %d\n", N);
System.out.printf("that are divisible by 13.\n", N);
}
}
Please enter an integer: 30
Example output:
![Page 45: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/45.jpg)
Update Without ++: An Example
45
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 0; i <= N; i += 13)
{
System.out.printf("%d\n", i);
}
System.out.printf("printed all numbers between 0 and %d\n", N);
System.out.printf("that are divisible by 13.\n", N);
}
}
Please enter an integer: 30
0
13
26
printed all numbers between 0 and 30
that are divisible by 13.
Example output:
![Page 46: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/46.jpg)
Counting Downwards: An Example
46
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = N; i >= 0; i -= 2)
{
System.out.printf("%d\n", i);
}
System.out.printf("Counting down %d to 0, with step 2.\n", N);
}
}
Please enter an integer: 5
Example output:
![Page 47: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/47.jpg)
Counting Downwards: An Example
47
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = N; i >= 0; i -= 2)
{
System.out.printf("%d\n", i);
}
System.out.printf("Counting down %d to 0, with step 2.\n", N);
}
}
Please enter an integer: 5
5
3
1
Counting down 5 to 0, with step 2.
Example output:
![Page 48: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/48.jpg)
for Loop With a String: Example 1
48
• Write a program that:
– Asks the user to enter a word.
– Prints each letter of that word on a separate line.
![Page 49: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/49.jpg)
for Loop With a String: Example 1
49
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
for (int i = 0; i < word.length(); i++)
{
System.out.printf("%s\n", word.charAt(i));
}
}
}
Please enter a word: hello
Example output:
![Page 50: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/50.jpg)
for Loop With a String: Example 1
50
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
for (int i = 0; i < word.length(); i++)
{
System.out.printf("%s\n", word.charAt(i));
}
}
}
Example output:
Please enter a word: hello
h
e
l
l
o
![Page 51: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/51.jpg)
while Loop Version
51
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int i = 0;
while (i < word.length())
{
System.out.printf("%s\n", word.charAt(i));
i++;
}
}
}
Example output:
Please enter a word: hello
h
e
l
l
o
![Page 52: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/52.jpg)
for Loop With a String: Example 2
52
• Write a program that:
– Asks the user to enter a word.
– Starting from the first letter, it prints every other letter of the word. The letters should be printed on the same line, not one per line.
– For example, for "Sunday" it should print "Sna".
![Page 53: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/53.jpg)
for Loop With a String: Example 2
53
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
for (int i = 0; i < word.length(); i+=2)
{
System.out.printf("%s\n", word.charAt(i));
}
}
}
Please enter a word: Sunday
Example output:
![Page 54: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/54.jpg)
for Loop With a String: Example 2
54
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
for (int i = 0; i < word.length(); i+=2)
{
System.out.printf("%s\n", word.charAt(i));
}
}
}
Please enter a word: Sunday
S
n
a
Example output:
Not what we want. We want all letters on the same line, like "Sna".
![Page 55: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/55.jpg)
for Loop With a String: Example 2
55
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
for (int i = 0; i < word.length(); i+=2)
{
System.out.printf("%s", word.charAt(i));
}
System.out.printf("\n");
}
}
Please enter a word: Sunday
Sna
Example output:
If we remove \n from the printf, it works. We just need to print a new line at the end of the program.
![Page 56: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/56.jpg)
while Loop Version
56
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int i = 0;
while (i < word.length())
{
System.out.printf("%s", word.charAt(i));
i += 2;
}
System.out.printf("\n");
}
}
Please enter a word: Sunday
Sna
Example output:
![Page 57: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/57.jpg)
for Loop With a String: Example 3
57
• Write a program that:
– Asks the user to enter a word.
– Prints the letters of the string backwards. The letters should be printed on the same line, not one per line.
– For example, for "Sunday" it should print "yadnuS".
![Page 58: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/58.jpg)
for Loop With a String: Example 3
58
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
for (int i = word.length() - 1; i >= 0; i--)
{
System.out.printf("%s", word.charAt(i));
}
System.out.printf("\n");
}
}
Please enter a word: Sunday
yadnuS
Example output:
(Very) important things: • initial value of i. • terminating condition. • update.
![Page 59: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/59.jpg)
while Loop Version
59
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int i = word.length() - 1;
while (i >= 0)
{
System.out.printf("%s", word.charAt(i));
i--;
}
System.out.printf("\n");
}
}
Please enter a word: Sunday
yadnuS
Example output:
![Page 60: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/60.jpg)
for Loop With a String: Example 4
60
• Write a program that:
– Asks the user to enter a word.
– Counts the number of times the letter 'a' appears in the word.
![Page 61: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/61.jpg)
for Loop With a String: Example 4
61
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if (c == 'a')
{
counter++;
}
}
System.out.printf("The letter a occurs %d
times.\n", counter);
}
}
Please enter a word: January
The letter a occurs 2 times.
Example output:
![Page 62: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/62.jpg)
for Loop With a String: Example 4
62
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if (c == 'a')
{
counter++;
}
}
System.out.printf("The letter a occurs %d
times.\n", counter);
}
}
• This is the classic counter problem (we will see MANY such problems). – We must count how many
times something happens.
![Page 63: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/63.jpg)
for Loop With a String: Example 4
63
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if (c == 'a')
{
counter++;
}
}
System.out.printf("The letter a occurs %d
times.\n", counter);
}
}
• To solve the counter problem: – Initialize a counter
variable to 0, before the loop.
– Do a loop, where you increment the counter every time you find what you are looking for.
![Page 64: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/64.jpg)
for Loop With a String: Example 4
64
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String word = in.next();
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
if (c == 'a')
{
counter++;
}
}
System.out.printf("The letter a occurs %d
times.\n", counter);
}
}
IMPORTANT NOTE:
• To test characters for equality, you use ==.
• To test strings for equality you use the equals method.
![Page 65: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/65.jpg)
The break statement
• The break statement forces termination of the current while loop or for loop.
• Example: print the first number >= N that is divisible by 13.
65
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = N;
while(true) {
if (i % 13 == 0) {
System.out.printf("%d is the first integer >= %d that is
divisible by 13.\n", i, N);
break;
}
i++;
}}}
![Page 66: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/66.jpg)
66
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
int i = N;
while(true) {
if (i % 13 == 0) {
System.out.printf("%d is the first integer >= %d that is
divisible by 13.\n", i, N);
break;
}
i++;
}}}
Please enter an integer: 62
65 is the first integer >= 62 that is divisible by 13.
Example output:
![Page 67: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/67.jpg)
break
while (condition)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a break within the body of the while loop.
• What line of code will be executed next?
67
![Page 68: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/68.jpg)
break
while (condition)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a break within the body of the while loop.
• What line of code will be executed next?
– The first line after the loop. 68
![Page 69: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/69.jpg)
break
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a break within the body of the for loop.
• What line of code will be executed next?
69
![Page 70: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/70.jpg)
break
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a break within the body of the for loop.
• What line of code will be executed next?
– The first line after the loop. 70
![Page 71: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/71.jpg)
break
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a break within the body of the for loop.
• What if there is no first line after the loop?
71
![Page 72: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/72.jpg)
break
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a break within the body of the for loop.
• What if there is no first line after the loop?
– The program will just terminate. 72
![Page 73: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/73.jpg)
The continue statement
• The continue statement skips the rest of the body of the loop and goes directly to the next iteration (or to termination).
• Example: print numbers between 1 and N that are divisible by 13.
73
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 1; i <= N; i++)
{
if (i % 13 != 0)
{
continue;
}
System.out.printf("%d\n", i);
}}}
Please enter an integer: 50
Example output:
![Page 74: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/74.jpg)
The continue statement
• The continue statement skips the rest of the body of the loop and goes directly to the next iteration (or to termination).
• Example: print numbers between 1 and N that are divisible by 13.
74
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter an integer: ");
int N = in.nextInt();
for (int i = 1; i <= N; i++)
{
if (i % 13 != 0)
{
continue;
}
System.out.printf("%d\n", i);
}}}
Please enter an integer: 50
13
26
39
Example output:
![Page 75: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/75.jpg)
continue
while (condition)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a continue within the body of the while loop.
• What line of code will be executed next?
75
![Page 76: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/76.jpg)
continue
while (condition)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a continue within the body of the while loop.
• What line of code will be executed next?
– condition 76
![Page 77: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/77.jpg)
continue
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a continue within the body of the for loop.
• What will happen next?
77
![Page 78: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/78.jpg)
continue
for (int var = init_value; condition; update)
{
line 1
line 2
…
line n
}
first line after loop
• Suppose that we execute a continue within the body of the for loop.
• What will happen next?
– Execute the update.
– Check the condition, and loop again or exit the loop. 78
![Page 79: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/79.jpg)
Nested Loops
• A loop can be part of another loop. Such a loop is called a nested loop.
• Example 1: Print out the 10x10 multiplication table.
79
![Page 80: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/80.jpg)
Nested Loops
• A loop can be part of another loop. Such a loop is called a nested loop.
• Example 1: Print out the 10x10 multiplication table.
80
public class example1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
System.out.printf("%d ", i*j);
}
System.out.printf("\n");
}
}
}
Code, version 1:
![Page 81: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/81.jpg)
Nested Loops
• A loop can be part of another loop. Such a loop is called a nested loop.
• Example 1: Print out the 10x10 multiplication table.
81
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Output, version 1:
Any problem?
![Page 82: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/82.jpg)
Nested Loops
• A loop can be part of another loop. Such a loop is called a nested loop.
• Example 1: Print out the 10x10 multiplication table.
82
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Output, version 1:
Any problem? The output is correct, but the numbers are not aligned nicely.
![Page 83: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/83.jpg)
Nested Loops
• A loop can be part of another loop. Such a loop is called a nested loop.
• Example 1: Print out the 10x10 multiplication table.
83
public class example1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
System.out.printf("%3d ", i*j);
}
System.out.printf("\n");
}
}
}
Code, version 2:
![Page 84: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/84.jpg)
Nested Loops
• A loop can be part of another loop. Such a loop is called a nested loop.
• Example 1: Print out the 10x10 multiplication table.
84
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Output, version 2:
![Page 85: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/85.jpg)
Break and Continue in Nested Loops
for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
...
}
first line after loop2
...
}
first line after loop1
• Suppose some break line belongs to multiple loops.
• If that break line is executed, what line of code do we go to?
85
![Page 86: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/86.jpg)
Break and Continue in Nested Loops
for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
...
}
first line after loop2
...
}
first line after loop1
• Suppose some break line belongs to multiple loops.
• If that break line is executed, what line of code do we go to?
– The first line after the innermost loop containing the break . 86
![Page 87: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/87.jpg)
Break and Continue in Nested Loops
for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
break;
...
}
first line after loop2
...
}
first line after loop1
• What line is executed after the break in this example?
87
![Page 88: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/88.jpg)
Break and Continue in Nested Loops
for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
break;
...
}
first line after loop2
...
}
first line after loop1
• The innermost loop that the break belongs to is loop 2.
• The next line is the first line after loop 2 (shown in green). 88
![Page 89: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/89.jpg)
Break and Continue in Nested Loops
for (int var1 = init1; condition1; update1) // start of loop1
{
...
break;
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
}
first line after loop2
...
}
first line after loop1
• What line is executed after the break in this example?
89
![Page 90: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/90.jpg)
Break and Continue in Nested Loops
for (int var1 = init1; condition1; update1) // start of loop1
{
...
break;
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
}
first line after loop2
...
}
first line after loop1
• The innermost loop that the break belongs to is loop 1.
• The next line is the first line after loop 1 (shown in green). 90
![Page 91: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/91.jpg)
Break and Continue in Nested Loops for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
...
}
first line after loop2
...
}
first line after loop1
• Suppose some continue line belongs to multiple loops.
• If that continue line is executed, what line of code do we go to?
91
![Page 92: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/92.jpg)
Break and Continue in Nested Loops for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
...
}
first line after loop2
...
}
first line after loop1
• Suppose some continue line belongs to multiple loops.
• If that continue line is executed, what line of code do we go to?
– The first line of the innermost loop containing the continue. 92
![Page 93: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/93.jpg)
Break and Continue in Nested Loops for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
continue;
...
}
first line after loop2
...
}
first line after loop1
• What happens after continue in this example?
93
![Page 94: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/94.jpg)
Break and Continue in Nested Loops for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
continue;
...
}
first line after loop2
...
}
first line after loop1
• The innermost loop that continue belongs to is loop 2.
• After continue, Java executes update2 and condition2. 94
![Page 95: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/95.jpg)
Break and Continue in Nested Loops for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
}
first line after loop2
...
continue;
...
}
first line after loop1
• What happens after continue in this example?
95
![Page 96: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/96.jpg)
Break and Continue in Nested Loops for (int var1 = init1; condition1; update1) // start of loop1
{
...
for (int var2 = init2; condition2; update2) // start of loop2
{
...
}
first line after loop2
...
continue;
...
}
first line after loop1
• The innermost loop that continue belongs to is loop 1.
• After continue, Java executes update1 and condition1. 96
![Page 97: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/97.jpg)
Example 1
97
public class example1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (j > i)
{
break;
}
System.out.printf("%3d ", i*j);
}
System.out.printf("\n");
}
}
}
What will this program do?
![Page 98: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/98.jpg)
Example 1
98
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
Output
![Page 99: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/99.jpg)
Example 2
99
public class example1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
System.out.printf("%3d ", i*j);
}
System.out.printf("\n");
if (i == 5)
{
break;
}
}
}
}
What will this program do?
![Page 100: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/100.jpg)
Example 2
100
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
Output
![Page 101: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/101.jpg)
The Circle Program, Revisited.
• It would be nice if the user could input multiple values (and see multiple results) without having to rerun the program.
• This is the previous version. How can we change it?
101
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter the radius: ");
double radius = in.nextDouble();
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.pow(radius, 2);
System.out.printf("The circumference is %.2f.\n", circumference);
System.out.printf("The area is %.2f.\n", area);
}
}
![Page 102: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/102.jpg)
The Circle Program, Revisited.
• First take: an infinite loop.
• Any room for improvement?
102
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true)
{
System.out.printf("Enter the circle radius: ");
double radius = in.nextDouble();
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.pow(radius, 2);
System.out.printf("Circumference = %.2f.\n", circumference);
System.out.printf("Area = %.2f.\n\n", area);
}
}
}
![Page 103: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/103.jpg)
The Circle Program, Revisited.
• First take: an infinite loop.
• Any room for improvement? User has no way to quit.
103
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true)
{
System.out.printf("Enter the circle radius, or -1 to quit: ");
double radius = in.nextDouble();
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.pow(radius, 2);
System.out.printf("Circumference = %.2f.\n", circumference);
System.out.printf("Area = %.2f.\n\n", area);
}
}
}
![Page 104: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/104.jpg)
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true)
{
System.out.printf("Enter the circle radius, or -1 to quit: ");
double radius = in.nextDouble();
if (radius == -1)
{
System.out.printf("\nExiting...\n");
break;
}
double circumference = 2 * Math.PI * radius;
double area = Math.PI * Math.pow(radius, 2);
System.out.printf("Circumference = %.2f.\n", circumference);
System.out.printf("Area = %.2f.\n\n", area);
}
}
}
• Second take: an infinite loop, with quit option.
• Any room for improvement?
![Page 105: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/105.jpg)
Enter the circle radius, or -1 to quit: 1
Circumference = 6.28.
Area = 3.14.
Enter the circle radius, or -1 to quit: 2.3
Circumference = 14.45.
Area = 16.62.
Enter the circle radius, or -1 to quit: -1
Exiting...
Example Output 1
• Second take: an infinite loop, with quit option.
• Any room for improvement?
![Page 106: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/106.jpg)
Enter the circle radius, or -1 to quit: 5,2
Exception in thread "main"
java.util.InputMismatchException
at
java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at
java.util.Scanner.nextDouble(Scanner.java:24
13)
at example1.main(example1.java:9)
Java Result: 1
Example Output 2
• Second take: an infinite loop, with quit option.
• Any room for improvement?
![Page 107: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/107.jpg)
Enter the circle radius, or -1 to quit: 5,2
Exception in thread "main"
java.util.InputMismatchException
at
java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at
java.util.Scanner.nextDouble(Scanner.java:24
13)
at example1.main(example1.java:9)
Java Result: 1
Example Output 2
• Second take: an infinite loop, with quit option.
• Any room for improvement?
• Would be nice to not crash when the input is not valid.
• In general: programs need input validation. – That will be our next topic in this course.
![Page 108: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/108.jpg)
Detour: Random Numbers
• To generate a random number:
– At the beginning of your java code, you should use this import statement:
import java.util.*;
– Once in your program, you should do:
Random rand = new Random();
– Then, to get a random integer from 0 up to (and including) MAX, you should call:
int random_pick = rand.nextInt(MAX+1); 108
![Page 109: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/109.jpg)
Guessing a Number
• Write a program that:
– Picks a random number from 0 up to and including 100.
– Gets in a loop where: • The user is asked to guess the number.
• If the user guesses correctly, the program terminates.
• If not, the system tells the user if the correct answer is higher or lower than the guess.
109
![Page 110: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/110.jpg)
110
import java.util.*;
public class guessing_game {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
int pick = rand.nextInt(101); // Between 0 and 100.
int attempt = 1;
while (true)
{
System.out.printf("Try %d: Guess the number: ", attempt);
int guess = in.nextInt();
if (guess == pick)
{
System.out.printf("Correct!!!\n");
break;
}
else if (guess < pick)
{
System.out.printf("Go higher.\n");
}
else
{
System.out.printf("Go lower.\n");
}
attempt++; }}}
![Page 111: Loops (While and For)vlm1.uta.edu/~athitsos/courses/cse1310_fall2015/lectures/08a_loop… · while loop execution while (condition) { line 1 line 2 … line n } first line after loop](https://reader036.fdocuments.us/reader036/viewer/2022070915/5fb5fb8d1d05805cdc4c18be/html5/thumbnails/111.jpg)
Example Programs
• Summing integers from 1 to N, and variations.
– Summing squares.
– Summing multiples of 7.
– Summing primes.
• Printing divisors of a number.
• Removing spaces, dashes, parentheses from a phone number (or a credit card number).
• Printing a pyramid using the * character.
111