Curly Braces (scope) related unexpected output of if-elseif loop in C++

97 Views Asked by At

I have used a normal if-elseif ladder to update some variables depending upon conditions. The condition can be either a, b or c. That's why I am using an if-elseif ladder.

But, when I use the if-elseif ladder without the curly braces, I'm not getting the required output. It's as if the code flows into the first if only, and if the condition is false, it's not going into the other else-if's. As far as I know, the default scope of the if block is 1 line below it, right? So, even if I refrain from using curly braces, it should work normally as I'm expecting it to, right?

If I use curly braces, I'm getting the desired output.

#include <iostream>
#define int long long
#define endl "\n"
using namespace std;

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t;
    int n;

    while (t--){
        cin >> n;
        int min_counter_11 = 1e10;
        int min_counter_10 = 1e10;
        int min_counter_01 = 1e10;
        while(n--){
            int m;
            string s;

            cin >> m >> s;

            if (s == "11")
                if (m < min_counter_11)
                    min_counter_11 = m;
            else if (s == "10")
                if (m < min_counter_10)
                    min_counter_10 = m;
            else if (s == "01")
                if (m < min_counter_01)
                    min_counter_01 = m;
        }
        int res = min(min_counter_01 + min_counter_10,min_counter_11);

        if (res > 1e9)
            cout << -1 << endl;
        else
            cout << res << endl;
    }
}
3

There are 3 best solutions below

0
On BEST ANSWER

Your code:

if (s == "11")
    if (m < min_counter_11)
        min_counter_11 = m;
else if (s == "10")
    if (m < min_counter_10)
        min_counter_10 = m;
else if (s == "01")
    if (m < min_counter_01)
        min_counter_01 = m;

is equivalent to the more accurately indented:

if (s == "11")
    if (m < min_counter_11)
        min_counter_11 = m;
    else if (s == "10")
        if (m < min_counter_10)
            min_counter_10 = m;
        else if (s == "01")
            if (m < min_counter_01)
                min_counter_01 = m;

C++ does not change your logic based on your indentation.

4
On

For starters, this directive:

#define int long long

does not make any sense.

Also, you need to include the <string> header:

#include <string>

As for your question, these if statements:

if (s == "11")
    if (m < min_counter_11)
        min_counter_11 = m;
else if (s == "10")
    if (m < min_counter_10)
        min_counter_10 = m;
else if (s == "01")
    if (m < min_counter_01)
        min_counter_01 = m;

are actually equivalent to:

if (s == "11")
    if (m < min_counter_11)
        min_counter_11 = m;
    else if (s == "10")
        if (m < min_counter_10)
            min_counter_10 = m;
        else if (s == "01")
            if (m < min_counter_01)
                min_counter_01 = m;

That is, the else part belongs to the closest if statement, and that has nothing with the scope of the if statement, or the way you indent your code (unlike in Python, for example).

You should rewrite the if statements the following way, using curly bracs around compound statements:

if (s == "11")
{
    if (m < min_counter_11)
        min_counter_11 = m;
}
else if (s == "10")
{
    if (m < min_counter_10)
        min_counter_10 = m;
}
else if (s == "01")
{
    if (m < min_counter_01)
        min_counter_01 = m;
}
0
On

My compiler gives the following warnings for your code:

scratch/src/s13.cpp:32:13: warning: add explicit
      braces to avoid dangling else [-Wdangling-else]
            else if (s == "01")
            ^
scratch/src/s13.cpp:29:13: warning: add explicit
      braces to avoid dangling else [-Wdangling-else]
            else if (s == "10")
            ^

As mentioned in the comments, the indentation does not match how the statements are paired.