-4

I am just starting out with C++, so my logic is a bit of a poor.

I want to form a for loop which says 'for an integer n; When value of n ranges from [a,b]; increment n by 1 (++n)."

Although I seem to have figured out what went wrong, I just want to exactly know the fault in my logic for a statement (for loop) from my solution:

Incorrect logic:

for (int n = 0; a <= n <= b; ++n) {} /*does not satisfy any test case*/

The above for loop gives me "Wrong output" (In this statement I intend to say: integer n has value 0 (initialized). When n is between a and b, keep incrementing n by 1). How exactly does this differ from the logic I originally want from the for loop (which I have specified at the start of this question)? Do they also consider n =0?

My solution got accepted when I modified the statement to:

Correct Logic:

for (int n = a; n <= b; ++n) {}
aquaticos
  • 25
  • 3
  • `a <= n <= b` compiles but does not do what you think. If you added the corrected code `a <= n && n <= b` your loop condition would be wrong because at the start of the loop `n` is less than `a` so the loop will not enter. You want the second code if `n` is to loop from `a` to `b` inclusive. – drescherjm Sep 17 '21 at 13:30
  • 2
    Please note that the questions asked on HackerRank are meant for persons who know the computer language well enough to never make mistakes like the one you're making. HackerRank is not there to teach C++ programming. – PaulMcKenzie Sep 17 '21 at 13:31
  • 2
    *I am just starting out with C++...* Then you would do yourself a big favor by learning C++ from a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Sep 17 '21 at 13:32
  • You might have attracted fewer down votes if you had neglected to mention "Hackerrank". You have nicely abstracted your issue away from your program (good!), and at this point it is no longer necessary to mention what the purpose of your program is. All you need is the purpose of your loop, and the fact that the output is incorrect, but became correct after your change (output `n` after the loop so that your code has output). Well, probably at that point you should add an example showing "incorrect" and "correct" output. – JaMiT Sep 17 '21 at 13:39
  • Thanks everyone :D. I am just learning the language using PPP2 and parallelly implementing what I learn on HackerRank. So many of my mistakes are silly, though I really couldn't figure out what went wrong this time and spent time trying to figure out myself before asking the question. Sorry. However I now understand what went wrong all thanks to the help all of you gave :D – aquaticos Sep 17 '21 at 13:39
  • Hint: after getting an answer you can still improve your question. – Evg Sep 17 '21 at 13:48
  • @aquaticos -- If you want to use HackerRank, again, their questions are meant for the person who knows the computer language they will be using to answer the question -- the questions there can be difficult enough, so why have two mountains to climb? It makes very little sense, for example, to try and code up a search tree, or a trie, etc. that will answer the question, and you have little to no grasp of dynamic memory handling, pointers, etc. I see too many questions here from HackerRank users who have little understanding of C++, yet are trying to code up some complex data structure. – PaulMcKenzie Sep 17 '21 at 14:19
  • No no I am using the C++ tree and solving the very basic problems like for loop, hello world etc... whatever I learn from the book I read. – aquaticos Sep 18 '21 at 01:39

1 Answers1

1

Conditions do not work this way.

Your condition is parsed as

(a <= n) <= b

Which means: Compare a to n then compare the result of that (a boolean) to b.

For example a=0, n= 42, b= 100:

(0 <= 42) <= 100
(true) <= 100
true

bools can convert to int then false is 0 and true is 1. Hence, for this example you get the right result, but for the wrong reason.

What you want is: Compare n to a and compare n to b:

 a <= n and n <= b   // or the equivalent
 a <= n && n <= b
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185