initializing iterator before for loop in Dart - for-loop

In my for loop I'm getting an 'avoid unnecessary statements', the code works fine but I assume there must be a more efficient way of writing the below.
int i = 0;
for (i; i < (list.length - list.indexOf('.')); i++)
^
I'm getting the alert on the first i in the for loop.
I understand that the alternative is to replace it with var i = 0, but I've done it this way so I can use the ith position for running the next loop after this one.

You don't need to put anything in the first part of the for:
var i = 0;
for (; i < list.length - list.indexOf('.'); i++) ...
Putting an i there is technically valid, but kindof useless since it doesn't do anything, (which is why you get a warning).
You can also do:
var i = 0;
for (var n = list.length - list.indexOf('.'); i < n; i++) ...
and avoid computing the end for every step of the loop. That would be quite a lot more efficient.
You can also do:
var n = list.length - list.indexOf('.');
for (var i = 0; i < n; i++) ...
and then use n afterwards instead of i. That would make it a more traditional loop for readers looking at it.

Related

How to determine the number of steps on a nested for loop?

I've been working on this algorithm question for a while now and haven't made much progress. The given code is
for(i = n; i > 0; i = i-4){
for(j = i; j < n; j++){
...
}
}
The goal is to determine the theta-runtime of the nested loop. My main issue came when trying to write out the values of i at each iteration. I figured on the first iteration i=n, then on the second iteration it will be i=n-4, and then i=n-8 and so on, but I get confused in determining what the last value of i should be(and more importantly, what the last iteration of the outerloop would be). I spoke with a friend who suggested that the total number of outerloop iterations should be the ceiling of n/4, which seems to make sense, but I don't know how to verify that. Does anyone have any idea how to approach this kind of problem?
I'm assuming you wanted to decrease the value of 'i' by 4 in every iteration.
But instead of decreasing, you're increasing it.
Try like following
for(i = n; i > 0; i = i-4){
for(j = i; j < n; j++){
...
}
}
"total number of outerloop iterations should be the ceiling of n/4" - it's true. As you're decreasing the value of 'i' by 4.
Think about it like this, when you're decreasing the value by 1, it'll run for 'n' times. When you'll decrease by 2, it'll run for n / 2 times. And so on...
To verify the answer, you can keep a counter in your code, then print the value of the count at the end of the program.
About determining the last of 'i':
The last value of 'i' will be from 1 to 4 for which your code will get into the inner loop. (because you're running the loop as long as i > 0 ).
int outerLoopCount = 0;
for( i = n; i > 0; i = i-4)
{
outerLoopCount++;
for(j = i; j < n; j++)
{
}
}
printf("outer loop count: %d\n", outerLoopCount);

Could someone explain this for me - for (int i = 0; i < 8; i++) [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Could someone explain in the simplest terms, as if you are talking to an idiot (because you are), what this code is actually saying/doing
for (int i = 0; i < 8; i++)
That's a loop that says, okay, for every time that i is smaller than 8, I'm going to do whatever is in the code block. Whenever i reaches 8, I'll stop. After each iteration of the loop, it increments i by 1 (i++), so that the loop will eventually stop when it meets the i < 8 (i becomes 8, so no longer is smaller than) condition.
For example, this:
for (int i = 0; i < 8; i++)
{
Console.WriteLine(i);
}
Will output: 01234567
See how the code was executed 8 times?
In terms of arrays, this can be helpful when you don't know the size of the array, but you want to operate on every item of it. You can do:
Disclaimer: This following code will vary dependent upon language, but the principle remains the same
Array yourArray;
for (int i = 0; i < yourArray.Count; i++)
{
Console.WriteLine(yourArray[i]);
}
The difference here is the number of execution times is entirely dependent on the size of the array, so it's dynamic.
for (int i = 0; i < 8; i++)
It's a for loop, which will execute the next statement a number of times, depending on the conditions inside the parenthesis.
for (int i = 0; i < 8; i++)
Start by setting i = 0
for (int i = 0; i < 8; i++)
Continue looping while i < 8.
for (int i = 0; i < 8; i++)
Every time you've been around the loop, increase i by 1.
For example;
for (int i = 0; i < 8; i++)
do(i);
will call do(0), do(1), ... do(7) in order, and stop when i reaches 8 (ie i < 8 is false)
The generic view of a loop is
for (initialization; condition; increment-decrement){}
The first part initializes the code. The second part is the condition that will continue to run the loop as long as it is true. The last part is what will be run after each iteration of the loop. The last part is typically used to increment or decrement a counter, but it doesn't have to.
for(<first part>; <second part>; <third part>)
{
DoStuff();
}
This code is evaluated like this:
Run <first part>
If <second part> is false, skip to the end
DoStuff();
Run <third part>
Goto 2
So for your example:
for (int i = 0; i < 8; i++)
{
DoStuff();
}
Set i to 0.
If i is not less than 8, skip to the end.
DoStuff();
i++
Goto 2
So the loop runs one time with i set to each value from 0 to 7. Note that i is incremented to 8, but then the loop ends immediately afterwards; it does not run with i set to 8.
it's the same as think the next:
"starting with i = 0, while i is less than 8, and adding one to i at the end of the parenthesis, do the instructions between brackets"
It's also the same as:
while( i < 8 )
{
// instrucctions like:
Console.WriteLine(i);
i++;
}
the For sentences is a basis of coding, and it's as useful as necessary its understanding.
It's the way to repeat n-times the same instrucction, or browse ( or do something with each element) an array
for (int i = 0; i &lt 8; i++) {
//code
}
In simplest terms
int i = 0;
if (i &lt 8) //code
i = i + 1; //i = 1
if (i &lt 8) //code
i = i + 1; //i = 2
if (i &lt 8) //code
i = i + 1; //i = 3
if (i &lt 8) //code
i = i + 1; //i = 4
if (i &lt 8) //code
i = i + 1; //i = 5
if (i &lt 8) //code
i = i + 1; //i = 6
if (i &lt 8) //code
i = i + 1; //i = 7
if (i &lt 8) //code
i = i + 1; //i = 8
if (i &lt 8) //code - this if won't pass

Big -Oh Computation (refresher help)

Okay, so I have a midterm later today and one of the items I am reviewing is big-O. Now, I did the homework way back in the day and got 100%....but I can't find it now and I am unsure of what I am doing. Sooo could someone give me an explanation as to what I am doing wrong...and if I am doing it right...well maybe you know why I am doubting myself?
Thanks!
Also, I remember before with my homework I was using summations, and I would work from the inside out. And when I finished each summation I used some "forumla" to calculate the highest n, and then keep that value and move on to the next summation, and so on and so forth until the summations were all completed.
Problem 1.
sum = 0;
for (i = 0; i < n; i++)
sum++;
So, since I forgot the whole summation aspect of this, my gut instinct tell me this is O(N), because the maximum runtimes is N times...since it is just one for loop.
Problem 2.
sum = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
sum++;
For this one, I "think" it is O(N^2) for the highest run time, since both loops are dependent on n, and it could maximize at N * N per if loop.
Problem 3.
sum = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n * n; j++)
sum++;
This is where I get stuck...I feel like I actually need to use the summation layout along with the formula for adding them up. The inner most loop can maximize at n*n, so n^2. On top of which, it can maximize at N again for the outermost loop...so I would guess 0(N^3).
Problem 4.
sum = 0;
for (i = 0; i < n; i++)
for (j = 0; j < i; j++)
sum++;
Again, I am more lost on this one. The inner loop can maximize i times...which is dependent on i however, which is dependent on N....So...I see three maximized variables, and I am literally unsure of how to compare them to find a maximized runtime. (I really need to remember that summation setup and formula).
Same goes for the next problems, no clue where to start, and I'd rather not try to because I don't want to get the wrong thinking in my head. I am positive once I see the formula again it will instantly click, because I got it before...I just lost it somehow.
Any help appreciated!
Problem 5:
sum = 0;
for (i = 0; i < n; i++)
for (j = 0; j < i * i; j++)
for (k = 0; k < j; k++)
sum++;
Problem 6:
sum = 0;
for (i = 1; i < n; i++)
for (j = 1; j < i * i; j++)
if (j % i == 0)
for (k = 0; k < j; k++)
sum++;
for problems 4 to 6, I would assume i j and k are all integers, unlike n which is a variable. how I would approach the problems would be:
e.g. problem 4
inner loop - iterations from 0 to (i-1), which gives us i number of iterations.
outer loop - n summations
combined - O(i * n) = O(n) since i is an integer.

Algorithm for comparing/matching all elements with one another both ways

What is the neatest algorithm for that ? Can it be done without helping/static variable ?
Use two "for" loops. Assuming that you don't need to compare elements against themselves, and you only need to test each pair one way around rather than both ways:
for (int i = 1; i < ints.length; ++i) {
for (int j = 0; j < i; ++j) {
match(ints[i], ints[j]);
}
}
If you want all pairs both ways, just change j < i to j < ints.length. If you want pairs both ways, excluding self-comparisons, either add if (i != j) in the inner loop or else do one inner loop from 0 to i-1, and another from i+1 to ints.length.

Ruby: Is it possible to have a for loop that increases more than one at a time?

For example:
for(int i = 0; i < 100; i += 2)
I could use a while loop or check if i % 2 == 0, but I was wondering if there wasn't a way to do it with the for loop. I could also use for(int i = 0; i < 50; i++) and multiply i by 2, but that doesn't seem right either. I'm just wondering if there is a simple, straightforward way to do it.
(0...100).step(2) do |i|
# loop body
end

Resources