0
How does the method get these answers?
// first answer becomes 36 // second answer becomes 88 i'm confused on how the result works and how the for loop is working within this program. static int Pow(int x, int y=2) { int result = 1; for (int i = 0; i < y; i++) { result *= x; } return result; } static void Main(string[] args) { Console.WriteLine(Pow(6)); Console.WriteLine(Pow(3, 4)); }
3 Réponses
+ 11
Pow(6) - Only one parameter is passed, default value for second parameter is used (2).
for (int i = 0; i < y; i++) { result *= x; }
Loop runs for values of i = 0, 1.
result *= x is executed two times.
Hence result stores 6 * 6. Value 36 is returned.
Pow(3, 4) - Two parameters passed.
Loop runs for values of i = 0, 1, 2, 3.
result *= x is executed four times.
Hence result stores 3 * 3 * 3 * 3. Value 81 is returned.
+ 2
Pow(6)
When this is called the second argument isn't passed, so the Pow method will use the default value of 2 for y.
static int Pow(int x, int y=2) // y is an optional parameter and defaults to the value of 2 if a value is not passed when Pow is called
In the for loop y is equal to 2 and i has an initial value of 0.
0 < 2 is true so the body of the loop is entered
result is equal to 1 and x is the value that was passed when the Pow method was called. It has a value of 6.
result *= x
is the same as
result = result * 6
result = 1 * 6
So now result is equal to 6
then the value of i is incremented by the for loop (i++) and is now equal to 1
1 < 2 is again true so the body of the for loop is ran again
result = 6 * 6
result = 36
then the value of i is incremented again (i++) and is now equal to 2
2 < 2 is now false so the for loop is exited
The value of result is returned to the
Console.WriteLine(36);
and 36 is output.
Then flow moves on to Console.WriteLine(Pow(3, 4));
This call works in the same way, but y will equal 4 instead of the default value of 2.
See if you can think through the loop with these values step by step.
+ 2
So for loops in general and in this for loop:
-you are defining an integer I and setting it to 0 (int i = 0) part
-Then you are setting the condition for it to follow (i < y)
-lastly, you are incrementing the value by 1 so you don't get stuck in an infinite loop
[Also you need to use the condition and incrementation together, for example, you should not check i < 10 and have i-1 because then you will have an infinite loop too because i will never reach 10 and thus the loop will run forever or until it uses up all your memory and crashes more here: https://en.wikipedia.org/wiki/Infinite_loop]
You can rewrite it like this with a while loop if it helps
(people tell me they can understand while loops better but they are quite similar)
static int Pow(int x, int y=2)
{
int result = 1;
//set intial position/value for the loop to start at
int i = 0;
//check if the condition is valid and keep running while it is valid
while (i < y)
{
result *= x;
//increment up to the next number getting closer to the condition we set for the loop.
i++;
}
return result;
}
then you can read it for yourself
-result is set to 1
- i is set to 0
-while i is less than y:
=> multiple result by x (y number of times because that is how many times the loop will run due to the condition you made)
=> then increment by 1
-once loop ends give me the calculated results
Then you can print it out
learn more about for loops: https://www.tutorialspoint.com/csharp/csharp_for_loop.htm