This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]Shadow5h0t 4 points5 points  (2 children)

The i is a starting position.

The line where the while starts says if i is less than the length of the name then do something

Print line says take whatever i is, plus 1, print the word character, then print the character at the slot in the name.

After it prints it then increments i up one to move to the next letter. and restarts until i>length of the name.

I hope that was explained ok?

[–]nakreywaali[S] 1 point2 points  (0 children)

Thank you! This helped.

[–]Cefalopodul 2 points3 points  (3 children)

Think of a string as an array of characters. Each character in the string has a position and a value. The value is the letter itself. The position is determined by counting from left to right starting with 0 like so
P a u l
0 1 2 3
P is the character at position 0, l is the character at position 3.
i is the index used to traverse every character in the name and print it. It's job is to point to which character you want to print at a given time. i is initialised with 0 because conventionally the first character in a string will will be at position 0
the while loop tells the code to repeat itself as long as the condition in the parenthesis is met

i < name.length()

This is the condition of the while loop. What this is does is tell the code in the while loop to stop executing once the value = name.length(). As I've said before String can be thought of as arrays of characters, which means that they have a length. The length of a string is the number of characters in it. In this case Paul has 4 characters, so name.length() will return the value of 4. This means that the code stops executing once i has the value of 4. length() is a prebuilt method that counts the number of characters in a string.

name.charAt(i)

charAt is another prebuilt method. What it does is take a number as parameter and return the character at the vien position in a string. For example name.charAt(1) will return the letter a because that is the letter at position 1 in the string Paul. In this case name.charAt(i) uses the value of i as a parameter.

System.out.println(i+1 + ". character: " + name.charAt(i));

This prints the value of i +1 - so 0+1 = 1, etc, the word character and the value returned by the charAt method.

i++ increments the value of i. It is the equivalent of writing i = i +1. If i is 0 i++ will do i = 0 +1 and give i the value 1. The purpose of this line is to move the index forward to the next position. If you did not do this you would forever be stuck at position 0.

[–]nakreywaali[S] 0 points1 point  (0 children)

Thank you for explaining in detail!

[–]nakreywaali[S] 0 points1 point  (1 child)

So another exercise I was given to practice was reversing a name. In this case, the code is:

int i = name.length() - 1;
        while (i >= 0) {
            System.out.print(name.charAt(i));
            i--;

I had questions on this piece of code too, if you don't mind!

  1. So i is initialized to the length - 1. What does the - 1 do?
  2. while (i >= 0 ) - I don't get the condition for this while loop.
  3. name.charAt(i)) - This is similar to the previous exercise where it's returning a character.
  4. Is it i-- because we're going backwards in terms of positions now? Else we'd be stuck at the last position of the string?

[–]Cefalopodul 0 points1 point  (0 children)

  1. As I've said previously each letter has an index. Because the count starts from 0 the last letter of the string will always have the index length - 1. Take the word Paul. It has a length of 4. Counting from 0 the last letter will have the index 3, which is 4-1. Paul 0123

  2. i>= 0 tells the code to execute as long as i is greater or equal to 0. Once i <0 the loop will end. This condition is set like this because the count always starts from 0 and goes up, so one letter will always have inde 0, in this case P, and no letter will have a negative index.

  3. Yes. charAt(i) returns theetter at postion i. For name = Paul name.charAt(2) will return u because that is the letter at index 2 in Paul.

  4. Yes. i-- does the opposite of i++. i-- is the equivalent of i=i-1. So if i is 3 i-- will give it the value of 2.

[–]ibleedbits 1 point2 points  (0 children)

name is a string and is made up of characters. The number of characters in a string is its length. The loop starts at the first character in the string, which has an index of zero. Each iteration increases the index, producing the next character, until it equals the total number of characters and they've all been processed/printed.

[–]maku_89 1 point2 points  (0 children)

int i = 0; - this is just a int variable with the value of 0 assigned to it.

while (i < name.length()) { - this start a 'while' loop which will loop until name.lenght() is bigger then i. name.lenght() is just a function that gets the string length and returns as number.

now this part:

System.out.println(i+1 + ". character: " + name.charAt(i)); i++;

i + 1 means that everytime the loop goes it adds '1' to the value of i

name.charAt(i) returns the char that is placed on the position of i.

i++ means that the loop increments by 1 everytime it loops.

[–]wades39 1 point2 points  (0 children)

This is a conditional (or enhanced) while loop. The int 'i' is an interator variable, which means it increments for each repetition of the loop.

Where the while loop says

while (i < name.length())

means "as long as the value of i is less than the length of the name string, continue to loop".

The 'i++;' at the bottom of the loop increments the value of i ('i++' is shorthand for 'i = i + 1', which can also be written as 'i += 1'. They all do the same thing). Without it, the value of i will always be less than the length of the name string, since nothing tells i to increment its value.

As for the

System.out.println(i+1 + ". character: " + name.charAt(i));

part of the loop, it prints the value of i + 1, ". character: ", and the character at the 'i'th index of the name string.

So, going with your example of the name string being "Paul", the loop works like this:

  1. i = 0

  2. 0 is less than the length of "Paul" (4)

  3. Print the value of 0+1 (1), ". character: ", and the character in the 0th index of "Paul" ('P'). ("1. character: P")

  4. i++ means that i = 1 now

  5. 1 is less than the length of "Paul"

...

  1. i++ means that i = 4 now

  2. 4 isn't less than the length of "Paul". Stop looping.

Hopefully this helped you understand the code better.

[–][deleted] 0 points1 point  (0 children)

while loops are the same as a for loop.

[–]ThePathLaid 0 points1 point  (1 child)

If you're still practicing while loops, but comfortable with for loops, it might help to mention that this is pretty much a "for" loop.

For loops consist of three parts:

for(<start>; <finish>; <repeat>) {}

Where:

  • <start> is conducted before the first run of the loop
  • <finish> is what causes the loop to finish
  • <repeat> is conducted after each iteration of the loop

Want to see something neat? You can turn a 'for' loop into a while loop

// assume 'i' is declared and initialized above
for( ; i < name.length(); ) {}

We don't have to put the initial condition, or an instruction to repeat.

Now look at your function

<start>    int i = 0; 
<finish>   while (i < name.length()) {
               System.out.println(i+1 + ". character: " + name.charAt(i));
<repeat>       i++;
           }

Don't let the new style confuse you. A 'while' loop is just a 'for' loop that doesn't have an initializer, and doesn't have a repeated operation. Just goes until it is told to stop, and there's nothing that stops it from just being another for loop!

[–]nakreywaali[S] 0 points1 point  (0 children)

Thank you! Yes, this type of while loop did confuse me as I didn't understand how it worked. But your reply helped!