Jump to content
ChetanaSforum
Sign in to follow this  
chitkaraankit

predict output c++

Recommended Posts

What would be the output for this code?#include<iostream.h>void main(){int x=0, y=4, z=5;if(x>2){if(y<5){cout<<"Message one"<<endl;}else{cout<<"Message two" << endl;}}else if(z<5){cout<<"Message three"<<endl;}else{cout<<"Message four" << endl;}}

Share this post


Link to post
Share on other sites

Message four

 

if(x>2)

{

if(y<5)

{

cout<<"Message one"<<endl;

}

else

{

cout<<"Message two" << endl;

}

}

The code given above is in the same block. Since, the condition if(x>2) becomes false, control is transferred to the next statement, which is :

 

else if(z<5)

{

cout<<"Message three"<<endl;

}

This condition also becomes false, which again transfers the program control to

else

{

cout<<"Message four" << endl;

}

And so, the output would be "Message four".Posted Image

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  



×