When do we use :
here mam is using String rrs : rr in the for loop. for(String rrs : rr)
Can u pls explain what is its meaning…
: Symbol use in for loop
Hey @code_breaker-1001 The format which ma used here is called FOR EACH loop.
The difference between for loop and for each loop is that in for each loop you don’t need any int variable to traverse through an array or a list.
for eg : for(int i = 0; i < ar.length; i++){
/Here ar[] is a String array.
//This is the for loop;
System.out.println(ar[i]);
}
//This is for each loop
for(String element; ar){
//Here element will be taking values of ar from index 0 to ar.length - 1.And here we didn’t took any variable for indexing.So in each traversal element will be storing next element of ar.
System.out.println(element);
}
//here the output of both for loop and for each loop will be same.So you can get to know the functioning of for each is similar to traversal loop for array or list.
oh Thank You So much. I got it!
I was trying to surf the net using : symbol for loop so was not getting any useful results
Can u also pls check my coding dbt …
I have not used for each loop there but the rest of the steps are same but I am getting an error.
@code_breaker-1001 Can you send me your code or you can create a doubt from the assignment section so that I will be able to see your code.
I have already posted a dbt
I ll send it again here
Mam has done something similar. I have just not used the for each loop but I am getting an error. Can u tell me what is wrong.
Can u pls check?..
Hey @code_breaker-1001 You have to remove = to sign from the line number 21 bcoz if j becomes equals to str length then you will get out of bound exception.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
Thank you Sir. Sorry for the late reply but can u tell me why it works when I remove the = there cause Sir the string length for suppose last case is 0 that is when str has empty string in it and c is the cc so cause of j < str.length the length and j both are 0 so for loop will not be entered only right? But it is working here how?
Hey @code_breaker-1001 You have to remove = sign bcoz as we know that indexing of string starts from 0 so if we include = then we will get StringOutOfBound Exception.
Ahhhhh, I got it now. I was taking str as empty and not the current str so was thinking wrong. Thank You…
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.