Carrying a torch
Badges: 9
Rep:
?
#1
Report Thread starter 1 month ago
#1
What's the easiest way to make use of loops within loops if the data you are dealing with are all Strings? Any example of nested loops I find seem to always be on integers.. but say I had a program that involves talking to the user... how could I use nested loops maybe for feedback on the conversation so far or something similar?
0
reply
0le
Badges: 21
Rep:
?
#2
Report 1 month ago
#2
You iterate through the length of the string. Most programming languages have a library to give you this information and it outputs an integer which you then use in your iteration.

Before you start the loop you would scan the shell for an input, determine the length of the input and use this in the for loop. If you have some sort of dynamic feedback loop where you constantly ask the user for a String, then a while loop is preferable with if statements to determine what to do based on the length of a string and a terminating statement if no input is made. Don't forget the terminating statement otherwise the while loop will proceed endlessly.

In Java you can also iterate through a string using a for-each loop:
Code:
for (char theLetter : myString.toCharArray() {
}
Last edited by 0le; 1 month ago
0
reply
Carrying a torch
Badges: 9
Rep:
?
#3
Report Thread starter 1 month ago
#3
(Original post by 0le)
You iterate through the length of the string. Most programming languages have a library to give you this information and it outputs an integer which you then use in your iteration.

Before you start the loop you would scan the shell for an input, determine the length of the input and use this in the for loop. If you have some sort of dynamic feedback loop where you constantly ask the user for a String, then a while loop is preferable with if statements to determine what to do based on the length of a string and a terminating statement if no input is made. Don't forget the terminating statement otherwise the while loop will proceed endlessly.

In Java you can also iterate through a string using a for-each loop:
Code:
for (char theLetter : myString.toCharArray() {
}
You point out some interesting stuff. So far I have a while loop that asks the user to ask a question and then their question is tested against to see if it contains a trigger words stored in arrays containing values from a record, if that makes sense. I made the while loop go off whenever the user basically says bye. I could technically use some sort of loop to search through each branch of the if statement to see if it contains the trigger words ... that way I end up making use of loops within the while loop? Not sure however since atm I just use .equals or .contains so would need a better reason to add a few loops to help the user
Last edited by Carrying a torch; 1 month ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you travelling in the Uni student travel window (3-9 Dec) to go home for Christmas?

Yes (120)
28.3%
No - I have already returned home (57)
13.44%
No - I plan on travelling outside these dates (83)
19.58%
No - I'm staying at my term time address over Christmas (40)
9.43%
No - I live at home during term anyway (124)
29.25%

Watched Threads

View All