Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop without the second condition, i.e. the boolean check?

Tags:

c

loops

for-loop

I have to write a function that calculates the floor of log base 16 of an unsigned int passed in. There are restrictions as to what operators and what constants we are allowed to use, and we can only use specifically for loops.

For clarity, we cannot use any conditional statements(if, else, switch ... ). The function prototype is:

int floor_log16(unsigned int x); 

Allowed operators: ++ -- = & | ~ ^ << ! >>

Allowed constants: 1 2 3 4 8 16

I wrote a version of the program as follows:

int floor_log16(unsigned int x) {
    int index=1;
    int count=(1!=1);

    count--;

    for(; index<=x; index<<=4) {
        count++;
    }

    return count;
}

which seems to work as desired. However, I realized that based on the later functions and description of the needed functionality we have to write, I noticed that under "allowed operators" sometimes > and < were listed.

I deduce this implies that since for the floor_log16 function listed above, we weren't explicitly told to use > or <, I can only assume that the solution posted above will not be accepted.

This leaves me rather confused because I don't understand how you can possibly have a for loop without a boolean check?

Isn't the whole idea of a loop to iterate while a condition is met?

like image 246
Arthur Collé Avatar asked Jun 08 '13 22:06

Arthur Collé


People also ask

Can we run for loop without condition?

Yes it is perfectly correct to do so.

How do you check for two conditions in a for loop?

If you want to test both conditions, use the && operator. What is happening in your code is related to how the comma operator , works. Both i < p and j < q are evaluated, but only the result of the 2nd expression j < q is checked by the for loop.

DO FOR loops check the condition first?

do while loop starts with the execution of the statement(s). There is no checking of any condition for the first time. After the execution of the statements, and update of the variable value, the condition is checked for true or false value. If it is evaluated to true, next iteration of loop starts.

Does for loop run only when the condition is true?

A for loop works as long as the condition is true. The flow of control, at first comes to the declaration, then checks the condition. If the condition is true, then it executes the statements in the scope of the loop. At last, the control comes to the iterative statements and again checks the condition.


2 Answers

Well, first of all, for-loop without the boolean check is perfectly fine. For example,

for (;;)

is a common way of writing

while (true)

Second, having a for-loop with other parts but without boolean check is still useful as you can exit it with return or break.

And the last thing. There are tons of ways of getting a boolean without using < and >. For example, you can simply use i to check that i != 0 and so on.

For example if you want to check that a < b you can check for (a - b) < 0 instead. Implementing addition (and hence subtraction) with bitwise operators is a well known interview question (you should really try to do this yourself, it's fun), and checking that your int is negative is as easy as looking at its most significant bit.

like image 118
kirelagin Avatar answered Sep 21 '22 10:09

kirelagin


I don't like to spoil your task but consider about for condition like 'comparison to 0'. This doesn't require any explicit operator. One of possible way to get it is something like this:

// This cycle will end as soon as index is 0.
for (;index; index = (index >> 4))
{
    // ...
}
like image 24
Roman Nikitchenko Avatar answered Sep 20 '22 10:09

Roman Nikitchenko