Nested Ifs in C++

How to Write Nested If Statement in C++

Page 1 of 1

3 Replies - 2575 Views - Last Post: 04 October 2009 - 01:26 PM Rate Topic: -----

#1 hedmbuck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 09

Nested Ifs in C++

Post icon  Posted 04 October 2009 - 12:57 PM

I am in a basic Fundamentals of Programming class, and have returned to school after being in the workforce for 28 years. Our class meets once per week for three hours. One of our assignments had two parts: ask the user to input three numbers and output the largest. Write the code two ways: one with nested ifs and one with compound if - then - else. Got the compound part - no problem. I don't understand AT ALL how to even START with the nested if without having the first IF statement being compound.

This is what I have written (leaving out all the items at the top I have to include in my assignment):

int Number1
int Number2
int Number3

cout <<"Please enter your first number."<< endl;
cin >>Number1;

cout <<"Please enter your second number."<< endl;
cin >>Number2;

cout <<"Please enter your third number."<< endl;
cin >>Number3;

if ((Number1 > Number2) && (Number1 > Number3))
	 {
	 cout <<Number1;
	 };


This is where I get stuck because I am not supposed to have a compound statement in this part of the exercise. I know this is VERY basic, and I don't know why I am not getting this particular thing; I drew a flowchart and I get how it works, just not how to write it.

Can anyone get me started? Thank you so much for your assistance.

Is This A Good Question/Topic? 0
  • +

Replies To: Nested Ifs in C++

#2 ladyinblack   User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: Nested Ifs in C++

Posted 04 October 2009 - 01:19 PM

Yes, I doubt you were to start with a compound.

Try one statement at a time, like this:

if (number1 > number2)
{
  if (number1 > number3)
  {
	cout << number1;
  }
  else if (number2 > number3)
  {
	 cout << number2;
  }


Think you can take it from here?
Was This Post Helpful? 0
  • +
  • -

#3 polymath   User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Nested Ifs in C++

Posted 04 October 2009 - 01:20 PM

int Number1; //SEMICOLONS
int Number2;
int Number3;

cout <<"Please enter your first number."<< endl;
cin >>Number1;

cout <<"Please enter your second number."<< endl;
cin >>Number2;

cout <<"Please enter your third number."<< endl;
cin >>Number3;

if (Number1 > Number2) {
   if (Number1 > Number3) {
	  cout << Number1 << endl;
   }
}



when you use and, it's the same as throwing them inside each other. i reccomend reading my tutorial here: http://www.dreaminco...wtopic53455.htm
Was This Post Helpful? 0
  • +
  • -

#4 hedmbuck   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 09

Re: Nested Ifs in C++

Posted 04 October 2009 - 01:26 PM

Oh, that makes perfect sense! I knew it would be something so very basic that when I saw it I would be VERY embarrassed for asking the question.

But thank you so much.

This is a great site...I am going to let my classmates know about this site!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1