### I need Efficiency suggestions

Hello, I made my first program, I know its newbie and lame...
Anyway, I have a problem.
I used the techniques I learned so far.
This should calculate powers:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081`` ``````// // FirstTry - the first try of shalito to create a program. // a program that calculates power. // #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { //enter a number and a power to calculate the result cout << "This program is powering numbers.\n" << endl; int nNumber; int nPower; cout << "Enter a number:"; cin >> nNumber; cout << "Enter a Power number:"; cin >> nPower; // declare nPower and nNumber to use the calculation formula int nPower2; int nNumber2; // if the number entered is 1, it allways will be 1 if (nNumber == 1) { cout << "1 after powering it by " << nPower << ", equals: 1." << endl; } // if the Power number is 1 it will allways will be the same number if (nPower == 1) { cout << nNumber << " after powering it by 1, equals: " << nNumber << "." << endl; } //if the number is 0, it will allways be 0. if (nNumber == 0) { cout << "0 after powering it by " << nPower << ", equals: 0." << endl; } //if the power number is 0, it will allways be 1. if (nPower == 0) { cout << nNumber << " after powering it by 0, equals: 1." << endl; } if (nPower < 0) { nPower2 = -1; nNumber2 = nNumber; while (nPower2 > nPower) { nNumber *= nNumber2; nPower2--; } cout << nNumber2 << " after powering it by " << nPower << ", equlals: " << "1/" <

The problem is, when when two 'if's are true. and it uses both's braces { }
how can i avoid that?
Like if one if is true, Thats it it will use it's braces and stop
I don't understand your question. Are you trying to keep your program going? Like say I enter 6^2 and it compleates the process and say's push any key, then exits? You don't want it to do this? I"m not sure if I can help you but I would still like to know the question please. Thank you :)
`else`

¿how's that related to efficiency?
Sorry.
The problem is that
`'if (nPower == 0)'`
and
`'if (nNumber == 0)'`
can be entered in the same time
which leads to display both if's:
 ``12`` `````` cout << "0 after powering it by " << nPower << ", equals: 0." << endl;``````

and

` cout << nNumber << " after powering it by 0, equals: 1." << endl;`

and i need a tip or a technique to avoid that

There are a couple ways you could accomplish this. Here's one:

 ``12345678910111213141516`` ``````if(nNumber == 1 && nPower == 1) // If both nNumber and nPower are equal to 1... { cout << "1 after powering it by 1 equals: 1" << endl; // Output this } else if(nNumber == 1) // Otherwise, if nNumber is equal to 1... { cout << "1 after powering it by " << nPower << ", equals: 1." << endl; } else if (nPower == 1) // And if nNumber wasn't equal to 1, but nPower IS equal to 1... { cout << nNumber << " after powering it by 1, equals: " << nNumber << "." << endl; }``````

The first if statement is checking both nNumber and nPower to see if their values match. Then, it steps to the next part of the if statement, which is `else if(nNumber == 1)`. Else If means that if the first part of the if statement wasn't true, then the program should check for this. Then, if THAT statement isn't true, go on to the next `else if` and check to see if that's true.

Using `else if` makes it so the system doesn't look at that code if the first if statement is true. Otherwise, when using plain if statements, the system will go through each one separately.

Hope that helps.
Wow! Thanks! It worked... i just added 'else if' to all other statements but the first one.
Its perfect! Thank you very very much!
Topic archived. No new replies allowed.