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;
}
}
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.