Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite+ 8
In the code below the output is 2. This code is used in a challenge and I get an error when I select the answer 2. It accepts as correct the answer 1 which I believe is wrong. If you agree please thumbs up. If you find any other errors please post them here! #include <iostream> using namespace std; int main() { for(int i=1; i>=1; i++) { if(i<1 || i>1){ cout << i; break; } } return 0; }
Error in challenge! SoloLearn correct this!
8 Réponses
+ 2
I think they have already corrected the answer, as I got the same question when playing a challenge and the answer accepted was 2.
+ 2
@James Flanders
Yes you are right. l send an email to SoloLearn and they corrected the error.
+ 1
the answer is 1 and it is correct becoz in for loop u can see the second condition i.e i>=1 its not just greater its an equals too so now cmg to the first condition it is i=1 so its obvious tht the answer is 1 since it satisfies first two conditions i is therfore not incremented i.e i++ so answer is 1
+ 1
@Suhail Pappu sorry my friend but the output is not 1. Below I explain why.
The for loop executes while the condition i >=1 is true.
The 1st time i=1, therefore the condition i >=1 is true. The if condition is true only when i is not equal 1( i<1 || i>1). So the 1st time is false.
The 2nd time i increases by one and i=2, therefore the condition i >=1 is true again. The if condition is also true so it outputs 2 and command break stops the execution of the loop.
Also see the table below for better understanding.
variab | conditions | Out
--------------------------------------------------------------------
i | i >=1 | T/F | i > 1 || i < 1 | T/F |
====================================
1 | 1>=1 | T | 1 > 1 || 1 < 1 | F |
2 | 2>=1 | T | 2 > 1 || 2 < 1 | T | 2
+ 1
ok good
+ 1
I think that the initial value was for (I = 0;....
thus , it was 1 ( as the correct answer)
0
c++
0
your answer is right it is 2. the first condition i<1 is wrong 1 is not less than 1. the second condition i>1 is right because 2 is greater than 1 but that will cause a infinite loop that's why a break is in the code to stop the infinite loop.