5 Replies - 402 Views - Last Post: 09 February 2013 - 08:21 AM Rate Topic: -----

#1 zximtiaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-November 12

If / else If Statements.

Posted 08 February 2013 - 06:46 PM

// First someone explain me if I can use an If loop within an If loop. This is what I did in this program. If No then can someone explain me how this program works.


#include <iostream>

using namespace std;

int main ()

{

	int Weight = 0;
	char Gender = 0;
	char ActivityLevel = 0;
	int Calories = 0;
	int NumberOfCalories = 0;


	cout << "Enter the Gender (M/F) : ";
		cin >> Gender;

	if (Gender == 'M' )
	{
	
		cout << "Enter the Activity Level (A/I) : ";
		     cin >> ActivityLevel;

	       if (ActivityLevel == 'A')
		{
		 cout << "Enter the Weight of the Person (In Pounds)  : ";
		    cin >> Weight;

			NumberOfCalories = (Weight * 15);
				cout << "The Total no. of Calories required are : " << NumberOfCalories << endl << endl;

		else 
			
			if (ActivityLevel == 'I')
		{
		
			cout << "Enter the Weight of the Person (In Pounds)  : ";
		    cin >> Weight;

			NumberOfCalories = (Weight * 13);
				cout << "The Total no. of Calories required are : " << NumberOfCalories << endl << endl;
		}

	else 
		
		if (Gender == 'F' )
	{
	
		cout << "Enter the Activity Level (A/I) : ";
		     cin >> ActivityLevel;

		if (ActivityLevel == 'A')
		{
		 cout << "Enter the Weight of the Person (In Pounds)  : ";
		    cin >> Weight;

			NumberOfCalories = (Weight * 12);
				cout << "The Total no. of Calories required are : " << NumberOfCalories << endl << endl;

		else  
			
			if (ActivityLevel == 'I')
		{
		
			cout << "Enter the Weight of the Person (In Pounds)  : ";
		    cin >> Weight;

			NumberOfCalories = (Weight * 10);
				cout << "The Total no. of Calories required are : " << NumberOfCalories << endl << endl;

	}
		}
	}

	return 0;

}

This post has been edited by jimblumberg: 08 February 2013 - 07:14 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: If / else If Statements.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5468
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: If / else If Statements.

Posted 08 February 2013 - 07:48 PM

if is not a loop - so you need to decide if you need an if, or if you need a loop.

Yes you can nest if comparisons within blocks of code that would be run by other if comparisons.

If you would run your code in debug and walk through the execution line by line you would see it happening.
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: If / else If Statements.

Posted 08 February 2013 - 07:58 PM

If statements are switches in logic:

If this, then do that. (Otherwise, don't do that).

They're not loops.

You should ask the common questions to the user, first - THEN ask the gender, to avoid duplication of code.

You can nest if statements, one inside the other - no problem. You have to be careful about using else statements, in that case.
Was This Post Helpful? 0
  • +
  • -

#4 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: If / else If Statements.

Posted 09 February 2013 - 07:59 AM

Why not use a function call to reduce all that duplicated code?

#include <iostream>

using namespace std;

void calculateCalories(int, int);

int main ()

{

	int Weight = 0;
	char Gender = 0;
	char ActivityLevel = 0;

	cout << "Enter the Gender (M/F) : ";
	cin >> Gender;
	
	cout << "Enter the Activity Level (A/I) : ";
	cin >> ActivityLevel;

	cout << "Enter the Weight of the Person (in Pounds) : ";
	cin >> Weight;
	
	int caloriesFactors[][2] =
	{   // A   I
		{ 15, 13 },    // M
		{ 12, 10 },    // F
	};

	int row = Gender == 'M' ? 0 : 1;
	int col = ActivityLevel == 'A' ? 0 : 1;

	calculateCalories(caloriesFactors[row][col], Weight);

	return 0;

}

void calculateCalories(int calorieFactor, int Weight){
	int NumberOfCalories = (Weight * calorieFactor);
	cout << "The Total no. of Calories required are : " << NumberOfCalories << endl << endl;
}


This post has been edited by axnjxn: 09 February 2013 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,944
  • Joined: 05-May 12

Re: If / else If Statements.

Posted 09 February 2013 - 08:06 AM

And a lookup table:
int calorieFactors[][2] =
{   // A   I
    { 15, 13 },    // M
    { 12, 10 },    // F
};
:
int row = Gender == 'M' ? 0 : 1;
int col = ActivityLevel == 'A' ? 0 : 1;
:
calculateCalories(caloriesFactors[row][col], Weight);


Was This Post Helpful? 1
  • +
  • -

#6 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: If / else If Statements.

Posted 09 February 2013 - 08:21 AM

Good call. Integrating that into the code I posted for him.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1