programming.dev
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
Albin Jose@suppo.fi to Programming · 2 years ago

What will happen if we put a semi-colon after a for loop in C++?

message-square
message-square
17
link
fedilink
15
message-square

What will happen if we put a semi-colon after a for loop in C++?

Albin Jose@suppo.fi to Programming · 2 years ago
message-square
17
link
fedilink
alert-triangle
You must log in or register to comment.
  • plistig@feddit.de
    link
    fedilink
    arrow-up
    18
    ·
    edit-2
    2 years ago

    A semicolon ends a statement, and semicolon is a statement on its own. One that does nothing. That’s why you can write

    int i;
    for (i = 0; i ᐸ 3; i++);
    

    to set i = 3. You can use that pattern to find something in an iterator, etc. But I would prefer

    int i = 0;
    while (i ᐸ 3) {
       i++;
    }
    

    for readability.

    • octoperson@sh.itjust.works
      link
      fedilink
      arrow-up
      18
      ·
      edit-2
      2 years ago

      Your less thans got HTML-escaped into < and I spent embarrassingly long trying to figure out what pointer magic you were demonstrating

      • plistig@feddit.de
        link
        fedilink
        arrow-up
        4
        ·
        2 years ago

        Yeah, both Voyager and the normal lemmy web client escape the less-than sign. I tried it twice on both clients.

        • shagie
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          2 years ago

          deleted by creator

          • plistig@feddit.de
            link
            fedilink
            English
            arrow-up
            6
            ·
            2 years ago

            Canadian Aboriginal syllabics to the rescue!

  • JakenVeina@lemm.ee
    link
    fedilink
    arrow-up
    17
    ·
    2 years ago

    The loop will run to completion with no side effects. Unless it gets optimized away by the compiler or CPU.

    • vext01@lemmy.sdf.org
      link
      fedilink
      arrow-up
      13
      ·
      2 years ago

      Now slap some commas in there and you can have side effects in the loop header.

      Guaranteed to confuse :)

    • Miaou@jlai.lu
      link
      fedilink
      arrow-up
      5
      arrow-down
      2
      ·
      2 years ago

      It’s actually ub, so it could also find the cure to cancer

  • Otter@lemmy.ca
    link
    fedilink
    English
    arrow-up
    12
    ·
    2 years ago

    Might help if you add some sample code

  • pelya@lemmy.world
    link
    fedilink
    arrow-up
    8
    arrow-down
    2
    ·
    2 years ago

    Pro tip: for(;;) takes less to type thann while(true)

    • deegeese@sopuli.xyz
      link
      fedilink
      arrow-up
      21
      ·
      2 years ago

      My programs cause enough tears on their own without having to put them in the code itself.

    • Joey
      link
      fedilink
      arrow-up
      8
      ·
      2 years ago

      #define EVER (;;)

    • DreamButt@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 years ago

      As yes. Readability destroying anti-patterns. My old enemy

    • Sigmatics@lemmy.ca
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      I still struggle to see how that’s valid syntax

      • pelya@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        But it compiles.

        • Sigmatics@lemmy.ca
          link
          fedilink
          arrow-up
          2
          ·
          2 years ago

          Yes, I know. On the other hand, when you see people code-golfing C, this is on the light end of the spectrum

  • Gamma@beehaw.org
    link
    fedilink
    English
    arrow-up
    4
    arrow-down
    3
    ·
    2 years ago

    Nothing

  • randompepsi@lemmy.ml
    link
    fedilink
    arrow-up
    1
    arrow-down
    17
    ·
    2 years ago

    deleted by creator

    • vext01@lemmy.sdf.org
      link
      fedilink
      arrow-up
      7
      ·
      2 years ago

      Only if you malloc in the loop header buddy!

Programming

programming

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 190 users / day
  • 1.11K users / week
  • 2.98K users / month
  • 7.19K users / 6 months
  • 2.71K local subscribers
  • 20.1K subscribers
  • 2.17K Posts
  • 34.9K Comments
  • Modlog
  • mods:
  • snowe
  • Ategon
  • MaungaHikoi@lemmy.nz
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org