The code below output different results. Can anyone clarify me on that. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The code below output different results. Can anyone clarify me on that.

#include<iostream> using namespace std; int main() { int res; for(int flag = 0; flag < 5; flag++) { if((flag > 2) && (res > 2)) { res += 7; } } cout << res; return 0; }

29th Jul 2021, 11:26 AM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
3 Answers
+ 3
In if condition your res variable is un-initilized that's why it taking default values and your if condition never be true and by using cout u printing value of res which is garbage
29th Jul 2021, 11:39 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 5
I think you need to initialize "res" variable before you apply .
29th Jul 2021, 11:36 AM
ln e
+ 4
Thanks much I get it
29th Jul 2021, 11:48 AM
Apongpoh Gilbert
Apongpoh Gilbert - avatar