-2

I'm doing practice coding on HackerRank and I was having trouble with one of the problems. I looked at how others would handle it and came across something I haven't seen before.

public static int jumpingOnClouds(List<int> c)
{
        int count = 0;

        for (int i = 0; i < c.Count - 1; i++) 
        {
            if (c[i] == 0) 
                 i++;

            count++;
        }
        
        return count;
}

This iteration is supposed to go through an array of integers. It the integer is 0 it takes a step and if it's 1 it does nothing. At the "if" statement it goes:

    if (c[i] == 0) 
        i++;
    count++;
    

And this code works. However I tried rewriting it like this:

if (c[i] == 0) 
{
    i++;
    count++;
}

But it doesn't work the way the previous code did. Can someone help and explain what this person did because I'm not sure where to look or what this is even called?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Your description of what this code does, and what it actually does is not really accurate – TheGeneral Jul 05 '21 at 03:44
  • Sorry, what's the question here? If you're wondering why the code doesn't work after you've added `{}` it's because in the original code the author used a single-line if, so it's the equivalient to `if (c[i] == 0) { i++; } count++;` – Andrey Stukalin Jul 05 '21 at 03:46
  • 1
    When you don't use brackets, interpretation depends on how you indent your lines. Which is precisely why I always use brackets; that syntax is a recipe for disaster. – Obsidian Age Jul 05 '21 at 03:49
  • Walk through what the code does. Once you figure it out, make a promise to never use clever hacks like this. Let's say that the list contains 5 ints, and index 1 and 3 are zero (so index 0, 2 and 4 are non-zero). On i==zero, it's non-zero, so it's going to skip the `if` block, count is incremented. On i==1, the value is zero, so both `i` and count will be incremented. The next iteration will have i==3 (not 2), confusing everything. This means that the value at index 2 will be skipped from consideration. I'll let you figure out the rest – Flydog57 Jul 05 '21 at 03:55
  • 2
    @obsidan: No! in C#, indentation is completely ignored when determining block structure (unlike, say, Python). After an `if` (or `for`, or...), either one statement or a curly bracket-delimited block is considered the target of the `if` (etc) – Flydog57 Jul 05 '21 at 03:58

1 Answers1

5

If an if, while, for has no brackets { }, it will run the first line, then exist out of the statement.

This:

if (c[i] == 0) 
    i++;
count++;

Actually means this:

if (c[i] == 0) 
{
    i++;
}

count++;

A statement with no brackets is called an Embedded Statement. It works almost the same as an new created scope for an if statement but there are differences.

  • Only runs the first line/ functionality it reaches.
  • You cannot define variables in an Embedded statement.
  • No new scope is created.
CorrieJanse
  • 2,374
  • 1
  • 6
  • 23
  • Thanks for the answer. I was misreading it and didn't realize that count is iterating every loop and the "i" was only being iterated when c[i] == 0. – Jeremy Washington Jul 06 '21 at 03:57