How can i make a proper if/else if/else statement?

if else statement c++
if-else if java
else if python
if else statement example
if else javascript
if statement
javascript if statement multiple conditions
for loop

I'm attempting to create a game in Unity for my game development class using c#. I'm having a problem trying to make an input statement. What i would like it to do is if i input for example "1", i want it to respond (for now) "you chose 1!" but it is not playing it. If it helps I'm using this for my game.

Here is what i have so far

void UserInput(string input)
    {
        if (input == "1")
        {
            print("You choose 1");
        }
        else if (input == "2")
        {
            print("You choose 2");
        }
        else
        {
            print("Invalid Response");
        }
    }

Any help would be great! Thanks!

Edit: Thank you all for helping me. I'm very new to all of this. I appreciate all of the responses

If your output what was input, perhaps you might be able to spot the problem:

void UserInput(string input)
{
  Console.WriteLine($"The input was '{input}'");

The single quotes are useful when the input is empty, because then you will see The input was '' rather than The input was.

Java If Else, Use the else statement to specify a block of code to be executed if the condition is false . If the time was less than 18, the program would print "Good day". Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to select one of many blocks of code to be executed.

In your specific case, you seem to only want to inform the end-user of their input, in which case you can simply do:

public void UserInput(string input) {
    Console.WriteLine($"You choose {input}");
}

However, if you only wish to handle certain cases (in your case, only 1 and 2), then you can use a switch statement:

public void UserInput(string input) {
    switch(input) {
        case "1":
        case "2":
            Console.WriteLine($"You choose {input}"); // will only write 1 or 2
            break; // necessary to prevent fall-through
        default:
            Console.WriteLine("Invalid Response");

Chapter 4: The if/else (if) statement and Nested statements, if (condition) { // do this if condition is true // if true statements } else { // do this is With the if statement, a program will execute the true code block or do nothing. you must test a combination of conditions before deciding on the proper action. An if statement can be followed by an optional else ifelse statement, which is very useful to test various conditions using single ifelse if statement. When using ifelse if..else statements, there are few points to keep in mind − An if can have zero or one else's and it must come after any else if's. An if can have zero to many else if's and they must come before the else. Once an else if succeeds, none of the remaining else if's or else's will be tested. Syntax

another solution is

void UserInput(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                Console.WriteLine("Invalid Response");
                return;
            }
            Console.WriteLine($"You choose {input}");
        }

ifelse, Multiple ifelse statements can be nested to create an else if clause. works, this is how it would look if the nesting were properly indented: ELSE-IF. ELSE-IF is optional and identifies a conditional expression to be tested when the previous conditional expression is false. ELSE-IF statements allow multiple conditions to be nested without requiring an END-IF statement on each condition. You can code as many ELSE-IF statements as necessary.

C++ ifelse statement, C++ ifelse statement - An if statement can be followed by an optional else statement, which executes when the boolean expression is false. 1. else and else..if are optional statements, a program having only “if” statement would run fine. 2. else and else..if cannot be used without the “if”. 3. There can be any number of else..if statement in a if else..if block. 4. If none of the conditions are met then the statements in else block gets executed. 5.

Java if, ifelse Statement (With Examples), Here, our program will do one task (task inside if block) if the test expression is true and another task (task inside else block) if the test expression is false . How if​ If else statement in C++ Sometimes you have a condition and you want to execute a block of code if condition is true and execute another piece of code if the same condition is false. This can be achieved in C++ using if-else statement. This is how an if-else statement looks:

If else Statement in C++, Sometimes we need to execute a block of statements only when a particular condition is met or not met. This is called decision making, as we are executing. The if-else-if-else Construct In the same way that we could add an else statement below an if statement, we can add an else statement below the if-else-if construct as the sketch below shows. This sketch works in the same way as the previous sketch, except that when any character except 'a' or 'b' is sent, the else block is run which flashes the LED even faster than the previous two blink rates.

Comments
  • The best way to solve your problem is to use the integrated debugger. You will discover in no time that your input is not what you think.
  • Please post all relevant code in the question. I doubt anyone is going to request permission to the file you linked, and they shouldn't be expected to.
  • Its also worth noting that in this case, a switch statement or strategy pattern is much better for code clarity and isolation.