Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. for each loop with if else statement

for each loop with if else statement

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpvisual-studiodata-structureshelpquestion
3 Posts 2 Posters 35 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Walter Schmidt 2022
    wrote on last edited by
    #1

    Just out of curiosity: What is the reason, that the following code doesn't compile under Visual Studio:

    int main(array ^args)
    {
    array^ l = {1,2,3};

    for each (int a in l)
    if (a % 2 == 1)
    Console::WriteLine ("even");
    else
    Console::WriteLine ("odd");

    return 0;
    

    }

    This results in the error "else without if". One needs to use a compound statements around the if-else-statement. Using a standard for-loop this works as expected, of course:

    int main(array ^args)
    {
    array^ l = {1,2,3};

    for (int i = 0; i < l->Length; ++i)
    if (l[i] % 2 == 1)
    Console::WriteLine ("even");
    else
    Console::WriteLine ("odd");

    return 0;
    

    }

    J 1 Reply Last reply
    0
    • W Walter Schmidt 2022

      Just out of curiosity: What is the reason, that the following code doesn't compile under Visual Studio:

      int main(array ^args)
      {
      array^ l = {1,2,3};

      for each (int a in l)
      if (a % 2 == 1)
      Console::WriteLine ("even");
      else
      Console::WriteLine ("odd");

      return 0;
      

      }

      This results in the error "else without if". One needs to use a compound statements around the if-else-statement. Using a standard for-loop this works as expected, of course:

      int main(array ^args)
      {
      array^ l = {1,2,3};

      for (int i = 0; i < l->Length; ++i)
      if (l[i] % 2 == 1)
      Console::WriteLine ("even");
      else
      Console::WriteLine ("odd");

      return 0;
      

      }

      J Offline
      J Offline
      jochance
      wrote on last edited by
      #2

      Dunno, but odd/even appear to be transposed (or the if(%) incorrect). %2 == 0 would be even?

      W 1 Reply Last reply
      0
      • J jochance

        Dunno, but odd/even appear to be transposed (or the if(%) incorrect). %2 == 0 would be even?

        W Offline
        W Offline
        Walter Schmidt 2022
        wrote on last edited by
        #3

        Yeah, right, typo. But this is not the point of the question!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups