2

I have a situation where I have to do some thing like this

for (int i = 0; i < list.size; i++) //Master for loop
{ 
    //I need a control statement here to control list1 i.e. only if statement

    if (time == list1.get(someInteger))
    {
        //do something
    }
    else
    {
        //do something else
    }
}

But every "if" should be followed by an "else", and I don't seem to understand how to go about it. Ive tried do and while but to no avail. For me it is important to execute both if and else, yet have a control statement for only "if".

Is that possible?

dtb
  • 213,145
  • 36
  • 401
  • 431
Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
  • not clear what you want. – Bozho Feb 17 '12 at 07:13
  • Need this to be described better. It's not a requirement to follow `if` with `else`. Is that a requirement of yours? If so please describe why. The `continue` and `break` keywords may be of interest to you. – Joshua Enfield Feb 17 '12 at 07:17
  • Can you elaborate bit more. Like the things you need to do in side the if and else statement? Since you are telling that you need to execute both if and else blocks I guess you are using those for different purposes which are not matching. (For and example your if statement is specially for setting a flag and the else part is a compulsory block to be executed) – Eshan Sudharaka Feb 17 '12 at 09:30
  • @Eshan Sudharaka yes you are right, my else part is to be executed for everything else, if statement checks if time is equal to the time from the list1 and if it is true draws a rectangle on the right side of the canvas, else let it stay on the left side. – Arif Nadeem Feb 17 '12 at 09:49

3 Answers3

8

...You don't need an else with every if. It's as simple as that.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • In my case I do, my else part is to be executed for everything else, if statement checks if time is equal to the time from the list1 and if it is true draws a rectangle on the right side of the canvas, else lets it stay on the left side. If I dont include else then the rectangles are drawn on both right and left of the canvas – Arif Nadeem Feb 17 '12 at 09:50
  • @mirroredAbstraction See Joshua Enfield's comment – L.B Feb 17 '12 at 09:54
2

With your current constraints and description and rational assumptions No.

For me it is important to execute both if and else, yet have a control statement for only "if".

This violates the specification of what a if...else... is. You cannot execute both if and else and keep the if...else... structure. Logically you could drop the else and just have the code in the else inside the loop as other answers have pointed out. However, if you actually do need to keep if and else you need to clarify why so we have a context to answer your question.

If there is some tricky stuff unstated here you might also have a look at switch and use a Goto Case setup. Might be worth saying this probably wouldn't be recommended by many seasoned programmers. Switch statement fallthrough in C#?

Community
  • 1
  • 1
Joshua Enfield
  • 17,642
  • 10
  • 51
  • 98
  • in my code, I compare the time with the time's from the list and see if they are equal in the "if statement" and draw those times on right side of the canvas. "else statement" is for everything else and those time's should be drawn on the left. So basically I need both if and else because if I dont include else then times in the if loop are drawn both on left and right side of the canvas. – Arif Nadeem Feb 17 '12 at 09:54
  • Does that mean your sample logically does what you need it to? Do you want to get rid of the else but only have it execute when the if isn't, if so why? You could put a `continue;` in the `if` command , remove the else, and it would function this way, but logically it's *almost* equivalent to having the both the if...else.. If you want the if or the else to only be executed once use `break;` where you want to break out of the loop. – Joshua Enfield Feb 17 '12 at 14:35
  • My Algorithm: >If if statement is executed draw the view on right side >Else else statement should be done compulsorily for everything other than the items which match in if statement. Now if I do it your way i.e. I don't use else then what happens is that "if" is executed and the items are drawn on the right side but again the same items (which should only be drawn on right)are drawn along with other items on the left since every item is a part of the master for loop.. This doesnt happen if I include else statement... – Arif Nadeem Feb 17 '12 at 19:23
1

try:

 for (int i = 0; i < list.size; i++) //Master for loop
{ 
    //I need a control statement here to control list1 i.e. only if statement

    if (time == list1.get(someInteger))
    {
        //do something
    }
        //do the other something
}

The if will only be hit if the condition is met, the other part will always be hit.

Stefan H
  • 6,635
  • 4
  • 24
  • 35