LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2023, 09:24 PM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 377

Rep: Reputation: 4
Why for loop without any working code (in third part) is there?


In the link given here, in the main() there is twice given a for-loop, that has empty 3rd part, i.e. nothing gets executed.

Kindly help by telling why such for-loop was used twice.
The code is repeated below:

Code:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define pcx putchar_unlocked
#define gcx getchar_unlocked
 
typedef int32_t ichar;
typedef int_fast64_t fint;
 
static inline fint getfi () {
    ichar c = gcx();
    while (!isdigit (c)) c = gcx();
    fint n = 0;
    while (isdigit (c)) {
        n = n * 10 + c - '0';
        c = gcx();
    }
    return n;
}

static inline void putfi (fint n, char lc) {
    if (0 == n) {
        pcx ('0');
        if (lc) pcx (lc);
        return;
    }
    char s[24];
    fint rdi = 0;
    while (n) {
        s[rdi++] = '0' + n % 10;
        n /= 10;
    }
    while (rdi) pcx (s[--rdi]);
    if (lc) pcx (lc);
}

static inline fint min2 (fint A, fint B) {
    return (A < B ? A : B);
}
 
int main () {
    fint A [100000];
    fint R [100000];
    fint T = getfi() + 1;
    while (--T) {
        fint N = getfi();
        for (fint ai = 0; ai < N; )
        A[ai++] = N - getfi();
        for (fint ri = N-1; ri >=0; )
        R[ri--] = N - getfi();
        char nlt[100008] = {0};
        fint exit = 0;
        for (fint ai = 0; ai < N; ++ai) {
             A[ai] = A[ai] + R[ai] - N;
             if (A[ai] < 1 || A[ai] > N) {
                 exit = 1; 
                 break;
             }
            if (nlt[A[ai]]){
                exit = 1; break;
            }
            else nlt[A[ai]] = 1;
       }
       if (exit) {
            pcx('-');
            pcx('1'); 
            pcx('\n'); 
            continue; 
       }
       for (fint ai =0; ai < N; )
       putfi(A[ai++], ' ');
       pcx('\n');
      }
      return 0;
}
 
Old 12-13-2023, 12:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Indentational problem, should be:
Code:
    for (fint ai = 0; ai < N; ) {
        A[ai++] = N - getfi();
    }
    for (fint ri = N-1; ri >=0; ) {
        R[ri--] = N - getfi();
    }
Incrementing ai/ri inside the loop is bad idea but it works nonetheless.
 
Old 12-13-2023, 12:39 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,828

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
The "end action", for example ai++, can be omitted if it's done in the loop block.
Code:
        for (fint ai = 0; ai < N; )
        A[ai++] = N - getfi();
The advantage of a for loop is that you can see the "initial action", "leave condition", "end action" alltogether.
Here it is still readable because the loop body is only one line.

The normal loop would use a cpu cycle more:
Code:
        for (fint ai = 0; ai < N; ai++ )
        A[ai] = N - getfi();

Last edited by MadeInGermany; 12-13-2023 at 12:43 AM.
 
Old 12-13-2023, 01:12 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,027

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
you can even use a formatter to fix issues like this: https://codebeautify.org/c-formatter-beautifier
(just copy your code into the box and check the result)
 
1 members found this post helpful.
Old 12-13-2023, 03:45 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
> The normal loop would use a cpu cycle more
With modern compilers (meaning: since 199x) you cannot this way micro-optimize the code.
 
1 members found this post helpful.
Old 12-13-2023, 08:26 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,683
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
I would always use "the third part" because this what every programmer expects to see.

It's kinda like "Paris In The The Spring." Your wandering eye, "knowing" what to expect, might not have noticed the second "The" until I just mentioned it . . .

. . . and a programmer, while frantically trying to locate a bug, just might not either.

If you're lucky, they might just say "D-oh!" But they might instead say something unprintable. So, "when you write code, think about the next person who will read it – under pressure and in a hurry."

Last edited by sundialsvcs; 12-13-2023 at 08:28 AM.
 
1 members found this post helpful.
Old 12-13-2023, 12:25 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by ajiten View Post
Title: Why for loop without any working code (in third part) is there?

