C ++ question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C ++ question

#include <iostream> using namespace std; int x=20; int f(){ x-=2; return --x; } int g(){ int x=50; x++; return f(); } int main() { while(x%3>=1) cout<<g()<<" "; return 0; } Can someone tell me how this is work? I know the answer but not how it’s work

26th May 2021, 1:16 PM
Ismahel Zero Sadraden
Ismahel Zero Sadraden - avatar
1 Answer
+ 4
start with " while " loop >>while(20%3>=1) //true >>calls " g() " function >>g() calls " f() " //x in g() set to x=51 , I think no work of this x >>in f() value of " x " is 20 //x is declared global (not in f() ) >>in f() x=x-2; > x=20-2; x=18 >>return --x //it makes x=17 and it return to g() , g() return in main and 17 will be printed >>now value of global variable is x=17 >>again start with while loop while(17%3>=1) //same process till while condition evaluates to false ! HOPE THIS HELP ! I suggest, if you are beginner, write this process on your notebook
26th May 2021, 1:38 PM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar