While Loop: Challenge for beginners - while-loop

Challenge:
Use a while loop to increment count by 2 on each repetition of the block of code. Run the code block of your while loop until count is 8
let count = 2;
I just started learning code two days ago, I am not really sure the logic behind what it is I should do, when to use a while loop or logically the difference between that and an infinite loop. I would really appreciate any help

You call while with a condition, in your case while count < 8.
Inside your while loop you increment count by two every iteration until it is 8 or bigger. So it would look like this:
let count = 2;
while (count < 8) {
count = count + 2;
}
When the condition is not true, so when your count is equal or bigger than 8, the inner part of the while loop is not being executed (your while loop terminates).
An example for an infinite while loop would be with the condition "true":
while (true) {
...
}

Related

What is the difference between these two loops?

I want to write a loop in which I increase my variable i, until arr[i] is less or equal than v.
I've tried these two loops but only the first loop is working and I can't tell the difference.
first loop:
do{
i++;
if(arr[i] >= v)
break;
}while(true);
second loop:
do{
i++;
}while(arr[i] <= v)
I was wondering what exactly the second loop is doing that I don't get the expected result.
In the first one you are breaking when the value is greater than or equal to v
In the second one you are breaking when the value is greater than v
The break conditions are different for each loop
For the second one to work correctly,
do{
i++;
}while(arr[i] < v)

Time complexity of the for-loop

I need to calculate the time complexity of the following loop:
for (i = 1; i < n; i++)
{
statements;
}
Assuming n = 10,
Is i < n; control statement going to run for n time and i++; statement for n-1 times? And knowing that i = 1; statement is going to run for a unit of time.
Calculating the total time complexity of the three statements in for-loop yields 1+n+n-1 = 2n and the loop with its statements yields 2n+n-1 = 3n-1 = O(n).
Are my calculations correct to this point?
Yes, your calculations are correct, a for loop like such would have O(n) notation.
Similarly, you could make a calculation like such:
for(int i = 0; i <n*2; i++){
//calculations
}
in this case, the for loop would have a big O notation of O(n^2) (you get the idea)
This loop takes O(n^2) time; math function = n^n This way you can calculate how long your loop need for n 10 or 100 or 1000
This way you can build graphs for loops and such.
as DAle mentioned in the comments the big O notation is not affected by calculations within the loop, only the loop itself.

Compute Time Complexity

I got skunked in computing the time complexity in the inner loop.
Lets consider the following case.
Case 1:
for(int i = 0; i <= n; i++) - O(n)
{
for(int j = 0; j <= i; j++) - O(?);
{
//Some thing goes here
}
}
Here the inner loop got executed every time up to value i.
So, can I tell like, the complexity for the inner loop is some O(i),
and the overall complexity is O(N) * O(I); ie: O(N*I)
Could some one explain in some brief manner, so i can able to understand the computing.
Thanks.
The overall time complexity is O(n²) (in fact, it’s even Θ(n²)).
The inner loop has complexity O(i). However, n is related to i, so simply saying that the whole thing has complexity O(ni) is wrong. The body of the inner loop will run 0 + 1 + 2 + ⋯ + n = (n² + n) / 2 = Θ(n²) times.
Before i go over what you asked for i will explain a simple example.
We caliculate the time complexity based on how many times the innermost loop is executed
consider this case:
for(i=0;i<n;i++){
for(j=0;j<n;j++){
....
}
}
here the outer loop is executed n times and in every iteration the inner loop is executed n times.
1st iteration - n
2st iteration - n
3st iteration - n
.
.
.
nth iteration -n
so the inner loop is executed n*n times.so it is O(n^2).
Now we caliculate for the case what you asked for
for(int i=0;i<=n;i++){
for(int j=0;j<=i;j++){
//Some thing goes here
}
}
Here the Outer loop is executed for n times. and in every i'th iteration the inner loop is executed i times.
1st iteration - 1
2nd iteration - 2
3rd iteration - 3
.
.
.
nth iteration -n
so when we caliculate it would be
1+2+3+.....+n = n(n+1)/2
which is basically O(n^2). :)

Loop is not doing what expected

I'm studying Obj-C and now I'm not understand why my loop isn't work how it should. I know a way i could achieve result with single while loop, but i want to do this through do while and can't figure out whats going wrong.
What i want is, to show integer called triangularNumber for integers 5,10,15.. and so on. There is what i've tried:
for (int i=1; i<51; i++){
int triangularNumber;
do {
triangularNumber = i * (i+1)/2;
NSLog(#"Trianglular number is %i", triangularNumber);
}
while (i%5 == 0);
}
It produce odd results:
1) Condition of i%5==0 is not met, it output 1,3,6,10 then infinite numbers of 15
2) It create an infinite loop
Please tell me, what is wrong in that code and how to fix it. Thanks!
Instead of do while loop within for loop, use if to check whether a number is divisible by 5 or not.
If you want to do it with do while loop, you could do the following:
int i = 5;
int triangularNumber;
do {
triangularNumber = i * (i+1)/2;
NSLog(#"Trianglular number is %i", triangularNumber);
i += 5;
} while (i < 51);
Reason your code is not working:
Within your do while loop, if i say is 5, then you will end up running in infinite loop as you will satisfy your do while loop's condition which is i%5==0
You just need numbers like 5, 10, 15.. so it's just matter of one loop. Now loop starts with 1 till 50, and you are picky in the sense you just need one element of 5, hence to be picky you could use if condition which says print if and only if my i is multiple of 5.
Do while will always enter into loop and will execute it once. So for 1 it will enter and do calculation for triangularNumber as 1 * 2 /2 = 1 and hence your output 1 and checks condition post printing is 1 divisible by 5, no then it comes out and increments i to 2 and follows same routine as above.

Correct loop invariant?

I am trying to find the loop invariant in the following code:
Find Closest Pair Iter(A) :
# Precondition: A is a non-empty list of 2D points and len(A) > 1.
# Postcondition: Returns a pair of points which are the two closest points in A.
min = infinity
p = -1
q = -1
for i = 0,...,len(A) - 1:`=
for j = i + 1,...,len(A) - 1:
if Distance(A[i],A[j]) < min:
min = Distance(A[i],A[j])
p = i
q = j
return (A[p],A[q])
I think the loop invariant is min = Distance(A[i],A[j]) so closest point in A is A[p] and a[q] .
I'm trying to show program correctness. Here I want to prove the inner loop by letting i be some constant, then once I've proven the inner loop, replace it by it's loop invariant and prove the outer loop. By the way this is homework. Any help will be much appreciated.
I'm not sure I fully understand what you mean by replacing the inner loop by its loop invariant. A loop invariant is a condition that holds before the loop and after every iteration of the loop (including the last one).
That being said, I wouldn't like to spoil your homework, so I'll try my best to help you without giving too much of the answer away. Let me try:
There are three variables in your algorithm that hold very important values (min, p and q). You should ask yourself what is true about these values as the algorithm goes through each pair of points (A[i], A[j])?
In a simpler example: if you were designing an algorithm to sum values in a list, you would create a variable called sum before the loop and assign 0 to it. You would then sum the elements one by one through a loop, and then return the variable sum.
Since it is true that this variable holds the sum of every single element "seen" in the loop, and since after the main loop the algorithm will have "seen" every element in the list, the sum variable necessarily holds the sum of all values in the list. In this case the loop invariant would be: The sum variable holds the sum of every element "seen" so far.
Good luck with your homework!