...in the main() there is twice given a for-loop, that has empty 3rd part, i.e. nothing gets executed.
Given the thread title, it seems to me that you are saying nothing gets executed, that is the loop is not executed due to the missing third expression, so why is the loop even there.

If so, that is not correct.

All three expressions are optional, the semicolons are not, so that even with for(;;); the loop will be executed (endlessly in this case).

Quote:
Originally Posted by ajiten View Post
Kindly help by telling why such for-loop was used twice.
Only the person who wrote it can tell you why they did it in that way.

Last edited by astrogeek; 12-13-2023 at 05:37 PM. Reason: typos
 
1 members found this post helpful.
Old 12-18-2023, 08:35 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,683
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Quote:
Originally Posted by astrogeek View Post
Only the person who wrote it can tell you why they did it in that way.
But plenty of programmers who have to follow that person, and debug his work, will be "mightily annoyed." Yes, there is a reason why you should write your code in a way that conforms to your successor's expectations. Your successor wants above all to understand your (erroneous ...) intentions "at a glance." Because, anything else is "a waste of time," and "time is money."

Frankly, there are quite a few times over the years when I looked at a block of code, said to myself "what the f&ck is this?" Ripped the whole thing out and re-wrote it in a way that was much more obvious. (Granted: Some of that code was so old that it had been written to cope with hardware restrictions that, quite thankfully, no longer exist.)

Last edited by sundialsvcs; 12-18-2023 at 08:37 AM.
 
Old 12-18-2023, 09:18 AM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
You're asking about this loop? Which I've taken the liberty of adding brackets to?

Code:
for (fint ai = 0; ai < N;) {
    A[ai++] = N - getfi();
}
It's equivalent to this:

Code:
fint ai = 0;
while (ai < N) {
    A[ai++] = N - getfi();
}

Last edited by dugan; 12-18-2023 at 10:02 AM.
 
Old 12-18-2023, 09:59 AM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Since this came up:

There's a website called https://godbolt.org/ that you can use to check the assembler that C/C++ code would become.

The asm produced by the following two programs is not identical. I'll leave the interpretation as an exercise to someone who knows ASM better than I do.

Code:
#include <stdio.h>

int main()
{
    for (int i = 0; i < 10;) {
        printf("%d\n", i++);
    }
    return 0;
}
Code:
#include <stdio.h>

int main()
{
    for (int i = 0; i < 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}
 
Old 12-18-2023, 12:03 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,027

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by dugan View Post
Since this came up:

There's a website called https://godbolt.org/ that you can use to check the assembler that C/C++ code would become.

The asm produced by the following two programs is not identical. I'll leave the interpretation as an exercise to someone who knows ASM better than I do.

Code:
#include <stdio.h>

int main()
{
    for (int i = 0; i < 10;) {
        printf("%d\n", i++);
    }
    return 0;
}
Code:
#include <stdio.h>

int main()
{
    for (int i = 0; i < 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}
Yes, you are right, they are not identical.
the question is if you switch on optimization what will be the result.
 
Old 12-18-2023, 06:48 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by pan64 View Post
Yes, you are right, they are not identical.
the question is if you switch on optimization what will be the result.
I get the same instructions, but the order is slightly different. I guess it would be the same amount of cycles either way, but it's hard to be sure since modern CPUs are complicated.

Code:
        mov     esi, ebx
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        add     ebx, 1
        call    printf
Code:
        mov     esi, ebx
        xor     eax, eax
        add     ebx, 1
        mov     edi, OFFSET FLAT:.LC0
        call    printf
There is some more code around this, but apart from this sequence it's the same for both. The .LC0 refers to "%d\n".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recover after dd(part table on extended part lost) but data should be there roberto32 Linux - Kernel 3 01-02-2014 10:39 AM
How can I remove UBUNTU from my computer. Accidentaly installed without third part. habimana Linux - Newbie 2 02-05-2013 07:37 PM
Finding Third Part Packages in AIX manoj.linux AIX 2 08-07-2011 10:19 PM
Is there any benefit to rewrite OSS-based code to ALSA-based code? RogueWarrior65 Linux - Software 1 08-13-2010 02:11 AM
want to install third part application under /opt yogesh619 Linux - Newbie 3 07-21-2008 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration