-1

Can anyone explain to me what's happening in line number 4, and how to understand these types of loops in future.

I was solving this problem. I have used basic approaches like pow(2,n) and (1<<n), but it overflows. Then I got this solution, but I'm unable to understand that fourth line. I know how to use for() loops in C++, but I'm a bit confused because of starting, i.e. nothing is there i.e. for(; e > 0; e >>= 1).

long long modpow(long long b, int e)
{
    long long ans = 1;
    for (; e > 0; e >>= 1)
    {
        if (e & 1)
            ans = (ans * b) % mod;
        b = (b * b) % mod;
    }
    return ans;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
code_
  • 3
  • 3
  • Welcome to Stack Overflow. What understanding do you have of this already? For example, when you write a `for` loop, what do you think each part between the `()` means? Why do you suppose that there are two `;`s? – Karl Knechtel Oct 03 '22 at 06:19
  • You can also the explanation of >>= https://stackoverflow.com/questions/38922606/what-is-x-1-and-x-1 – holopekochan Oct 03 '22 at 06:19
  • 1
    What does your text-books or tutorials say about `for` loops? What have your teachers said? Is it the whole concept of the `for` loop you're wondering about? Or is it some small part of the shown `for` loop you're wondering about? Please **[edit]** your question to clarify. – Some programmer dude Oct 03 '22 at 06:23
  • 1
    Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 03 '22 at 06:24
  • 1
    A `for` loop has four parts: 1) An initializer expression; 2) A condition; 3) "Incremental" expression; and 4) A statement. The three expressions are all optional, only the statement is mandatory. For example for an infinite loop `for (;;) { /* Statement */ }` is perfectly valid. – Some programmer dude Oct 03 '22 at 06:53

1 Answers1

1

The for loop has 3 components:

for (a; b; c) {
}

a runs at the start. The loop will break when b is no longer true, and after each iteration c is executed.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • 2
    Did you mean "will break *when* 'b' is no longer true"? – SiHa Oct 03 '22 at 06:22
  • If your understanding of the question is such that this answers it, then it is surely a duplicate... ? – Karl Knechtel Oct 03 '22 at 06:24
  • @KarlKnechtel To be honest, the question is pretty unclear/non-specific. Not sure this answers it – George Oct 03 '22 at 06:26
  • This answer sums up pretty well how a for loop works. In your case, you don't have **a**, so your loop does nothing at the start. Your **c** is ```e >>= 1``` which is short for ```e = e >> 1```, right shift e by one bit. It is basically divining e by 2 at every loop (throwing out remainders) – Hackjaku Oct 03 '22 at 07:27
  • Thank you everyone for Help, Special thanks to. https://stackoverflow.com/users/10848956/hackjaku and https://stackoverflow.com/users/440558/some-programmer-dude – code_ Oct 03 '22 at 07:47