c++ - ORing of statements -
where difference lies between these 2 style of writing. compiler showing correct answer in first case , wrong in second case.
1.
string s[6]; for(int i=0;i<6;i++) cin>>s[i]; if(s[0]==s[2] && s[0]==s[4]) { cout<<"yes"<<endl; } else if(s[0]==s[2] && s[0]==s[5]) { cout<<"yes"<<endl; } else if((s[0]==s[3] && s[0]==s[5])) { cout<<"yes"<<endl; } else if((s[0]==s[3] && s[0]==s[4])) { cout<<"yes"<<endl; } else if((s[1]==s[2] && s[1]==s[4])) { cout<<"yes"<<endl; } else if((s[1]==s[2] && s[1]==s[5])) { cout<<"yes"<<endl; } else if((s[1]==s[3] && s[1]==s[4])) { cout<<"yes"<<endl; } else if((s[1]==s[3] && s[1]==s[5])) { cout<<"yes"<<endl; } else cout<<"no"<<endl;
and
2.
string s[6]; for(int i=0;i<6;i++) cin>>s[i]; if(s[0]==s[2]||s[0]==s[3]) { if((s[0]==s[4]||s[0]==s[5])) { cout<<"yes"<<endl; } } else if(s[1]==s[2]||s[1]==s[3]) { if((s[1]==s[4]||s[1]==s[5])) cout<<"yes"<<endl; } else cout<<"no"<<endl;
consider scenario
s[1]=s[2] , s[1]=s[4] , s[0]=s[2]//rest don't care
code1 output yes
.good. consider code2
if(s[0]==s[2]||s[0]==s[3])//s[0]==s[2] enter here { if((s[0]==s[4]||s[0]==s[5])) //this condition not true `yes` not printed { cout<<"yes"<<endl; } } else if(s[1]==s[2]||s[1]==s[3])//now won't enter here entered if { if((s[1]==s[4]||s[1]==s[5])) cout<<"yes"<<endl; }
so code2 outputs nothing.
Comments
Post a Comment