+ 1
Hello. Why in programming for loop is used so much ? I mean ex:instead of saying (for a==10 a<20 a++); we can say a<20 a++;
For loop
12 ответов
+ 5
That would only work if a already had a value from before.
And then it wouldn't really be shorter - it would just be in another place.
If you want to repeat an action based on a changing value, you need to know, where that value starts, where it will stop and how large the steps will be.
But maybe you want to share a code example where you believe a loop is not needed? We can discuss about it.
+ 5
For loops are used for repetition which is essential in programming.
+ 4
You can use a while loop with any term that can be evaluated as 'true' or 'false', not only with numbers. while(true) means that the loop runs unconditionally.
And you can put all sorts of code in the body of a loop.
Maybe it would be best if you find an example where people used a for loop unnecessarily and show us. We can then discuss about the pros and cons.
+ 3
There is a programming principle called DRY - Don't Repeat Yourself.
for(int n=0; n<10000; ++n)
printf("%d\n", n);
These two lines output the numbers from 0 to 9999 on the screen.
You could also write:
printf("0");
printf("1");
printf("2");
...
But doing it like this will require 10,000 lines of code. And in reality it's always the same line, only a different number.
Think about it:
Computers are meant to do work for us.
So if they can easily do something with only 2 lines of code, why would you insist on writing all these instructions yourself?
+ 3
It depends on the language.
In Python, for all the built-in data types it is defined how they are evaluated in a boolean context.
The general principle there is that everything that is 'nothing' returns False, while 'something' returns True.
For example you can do something like this:
fruits = ['apple', 'banana', 'grapefruit']
while fruits:
print(fruits.pop())
A list that contains at least one element returns True, an empty list False.
So this example removes the 'fruits' from the list one after another and outputs them.
As soon as the list is empty, the loop stops.
+ 1
I'm with you on this. I know that I don't have waste my time writing hundreds of lines if there's a code which consists of 2 lines can do the thing. I know about this. What I don't understand is I see a lot of people using for loop in different things not numbers.
+ 1
Also the while loop. We use it for numbers while other people use it like
While (true). For me all I know is that while loop works only for numbers not words.
+ 1
I didn't say for loop is used unnecessarily.I myself know that for loop is very important.What I wanted to know some people use it in different way. Because for me for loop and while loop only works with numbers that's what I understood. Does it work with different things than numbers ? That's what I wanted to know.
+ 1
Thank you so much for your awesome explanation. The only problem is I want to create a game engine and I don't know how I can do it,which language should I use.
A lot of people told me to use unity or unreal which I already did but I couldn't understand anything from so that's why I want to create my own engine but problem is I don't know which language does it.
+ 1
Probably you'll want to be as close to the hardware as possible for a high performance. That suggests C(++) and/or assembly.
That's quite advanced stuff, I won't really be a help there.
0
Thanks for explanation. But what is better. Should i go with for loop or just normal.
Ex:for loop
int a=5
for (a=5; a<10; a++;)
Ex:normal
int a=5
a<5;
a++;
which one is the best for me to use.
Because for me I see that writing it normal is similar to writing it with for loop. I mean I can repeat a code a lot of times than writing it with for loop.
0
If I'm not wrong that means I should learn opengl ? Or something which is related to hardware ?