+ 1
Where can I learn how to use the syntax in a practical way (in any language)
the problem with sololearn is that in most cases like loops, it doesn't tell you how or how you would use it to make programs. It just tells you how to print out numbers , so where could I learn how to use the syntax practically?
2 Answers
+ 5
Well what is a loop? There's your answer.
Anytime you want to repeat execution/statements instead of writing a simular thing over and over you just loop.
int[] array = {1,2,3}
int sum = 0;
for(int i = 0; i < array.length; i++)
sum+=array[i];
You will learn how to use this practically as you practice other things, as loops come in handy for lots of things. That may sound like a nothing answer, but I assure you it is as straight forward as that. Specially helpful for arrays.
For example, How can I find out if 2 is in my array? Well, I loop through the array checking each index to see if 2 is the value of the element (linear search)
Here's a real example of a loop that I recently wrote:
int prevSong = -1;
int getNewSong(){
int randNum;
do{
randNum = (int)random(0,10);
}while(randNum == prevSong);
prevSon = randNum;//new song
return randNum;
}
I used the loop to keep grabbing a random number until it chooses a number different from the previous one.
+ 2
look at people's code, how they're using certain structures