Computer Science Canada

Short Way of Writing Code

Author:  EDEN-E [ 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



<img src=http://www.korea.net//learnaboutkorea/hangeul/images/1_03l.jpg>

<font color=EFF2F8>.</font>

Author:  AsianSensation [ Mon Apr 19, 2004 9:27 pm ]
Post 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

Author:  Catalyst [ Mon Apr 19, 2004 9:33 pm ]
Post 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

Author:  EDEN-E [ Mon Apr 19, 2004 10:17 pm ]
Post 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

Author:  Tony [ Mon Apr 19, 2004 10:32 pm ]
Post 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

Author:  EDEN-E [ Mon Apr 19, 2004 10:42 pm ]
Post 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

Author:  Catalyst [ Mon Apr 19, 2004 10:59 pm ]
Post subject: 

turing has a lot of advanced features for a learning language

Author:  gamer [ Wed Apr 21, 2004 5:32 pm ]
Post subject: 

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

Author:  Catalyst [ Wed Apr 21, 2004 5:41 pm ]
Post 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

Author:  gamer [ Wed Apr 21, 2004 5:58 pm ]
Post 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

Author:  Paul [ Wed Apr 21, 2004 6:35 pm ]
Post 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%

Author:  Catalyst [ Wed Apr 21, 2004 6:45 pm ]
Post 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.

Author:  gamer [ Wed Apr 21, 2004 7:34 pm ]
Post 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

Author:  AsianSensation [ Wed Apr 21, 2004 9:01 pm ]
Post 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.

Author:  Catalyst [ Wed Apr 21, 2004 9:05 pm ]
Post subject: 

both languages can be taught w/o their OOP (java is a bit tricky to do w/o oop tho)

Author:  gamer [ Wed Apr 21, 2004 9:24 pm ]
Post subject: 

oic...owell im still stuck with turing for now

Author:  MyPistolsIn3D [ Thu Apr 29, 2004 3:37 pm ]
Post subject: 

Well, at my school it goes from classic turing to oot, then they teach java in grade 12. without doing the turing first, java is harder.

Author:  wtd [ Fri Jul 16, 2004 8:46 pm ]
Post subject: 

Plenty of other languages use := for assignment, though the term also used sometimes is "binding", as in binding a name to an object or value.

Since = makes much more sense that == for evaluating constants, the typical approach is to use something else for assignment/binding, which is why := is used by many programming languages.

As for ~= vs. !=, there is history to support that as well. Typically ~ in low-level (assembly) languages is used to represent a "not" operation. In languages without operators like += and -= (one example would be Eiffel, and if memory serves, Fortran90), /= is sometimes used to represent inequality.


: