Forum > Beginners
ways to avoid begin-end’s?
Kays:
--- Quote from: Weiss on May 08, 2024, 10:26:09 pm ---ways to avoid begin-end’s?
--- End quote ---
Opening a can of… spaghetti: It is called goTo.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---label 9999;begin if x <> 1 then goto 9999; x := 2; doSomething 9999: ;Yeehaw, no (extra) begin … end anymore.
Weiss:
using my example above, begin-end could be avoided by passing argument like so
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if x=1 then doSomething(2);
Thaddy:
--- Quote from: Weiss on May 08, 2024, 10:26:09 pm ---I am thinking, there are quite often times when there are only two statements between begin-end encapsulation. Wouldn’t it be neat if I could say something like
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if x=1 then x:=2 AND DoSomething(x)
--- End quote ---
But that is already legal assuming that DoSomething is a function that returns an ordinal value. :D
dbannon:
--- Quote from: Thaddy on May 09, 2024, 05:50:02 am ---....
But that is already legal assuming that DoSomething is a function that returns an ordinal value. :D
--- End quote ---
Yes, to my surprise, it compiles. That means the compiler sees x:=2 as something that returns an ordinal value ? But not (x:=2) ?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if x=1 then (x:=2) AND DoSomething(x); // does not compile
Davo
Weiss:
however, something like
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if X=1 then X:=2 and i
does compile, and, like Thaddy mentioned, if doSomething is a function
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function doSomething : integerbeginwriteln('What Thaddy says goes');end;
then this
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if x=1 then x:=2 and doSomething
compiles and runs fine, prints on screen.
Navigation
[0] Message Index
[#] Next page
[*] Previous page