If and else statement

Hello hello everybody!
Ok ok it was a new update on codea and I wonder if I can write and if and else statement in codea and
How do I do it. I’m writing a little bit in javascript and found a little cool things example you can make a game easy with
If and else statement and math random and I wonder if I can do this in codea
In JavaScript you can write console.log to print text when you touch a sprite or something
And you can use prompt = (“hello”) my question is can I do this in codea?
Thanks for reading this! And thanks if you can help me :slight_smile: :slight_smile:

hello hello. you can do many things in lua language. Read the manual first!

Ok

@Gamerage13 You can also look at the example code posted here to see how to do a lot of things.

Ok thanks @dave1707

Ok thanks :slight_smile:

One more question about this why does this code don’t work it says bad argument
Missing nil or something

function setup()
    if print(math.random(1,10)) == 2 then
        print("hello winner")
    elseif var < 2 then
        print("hello noob")
    else
        print("hello player")
    end
end     

The line with math.random should not have the print function in it!

Try

if math.random(1,10) == 2 then

Var isn’t a variable yet, you need to make it one.

function setup()
    var = math.random(1, 10)
    if var == 2 then
        print("hello winner")
    elseif var < 2 then
        print("hello noob")
    else
        print("hello player")
    end
end

Ok thanks!

@Gamerage13 My bad for not being more clear on what var was :stuck_out_tongue:

I promise this is the last question
How can I do a if and else statement when I write something to get a answer
Example-

If I write hello the program will say hello bro
If I write goodbye the program will say see ya later
Or something

Example I was made a game in JavaScript
And it ask the player to write his name
If name == Jonas console.log("hello master ")
And then It asks for my age and if I

You now what I’m meaning…

I now I need to look at the wiki and search by myself but I didn’t find anything
And I want to get started with coding on my iPad because it’s fun and easy.
And where did YOU LEARN TO CODE? please leave a comment how you learned coding
And thanks for reading

@Gamerage Here’s 2 ways to do if/else code like what you want. Or you could use a table for the number compare.


function setup()
    -- using a string
    answer1("hello")
    answer1("whats the weather like")
    answer1"goodbye"
    
    -- using a number
    answer2("hello",1)
    answer2("whats the weather like",2)   
    answer2("goodbye",3)
end

function answer1(str)   -- comparing a string
    print(str)
    if str=="hello" then
        print("hello bro.")
    elseif str=="goodbye" then
        print("see ya later.")
    elseif str=="whats the weather like" then
        print("not too good, its raining")
    end
    print()
end

function answer2(str,v) -- comparing a number
    print(str)
    if v==1 then
        print("hi, how you doing.")
    elseif v==3 then
        print("where you going.")
    elseif v==2 then
        print("looks like the rain stopped.")
    end
    print()
end

@Gamerage13 Check out the wiki, there is a great ebook on learning the basics of lua and codea. It should help you get on track.

If else format.

if var == 1 do
    --code here
elseif var < 1 do
    --code here
else
    --code here
end

```


To print to console
print(var)
--or
print("Hello World!")

```


Random
print(math.random(1,10)) --random number from 1 to 10

```

@Gamerage13 Here’s the example using a table.


function setup()
    -- using a number and table
    answer3("hello",1)
    answer3("whats the weather like",3)   
    answer3("goodbye",2)
end

function answer3(str,v) -- using a table
    print(str)
    tab={"good to see you again.",
         "do you have to leave so soon.",
         "its starting to snow." }
    print(tab[v])
    print()
end

Thanks @dave1707

I was thinking about one thing
In parameters you can write
But can I put the if and else statement in there?
Example if I write hello
The parameter can see that and print hello player

And one more touch when touch detected do this
If touch detected start level or print hello or something
I can code in lua but not on iPad/codea it’s a little bit trickier in here
Because codea does not have the full power like programming in a computer/Mac/PC
Ok thanks for everything! And thanks for helping me!
I now how to write an if and else statement in lua but not in codea


function setup()


parameter.text("TitleText", "hello", function(t) print(t) end )
        parameter.watch("string.upper(TitleText)")


parameter.number = io.read()
   if parameter.watch == joke then
      print("you are a nerd")
  elseif
parameter.watch == hello then
        print("hello player")
   else  
     print("write something")
        
end
        end
function draw()
    background(255, 255, 255, 255)

    
end
       

@Gamerage13 Try this. Use the questions “hello” and “is it sunny”, then press answer. Your code wasn’t formatted correctly. The 3 ~'s need to be on lines by themselves. I changed it for you so your code looked correct.


function setup()
    parameter.text("question")
    parameter.action("answer",answer)
end

function answer()
    if question=="hello" then
        print("hi, how you doing")
    elseif question=="is it sunny" then
        print("no, its raining right now")
    else
        print("I dont know what you're asking.")
    end
end