The way to end this loop:
while (x != -1) {
System.out.print("Enter -1 to stop"); x = scan.nextLine();
}
User Input
What is the first and last numbers
int count = 4;
while (count <= 7) {
count++;
System.out.print(count + " ");
}
This code do this for strings.
for(int i = 0; i < str.length(); i++) { System.out.print(str.substring(i,i+1));
}
iterate through the characters
Loop within a loop
Nested Loops
This code does this for strings:
int count = 0;
for(int i = 0; i < str.length(); i++){ if(str.substring(i,i+1).equals("e")){
count++;
}
}
Count the number of times the letter "e" appears in a string
The three parts that composes the for loop?
i = 0;
initializes loop control variable i < 10;
tells loop when to stop i++; tells the loop what to count by
int num = 49; while (num > 0)
{
if (num % 2 == 0)
{ num++;
} else
{ num--;
}
}
Infinite Loop
The following code does what?
String str = "AP-CSA";
for (int i = 0; i < str.length(); i++)
{
if (str.substring(i, i + 1).equals("A"))
{
System.out.print(i + " ");
}
}
This can only be access within the body which it was declare.
Local scoping
This code does this to a string:
String str = "loop";
String newStr = "";
for(int i = str.length(); i > 0; i--){
newStr += str.substring(i-1, i);
}
Looping backwards through a string.
This code does what?
int a = 100;
while (a > 1)
{
System.out.println("$");
a /= 2;
}
Prints the number of $
What is the result of the following code:
for (int k = 30; k > 0; k = k - 3) {
if (k % 5 == 0) {
System.out.print(k + " ");
}
}
30 15
Is the purpose of this code.
String str = "loop";
String newStr = "";
for (int i = str.length(); i > 0; i--)
{
newStr += str.substring(i - 1, i);
}
System.out.println(newStr);
Concatenating strings
This can be access everywhere
Global Scoping
Is intended to print the sum of all the odd integers from 0 up to and including 101.
int r = 0;
int sum = 0;
/* missing loop header */
{
if (r % 2 == 1)
{
sum += r;
}
r++;
}
System.out.println(sum);
while (r <= 101)
What is the wrong?
Code Segment I
for (int i = 0; i < 10; i++)
{
System.out.print( "*" );
}
Code Segment II
for (int i = 1; i <= 10; i++)
{
System.out.print( "*" );
}
The output of the code segments is the same because the loops in both code segments iterate 10 times.
purpose of this code.
public int numDigits(int num)
{
int count = 0;
while (num != 0)
{
count++;
num = num / 10;
}
return count;
}
print the number of digits in the parameter num.
This code does what?
String str = "Mississippi";
String newStr = "";
for (int i = 0; i < str.length(); i++)
{
if (!str.substring(i, i + 1).equals("i"))
{
newStr += str.substring(i, i + 1);
}
}
str = newStr;
Removing letter "i"
What is the purpose of this code?
For (int i = 0; i < str.length; i++) {System.out.println(str.substring(i, i+1);}
The result is this for the code segement:
int a = 1;
String result = "";
while (a < 20)
{
result += a;
a += 5;
}
System.out.println(result);
161116
When would it be more beneficial to use a while loop instead of a for loop?
When you need your loop to be controlled by user input.
This code does what?
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += (i * i);
}
System.out.println(sum);
Sum of squares
The code does this.
for (int i=0; i<n; i++)
{
for (int j=n-i; j>1; j--)
{
// printing spaces
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
// printing stars
System.out.print("* ");
}
System.out.println();
}
Print a pyramid
Is the way we measure code efficiency.
Big O notation, counting variable
for (int j = 4; j > 0; j--) {
for (int k = 1; k <= j; k++) {
System.out.print(k + " ");
}
System.out.println();
}
1 2 3 4
1 2 3
1 2
1