Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 6.4.
Task one is to pick up the user's response and store it in a string variable.
So here I'm going to say string response is equal to scanner or scan next line.
And workbook 6.4 is basically identical to 6.3, just like in workbook 6.3, we're going to index every
single element inside of the store array and for every single element that we index, we're going to
compare it against whatever the user inputs.
And if the user's response matches one of the aisles inside of our store, then we are going to print.
We have that in aisle, whatever I equals when this condition evaluates to true.
And once we find what the user is looking for, we break the loop.
And that is all.
This is extremely identical to workbook 6.3.
It doesn't really introduce anything new aside from user interactivity.
So let's go ahead and put one breakpoint at the line where you define the for loop, and we are going
to visualize the runtime right now.
So right now, next line is waiting for me to enter a string.
I will enter the value bananas.
All right, so their response equals bananas.
This loop is going to keep running so long as I is smaller than the length of the store array.
When I.
Equals zero.
The element that index zero of our store array is apples.
Apples does not equal our response of bananas.
So this will evaluate to false getting skipped.
Now during the second run of our loop I equals one.
The element at index one bananas does equal our response.
So this will evaluate to true.
We have bananas in aisle one.
We're done searching.
We can break the loop and continue with the runtime.
Let's do this again.
That was kind of fun.
Let's say the user is looking for autonomy.
The loop starts with I equaling zero when I equals zero were grabbing the element at index zero.
So apples does not equal tw this if statement is going to evaluate the false.
During the second run of our loop I equals one.
The element at index one is bananas, which does not equal t during the third run of our loop.
I equals to the element and index to candy does not equal t, so that's going to get skipped.
Index three chocolate does not equal t now I equals for the element.
That index for coffee does not equal t.
And now during the last run of our loop I equals five.
The element of index five does equal our response of T.
So this evaluates to true.
We have that in aisle number five.
And now that we're done searching, we break the loop.
And now let's imagine that the user enters something that isn't in our store.
So let's say they are looking for, I don't know, crackers.
When the loop runs, I equals zero.
The element of index zero apples does not equal crackers, so this statement will get skipped.
And no matter what I ends up equaling, none of the elements in the array are going to equal crackers.
So this is going to keep going where the if statement is always going to evaluate the false until we
get to our very last run where I ends up equaling five, the element that index five does not equal
crackers.
So now I a plus plus is going to increase I to six.
The length of the store array is six.
Six is not smaller than six, so the loop ends.
And now we can just terminate the runtime.
So the only difference between this workbook and workbook 6.3 and this one is more dynamic.
The user can search for anything that they want.
If we find what they're looking for, we print the following message break the loop to stop our search.
But if we don't find what they're looking for, the for loop is going to run to completion, not printing
anything.