Hii frnds how is this code working could somebody help me ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hii frnds how is this code working could somebody help me ??

#include <iostream> using namespace std; int main() { int m; for(m=0;m<3;m++){ cout << ((m%2)?m:m+2); cout <<" "; } return 0; } //output---> 2 1 4

25th May 2018, 4:28 PM
Anuj Chourange
Anuj Chourange - avatar
4 Answers
+ 2
at the beginning of for loop m is 0. so program checks is 0 % 2 one or zero if one (so, true) then write just m (which is zero for now), if not then plus 2 to m (now m is 2) then print it. condition ? something1 : something2; if condition is right something1 is executed, if wrong something2 is executed.
25th May 2018, 4:50 PM
Mustafa K.
Mustafa K. - avatar
+ 2
https://code.sololearn.com/cnrK1VhSmyeP/?ref=app this versiin of your code should give you some insights about what is going on: the for loop runs 3 times and each time the value of m increases by 1. the output inline if statement returns m if the reminder of m/2 is 1 else m+2 if the reminder is 0 (m%2)? m : m+2; (m%2)? evaluate the reminder of m/2 return m if it is true/1 : else return m+2 (if is false/0)
25th May 2018, 4:55 PM
seamiki
seamiki - avatar
0
thankew @Mistafa K. i got it how this actually works
25th May 2018, 4:58 PM
Anuj Chourange
Anuj Chourange - avatar
0
thanks all of u
25th May 2018, 4:59 PM
Anuj Chourange
Anuj Chourange - avatar