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

Username:   Password: 
 RegisterRegister   
 Short Way of Writing Code
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
EDEN-E




PostPosted: Mon Apr 19, 2004 8:13 pm   Post subject: Short Way of Writing Code

1.
When we use loop statement, we often use some variable to count for some reason.

code:

   var counter : int
   counter := 0
   loop
      counter := counter + 1
      put "Hi!"
      exit when counter = 3
   end loop



The key for counter is "counter := counter + 1", which add 1 to itself.
However, it seems useless to write counter two times.
The short way for this code is
code:

   counter += 1



This way can be applied to substraction, mutiplication, or division.
Also to real type, string type, etc
code:

   makrs -= 1.5

   sentence += "word"

   power *= 2



What happens if you put more than a term on right side?
code:

   compsci += 5 + 1



I hope you know already, above code are exactly same as compsci := compsci + 5 + 1



2.
The next tip is about declaration and assignment of variable.
Do you think following code are efficient?
code:

   var counter : int
   counter := 0



Maybe NOT.
You can make this two steps into one step.
code:

   var counter : int := 0



But there is still one thing that you really don't need to put.
Counter was declared as a integer type and assign to 0 at the same time.
Because compiler know 0 is integer, you really don't need to say "counter is a integer type"
code:

   var counter := 0



Of course, you can declare any types of variable like this.
code:

   var counter := 0
   var word := "Hello"
   var check := true





3.
The last tip is for the use of boolean type in condition.

code:

   var check := true
   if check = true then
      put "check is true"
   end if



Because boolean data can have true or false, you don't need to put "=
true"
code:

   if check then



If you want to say "check = false", which means not true, put not infront of check.
code:

   if not check then





Bonus Tip
When you use "not=", have you thought that it looks ugly or odd for programming language?
Of course, there are another way to say "not=".

code:

   if counter ~= 3

   if ~check then




.
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Mon Apr 19, 2004 9:27 pm   Post subject: (No subject)

nice, I like the ~=. I would have thought that turing would have used !=, considering C++ uses it.

and another thing, to declare a real type variable and set it to 0, do this

code:
var num := 0.0


anyways, +10 bits
Catalyst




PostPosted: Mon Apr 19, 2004 9:33 pm   Post subject: (No subject)

other notes

code:

function FooBar:real
result 0
end FooBar

procedure FooTank
end FooTank


can shortened to


code:

fcn FooBar:real
result 0
end FooBar

proc FooTank
end FooTank
EDEN-E




PostPosted: Mon Apr 19, 2004 10:17 pm   Post subject: (No subject)

AsianSensation wrote:
nice, I like the ~=. I would have thought that turing would have used !=, considering C++ uses it.


yea, most langueges use != for not equal...
turing uses := rather than =

i don't know why holt soft made like this...

hmm... i think... holt soft wanted something differen.. like "Canada" haha
Tony




PostPosted: Mon Apr 19, 2004 10:32 pm   Post subject: (No subject)

well turing also uses = rather than == Laughing

but turing's main advantage is their simple syntax that students supposedly understand better... so who knows Rolling Eyes
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
EDEN-E




PostPosted: Mon Apr 19, 2004 10:42 pm   Post subject: (No subject)

tony wrote:
turing's main advantage is their simple syntax that students supposedly understand better... so who knows Rolling Eyes


turing syntax makes me mad!
i had learned GW-basic, Q-basic, turboC, C++, ...
and... now i am learning turing (for school compsci)

for i : 1 .. 10 so funny

and... there's no something like i++... Evil or Very Mad
no predeclaration ... Mad
which are really useful... hmm...

all programming languages are similar...
it does not take long time to learn another language, if you learned another language..

but turing is so different.....

besides,,, i forgot so many commands for c++... i should look up the book again.


arararkajlsdkfjahfalsdj;fkj
Catalyst




PostPosted: Mon Apr 19, 2004 10:59 pm   Post subject: (No subject)

turing has a lot of advanced features for a learning language
gamer




PostPosted: Wed Apr 21, 2004 5:32 pm   Post subject: (No subject)

yea man i agree....btw why do some people say turin sucks?
Catalyst




PostPosted: Wed Apr 21, 2004 5:41 pm   Post subject: (No subject)

while it has many nice features for a learning laguage, some of its syntax can confuse you once u move up to other languages

for example:
in c,c++ or java using = to compare things actually assigns them (u have to use ==)

Also many of the built it routines and such are slow and theres no way to try to fix them
gamer




PostPosted: Wed Apr 21, 2004 5:58 pm   Post subject: (No subject)

ok i think turing is for beginner programmer, but why wont schools just starting teach c++ or java in the beginning?? it shouldnt be any harder, since students are new to programmin anyway. this way, students can be much beter programmers later on
Paul




PostPosted: Wed Apr 21, 2004 6:35 pm   Post subject: (No subject)

ok, failure rate at my school of ppl who take compsci: 10%
according to my Java experience,
failure rate if my school taught java from the start: 45%
Catalyst




PostPosted: Wed Apr 21, 2004 6:45 pm   Post subject: (No subject)

failure rate at my school ~10%

failure rate if no one cheated ~70% Laughing


The school system seems to have the philosophy (which seems correct) that one of the major hurdles to learning programming is getting comfortable and understanding the syntax. Turing has a very readable syntax almost reading like pseudocode which makes it nice for new programmers.
gamer




PostPosted: Wed Apr 21, 2004 7:34 pm   Post subject: (No subject)

oic...dats why
but still, prsoanlly i would hav rather began with java or sumthin at first, cuz if u began with turing n decided to learn java or c++ later on, i think u'
ll get confused easily n mess up
AsianSensation




PostPosted: Wed Apr 21, 2004 9:01 pm   Post subject: (No subject)

well, learning Java and C with classes requires a new way of thinking. Java is all Object Oriented Programming, and teaching someone that doesn't even have a grasp of the simple concepts OOP will lead them to be even more confused then before.
Catalyst




PostPosted: Wed Apr 21, 2004 9:05 pm   Post subject: (No subject)

both languages can be taught w/o their OOP (java is a bit tricky to do w/o oop tho)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

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


Style:  
Search: