blah blah blah is here! blah blah » Close

up0down
link

public int Cash;
public int GiveCash(int amount)
{
if (amount <= Cash && amount > 1)
{
Cash -= amount;
return amount;
}
else
{
MessageBox.Show("Heldur þú að ég á endlaust af peninga?");
}
return 0;
i know i have a method and inside it there is parameter ( BTW What is Parameters?)
then i use if statement to give cash to a person

so here comes the if statement that i dont know what it does

all i know is my code works and the program works , but i still cant figure out
what this thing means " if (amount <= Cash && amount > 1)"

fire i know that amount must be less than cash and then i get confusing code that is && amount > 1- anyways i hope you guys can help me understand this thanks

last answered one year ago

1 answers

up1down
link

This piece of code:

if (amount <= Cash && amount > 1)
{
Cash -= amount;
return amount;
}
else
{
MessageBox.Show("Heldur þú að ég á endlaust af peninga?");
}
return 0;

means in words:

If amount is less than or equal to Cash AND amount is greater than 1, then

* subtract amount from cash
* return amount to whatever code is calling this method

otherwise:

* display some text in a MessageBox
* return zero to whatever code is calling this method

A parameter is a variable which accepts a value when you call a method. The value itself is called an argument to the method. So this declaration:
public int GiveCash(int amount)

tells us that GiveCash is a method wikth one parameter, amount, which accepts an int value when the method is called. The method itself also returns an int to the calling code.

You'd call the method with a line such as this:
int value = GiveCash(3);

Here 3 is an argument which is assigned to the parameter, amount.

Feedback