C++ ,"if" question........................................
Favorites|Homepage
Subscriptions | sitemap
HOME > > C++ ,"if" question........................................

C++ ,"if" question........................................

[From: ] [author: ] [Date: 12-07-09] [Hit: ]
what does first 2 IFs do? how does this program work?I got confused because I dont know how those two close if s work..if (y>z)-Proper indentation makes it easier to analyze a program (so I used _ characters here, added brackets that are equivalent to the way it is written,......
what does this mean/do
int x,y,z;
if (x>y)
if (y>z)
avg=(x+y)/2;
else if(x>z)
anything=x+z;

the main point is ,what does first 2 IFs do? how does this program work?I got confused because I don't know how those two close if s work..
if (x>y)
if (y>z)

-
Proper indentation makes it easier to analyze a program (so I used _ characters here, added brackets that are equivalent to the way it is written, and added my own comments):

int x,y,z;
if (x > y) {
__if (y > z) {
____//this is reached if z is the smallest out of x, y, and z
____avg = (x + y)/2;
__}
__else {
____//if this is reached, this means z is greater than y
____//and maybe greater than x also
____if (x > z) {
______//this is reached if z is between x and y
______anything = x + z;
____}
__}
}

So what it looks like it's doing is, if z is the greatest of the numbers x, y, and z, then it does nothing; if z is the smallest, then it stores the average value of x and y into variable avg (which I assume was initialized earlier); if z is between x and y, it stores the sum of x and z into variable anything.
1
keywords: quot,C++,if,question,C++ ,"if" question........................................
New
Hot
© 2008-2010 http://www.science-mathematics.com . Program by zplan cms. Theme by wukong .