# Which is better - for inside if or if inside for



## sakumar79 (Jun 17, 2011)

Hi,
   I am doing some coding in .NET in which there is a set of commands that I have to execute either once or twice (second time with slight changes) depending on user input. So, I want to know which is most efficient -

Option 1. 
For()
command 1
if (twice) command 2
End loop

Option 2.
for()
command 1
end loop
if (twice)
for()
command 2
end loop

Option 3.
if (twice) then
for ()
command 1
command 2
end loop
else
for()
command 1
end loop
end if

Thanks in advance
Arun


----------



## krishnandu.sarkar (Jun 17, 2011)

The thing that you want, Option 1 is for you.

Not all methods will do their jobs same. Try it yourself.


----------



## sakumar79 (Jun 17, 2011)

I am a casual programmer, my actual job is not in programming.... I am doing programming to automate some tasks on my job... Hence, I am doing this in whatever time I find available - so, it is difficult to try all the options...

I would have thought that option 3 would be best... Can you explain why option 1 is best in your opinion?

Thanks in advance
Arun


----------



## Zangetsu (Jun 17, 2011)

sakumar79 said:


> Option 1.
> For()
> command 1
> if (twice) command 2
> End loop



here For loop will run & will keep checking for twice condition
*remark:* command 1 will run always but command 2 will run if true
Grade: *A*


sakumar79 said:


> Option 2.
> for()
> command 1
> end loop
> ...



here For loop will run with command1
& if twice condition is true then only 2nd forloop with command 2

*remark:* command 1 will run always 
but command 2 will run multiple times in a for loop if true
Grade: *C*



sakumar79 said:


> Option 3.
> if (twice) then
> for ()
> command 1
> ...



if twice is true then command 1 & command 2 multiple times

else only command 1 multiple times

Grade: *B*

so A is more efficient

Btw all three options r not all related to each other(in terms of logic)


----------



## krishnandu.sarkar (Jun 17, 2011)

Ok, lemme try to explain in my way with my option.

*OPTION 1:*


```
int i=1;
if(i>1)
{
    for(int j=0; j<10; j++)
    {
         printf("%d %d",j,i);
    }
}
```

This would print nothing, as the first checking of if would be false and it'll simply exit.

Now let's keep the values same but interchange if() and for()

*OPTION 2:*


```
int i=1;
for(int j=0; j<10; j++)
{
    if(i>1)
    {
         printf("%d %d",j,i);
    }
}
```

This will also print nothing.

So you'd ask what happened after interchanging if() and for()??

In first case, the program starts, and it checks if i is greater than 1, and gets false and simply exists.

But in second case, it too doesn't print anything, but the for loop runs from j=0 to j=9 and doesn't print anything because the if returns false.

So if you just add a line...


```
int i=1;
for(int j=0; j<10; j++)
{
    printf("%d %d",j,i);
    if(i>1)
    {
         printf("%d %d",j,i);
    }
}
```

It'll print j from 0 to 9 and i will be 1 everytime.

But if you add the same line in first program...


```
int i=1;
if(i>1)
{
    printf("%d %d",j,i);
    for(int j=0; j<10; j++)
    {
         printf("%d %d",j,i);
    }
}
```

Then also it wont print anything, as the if will return false in the first check.

Hope I'm clear.


----------



## sakumar79 (Jun 17, 2011)

Sorry if I sound a bit dumb, but let me explain what I am thinking. There are two cases: DoTwice= true and DoTwice=False. Let us assume that the for loops are to go for 10 times. For the three options, let us see what happens in each case:

1. Case 1: DoTwice=true
    Here, Option 1 will run - for loop 10 times, command 1 10 times, if check 10 times, command 2 10 times, total 40 runs
    Option 2 will run - for loop 10 times, command 1 10 times, if check 1 time, new for loop 10 times, command 2 10 times, total 41 runs
    Option 3 will run - if check 1 times, for loop 10 times, command 1 10 times, command 2 10 times - total 31 runs

1. Case 1: DoTwice=false
    Here, Option 1 will run - for loop 10 times, command 1 10 times, if check 10 times, command 2 0 times, total 30 runs
    Option 2 will run - for loop 10 times, command 1 10 times, if check 1 time, new for loop 0 times, command 2 0 times, total 21 runs
    Option 3 will run - if check 1 times, for loop 10 times, command 1 10 times - total 21 runs

Hence, in my mind, I feel option 3 will be most efficient.

Please clarify where I am wrong...

Thanks again
Arun

PS: @Zangetsu - I dont know what is Grade for the code (not IT student) - please explain a bit...


----------



## Zangetsu (Jun 17, 2011)

sakumar79 said:


> Sorry if I sound a bit dumb, but let me explain what I am thinking. There are two cases: DoTwice= true and DoTwice=False. Let us assume that the for loops are to go for 10 times. For the three options, let us see what happens in each case:
> 
> 1. Case 1: DoTwice=true
> Here, Option 1 will run - for loop 10 times, command 1 10 times, if check 10 times, command 2 10 times, total 40 runs
> ...



initially I didn't understand your logic... 

but the total runs r predicted in a wrong way

*CASE A:* for loop will 10times & DoTwice=true

    Here, Option 1 
will run  (for loop + command 1 = 10 times)
           if check command 2  = 10 times = so only 10 checks & 10runs 

    Option 2 
will run (for loop + command 1 = 10 times)
    if check 1 time, (for loop + command 2 = 10 times) = 1 check & 20 runs

   Option 3 
will run - if check times,
 (for loop + command 1 + command 2 = 10 times) = 1 check & 10 runs


*CASE B:* for loop will 10times & DoTwice=false

  Here, Option 1 
  will run - (for loop + command 1 = 10 times)
  if check wont run = 10 check & 10 runs

  Option 2 
 will run - (for loop + command 1 = 10 times),
   if check will not run = 1check & 10 runs

    Option 3 
  will run - else part (for loop + command 1 = 10 times) = 1check & 10 runs

so option 3 is better efficiency


----------



## rhitwick (Jun 17, 2011)

Your program should match with with your requirement. Here all the solutions speak of different requirement.



sakumar79 said:


> Hi,
> Option 1.
> For()
> command 1
> ...


As I understood it,
>Loop starts
>Execute command 1
>Certain changes happen which is validated in next step
>if condition satisfies
>execute command 2

//Now, my question is, if output of command 1 is required for command 2? If yes, then this is perfect.



> Option 2.
> for()
> command 1
> end loop
> ...



Here,
>Loop starts
>Command 1 executes n no of times and produces a certain scenario which is the end product of for loop execution.
>if condition satisfies
>loop starts
>executes command 2 for n no of times.

Diff. b/w opt 1 and 2:-
In opt 1 o/p of *each iteration of for loop* is validated in next if condition and depending on that command 2 is executed or not.
Whereas, in opt 2, for loop execution ends and generates result, depending on that an if condition is validated and depending on that command 2 is executed.

In opt 1, command 2 may or may not execute all the iterations (as o/p of each steps are validated in next step. If the condition is static i.e. if o/p=7 (or true) then depending on the o/p of command 1 the command 2 would execute. e.g 


```
sum=0
for i=1 to 10
sum=i+sum
  if sum=9
     print sum
  end if
end loop
```

In opt 2, command 1 executes n no of times generates result, result is validated and depending on that command 2 gets executed n no of times,

above program as in opt2,


```
sum=0
for i=1 to 10
sum=i+sum
end loop

if sum=9
   for j=1 to 10
       print sum
   end loop
end if
```




> Option 3.
> if (twice) then
> for ()
> command 1
> ...



here the scenario is totally diff.

>if condition is checked first
>if matched, enter loop
>execute command 1
>execute command 2
>end loop
>if condition not matched
>for loop starts
>command 1 executes
>loop ends

if I write it using the above example,


```
if condition=true
  for i=1 to 10
       sum=i+sum
       print sum
  end loop
else
  for i=1 to 10
      sum=i+sum
  end loop
end if
```
Now, your task is to imagine the out put of all the examples I've used, u'd get your answer.


----------



## sakumar79 (Jun 17, 2011)

Thanks to all who have answered... As my condition is static (does not vary by the commands in the for loops), I have decided to go for option 3

Arun


----------



## krishnandu.sarkar (Jun 17, 2011)

Option 3 wont make you achieve the thing that you want.


----------



## sakumar79 (Jun 17, 2011)

Please elaborate...

Arun


----------

