Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Short Way of Writing Code
Author Message
EDEN-E

Posted: 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

.

AsianSensation

Posted: 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

Posted: 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

Posted: 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

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

well turing also uses = rather than ==

but turing's main advantage is their simple syntax that students supposedly understand better... so who knows
Tony's programming blog. DWITE - a programming contest.
EDEN-E

Posted: 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

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++...
no predeclaration ...
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

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

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

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

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

Catalyst

Posted: 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

Posted: 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

Posted: 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

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

failure rate at my school ~10%

failure rate if no one cheated ~70%

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

Posted: 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

Posted: 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

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: