Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help for if statements
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
storm2713687




PostPosted: Wed Feb 27, 2013 1:43 pm   Post subject: Help for if statements

I was just wondering if I can use if statements like this (For an example):
Turing:


var operation : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get operation
if 1 then
    put "1+1=", 1 + 1
elsif 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if



Would that work? I've only used booleans for my if statements so far, but I'd like to try this, but I'm not sure if it works (last time I remember doing something like that, it failed).
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Feb 27, 2013 5:28 pm   Post subject: RE:Help for if statements

you could read the documentation -- if -- to see exactly what you can do with the statement.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Wed Feb 27, 2013 7:39 pm   Post subject: RE:Help for if statements

The nice thing about programming is that if you want to try something, you can.
storm2713687




PostPosted: Thu Feb 28, 2013 8:31 pm   Post subject: Re: RE:Help for if statements

Insectoid @ Wed Feb 27, 2013 7:39 pm wrote:
The nice thing about programming is that if you want to try something, you can.

I was at school so I couldn't use Turing xD
Since there's compsci at high schools, the computer probably had it, but I just didn't know the location of it. Thanks for helping though, guys!
jbking




PostPosted: Fri Mar 01, 2013 12:30 pm   Post subject: Re: Help for if statements

Case would be like what you want so there is a command to do that, if you want to know about it.[/url]
yazdmich




PostPosted: Fri Mar 01, 2013 12:55 pm   Post subject: Re: Help for if statements

Turing:


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing [int, real, nat, string etc] can be inferred, but everything else is declared)
storm2713687




PostPosted: Fri Mar 01, 2013 5:11 pm   Post subject: Re: Help for if statements

Jbking, what do you mean by that?
yazdmich @ Fri Mar 01, 2013 12:55 pm wrote:
Turing:


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing [int, real, nat, string etc] can be inferred, but everything else is declared)

Can you give an example plz? Smile
I don't quite understand that Sad
yazdmich




PostPosted: Fri Mar 01, 2013 5:39 pm   Post subject: Re: Help for if statements

storm2713687 @ Fri Mar 01, 2013 5:11 pm wrote:
Jbking, what do you mean by that?
yazdmich @ Fri Mar 01, 2013 12:55 pm wrote:
Turing:


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing [int, real, nat, string etc] can be inferred, but everything else is declared)

Can you give an example plz? Smile
I don't quite understand that Sad


I fixed your code so it would work in turing. declaring means to say what it is checking, in this case the value of the variable op
Sponsor
Sponsor
Sponsor
sponsor
jbking




PostPosted: Fri Mar 01, 2013 6:17 pm   Post subject: Re: Help for if statements

I mean that the structure of code you want where you just want to state the value and have it automatically do the line you want already exists as a separate command called "case" which you may want to research. From the documentation that should look a lot like what you wanted to some degree if I interpret what you wanted correctly. Here would be an example that is similar to what you wanted:

Turing:

        case mark of
            label 9, 10 :   put "Excellent"
            label 7, 8 :        put "Good"
            label 6 :       put "Fair"
            label :     put "Poor"
        end case
storm2713687




PostPosted: Fri Mar 01, 2013 7:38 pm   Post subject: Re: Help for if statements

yazdmich @ Fri Mar 01, 2013 5:39 pm wrote:
storm2713687 @ Fri Mar 01, 2013 5:11 pm wrote:
Jbking, what do you mean by that?
yazdmich @ Fri Mar 01, 2013 12:55 pm wrote:
Turing:


var op : int
put "Type and enter 1 for operation 1, 2 for operation 2, or 3 for operation 3."
get op
if op = 1 then
    put "1+1=", 1 + 1
elsif op = 2 then
    put "1+2=", 1 + 2
else
    put "1+3=", 1 + 3
end if


You must tell the compiler what variable to read, easy mistake, Turing's compiler is highly declarative. almost nothing is inferred (constant typing [int, real, nat, string etc] can be inferred, but everything else is declared)

Can you give an example plz? Smile
I don't quite understand that Sad


I fixed your code so it would work in turing. declaring means to say what it is checking, in this case the value of the variable op

Ohhh thanks Razz
Is it possible to change else to elsif op = 3?

jbking @ Fri Mar 01, 2013 6:17 pm wrote:
I mean that the structure of code you want where you just want to state the value and have it automatically do the line you want already exists as a separate command called "case" which you may want to research. From the documentation that should look a lot like what you wanted to some degree if I interpret what you wanted correctly. Here would be an example that is similar to what you wanted:

Turing:

        case mark of
            label 9, 10 :   put "Excellent"
            label 7, 8 :        put "Good"
            label 6 :       put "Fair"
            label :     put "Poor"
        end case

I didn't get there in turing yet xD
Thanks for helping though Smile
Raknarg




PostPosted: Sat Mar 02, 2013 1:11 pm   Post subject: RE:Help for if statements

Personally I wouldn't use cases in turing, they're stupid. In most languages you can set ranges, but not in Turing. With bigger ranges you might as well use ifs.
storm2713687




PostPosted: Sat Mar 02, 2013 7:51 pm   Post subject: Re: RE:Help for if statements

Raknarg @ Sat Mar 02, 2013 1:11 pm wrote:
Personally I wouldn't use cases in turing, they're stupid. In most languages you can set ranges, but not in Turing. With bigger ranges you might as well use ifs.


Well now I know another thing not to use in turing Razz


Btw, is it possible to have the if statement something like this:
if variable1 = yes then
put "hi"
elsif variable1 = no then
put "bye"
end if
Raknarg




PostPosted: Sat Mar 02, 2013 7:56 pm   Post subject: RE:Help for if statements

Why not? It's essentially the same as what you were doing before.
storm2713687




PostPosted: Sat Mar 02, 2013 9:25 pm   Post subject: Re: RE:Help for if statements

Raknarg @ Sat Mar 02, 2013 7:56 pm wrote:
Why not? It's essentially the same as what you were doing before.

Well, I just wanted to have a bigger variety of options for my if statements.
Now I have another question Razz
Can I use an if statement within another if statement?
Tony




PostPosted: Sat Mar 02, 2013 9:36 pm   Post subject: Re: RE:Help for if statements

storm2713687 @ Sat Mar 02, 2013 9:25 pm wrote:

Can I use an if statement within another if statement?

Yes. As I've linked to above ? http://compsci.ca/holtsoft/doc/if.html "An ifStatement is:"
Quote:

if trueFalseExpn then
statementsAndDeclarations
{ elsif trueFalseExpn then
statementsAndDeclarations }
[ else
statementsAndDeclarations ]
end if

So inside of an ifStatement you can have any statementsAndDeclarations, which includes other ifStatement.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: