What is the output from each code segment below Iint x 1 y

What is the output from each code segment below? Iint x = 1, y = 0; if (x >0 && y

Solution

a.

int x = 1, y = 0;   //Variables are initialized.
if(x > 0 && y < 0) //x is greater than 0, but y is not less than 0, so condition fails.
x = y = 20;   //This line will not be executed.
cout<<x<<\" \"<<y<<endl;   //So, the output is: 1 0

b.

int x = 1, y = 0; //Variables are initialized.
if(x > 0 || y < 0)   //x is greater than 0, so the if block is executed.
x = y = 23;           //x and y values are updated to 23.
cout<<x<<\" \"<<y<<endl;    //So, the output is: 23 23

c.

int x = 10;    //Variables are intialized.
int y = 40;
if(x >= 10)   //x is greater than or equal to 10, so if block will be executed.
{
if(y < 40)   //y is not less than 40, so nothing is done infact.
y++;
}
else
y--;
cout<<x<<\" \"<<y <<endl; //So, the output is: 10 40.

d.

int x = 10;   //Variables are initialized.
int y = 40;
if(x >= 10)   //x is greater than or equal to 10, so the block is executed.
if(y < 40)   //y is not less than 40, so else block is executed.
y++;
else           //This block is executed.
y--;       //y value is decremented, and is now 39.
cout<<x<<\" \"<<y<<endl; //So, the output is: 10 39.

 What is the output from each code segment below? Iint x = 1, y = 0; if (x >0 && y Solutiona. int x = 1, y = 0; //Variables are initialized. if(x >

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site