# If / else If Statements.

Page 1 of 1

## 5 Replies - 562 Views - Last Post: 09 February 2013 - 08:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311649&amp;s=2997917a761b436d0deea2464791c5e3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zximtiaz

Reputation: 0
• 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

• Freelance developer

Reputation: 6053
• Posts: 13,316
• 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.

• D.I.C Lover

Reputation: 331
• 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.

### #4 axnjxn

Reputation: 14
• 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

### #5 Skydiver

• Code herder

Reputation: 4173
• Posts: 13,309
• 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);

```

### #6 axnjxn

Reputation: 14
• 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.