Recent

Author Topic: A lot If-Else-If  (Read 10364 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: i don't know why
« Reply #15 on: May 10, 2018, 07:56:57 pm »
@Handoko
"binned away" ...
This was my reaction to this:

Hello, i got this code ...

I assumed NightWing had been given that code and was trying to make sense of it, and get it to compile, in the mistaken belief that it was a good or educational example with some merit.
I don't think a complete beginner in Pascal (which he wrote that he was) would come up with such a long and convoluted spaghetti-like offering as he shared as one of his first attempts.

It is important in this forum not only to offer good example code, that others can usefully profit from, but to be honest (without being gratuitously rude) about code that is badly designed, poorly formatted, and a potential maintenance nightmare. Apart from the fact that in this case the code was so badly thought out that the coder tripped himself up, so that it does not even get past the FPC parser and compile.
If people are offended when valid criticism is offered about the code they share here, then they should not post it where hundreds of programmers, many far more experienced than themselves, will view it.

af0815

  • Hero Member
  • *****
  • Posts: 1289
Re: i don't know why
« Reply #16 on: May 10, 2018, 09:03:02 pm »
Hehe, if the guy use this without understanding, he will have an unwanted result :-)

Care to explain?

..
Simple: I have not read carefully enough ... i thought, it is weights of planets not bytes. -> my mistake.
After translating the first sentence from spanish to german, i see it.
regards
Andreas

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: i don't know why
« Reply #17 on: May 10, 2018, 09:50:46 pm »
... i thought, it is weights of planets not bytes. -> my mistake.

Exactly my thoughts when I glanced at the code the first time, also the reason I did not reply to the thread at that time, since I thought it had to do with astrology...

It's like obfuscated C  >:D

Bart

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: i don't know why
« Reply #18 on: May 10, 2018, 10:47:19 pm »
[…] i thought, it is weights of planets not bytes. -> my mistake.  […]
No, af0815, not weights, but masses are measured in Bytes. Didn't you know that?  :D More to chuckle about: triple F system.
« Last Edit: May 10, 2018, 10:48:50 pm by Kays »
Yours Sincerely
Kai Burghardt

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: i don't know why
« Reply #19 on: May 11, 2018, 12:08:55 am »
First the Pascal file extension is lpr for projects or pas/pp for units, this way you get syntax highlighting. The features described below only work if the file is recognized as a pascal source by the editor.

To your initial problem: Lazarus has two really handy features which really help in this case:

1. Go to Options-> Editor -> Display -> Markup and Matches and select Outline (global)
Now it will highlight matching begin end [else] blocks. This also works other keywords. Now it should be easy to see where your problem is (Obviously an end in line 263)

2. You can auto format your source with Ctrl+D so you do not need  to do this by hand if you have a file you didn't write yourself. I doesn't seem to work on your code though. Probably because of the errors? I rarely use this feature myself.

Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: i don't know why
« Reply #20 on: May 11, 2018, 03:42:39 am »
@howardpc, @Bart

Please don't get me wrong. I also agree with you, that code is poorly written. But we should at least letting the OP know why it is not good, how to avoid to write code like that bad code. For example avoiding to use too many if-then-else levels, how to proper format code, try to use simpler logic to solve the problem, etc.

~ Peace ~

Note:
I am also a part-time teacher, some of my students were not very smart. That forces me to understand and see things from their view point.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: i don't know why
« Reply #21 on: May 11, 2018, 11:01:55 pm »

NightWing

  • Newbie
  • Posts: 5
Re: i don't know why
« Reply #22 on: May 20, 2018, 07:36:35 pm »
Well,  I was very busy with supercomputing stuff and I did not have much time to work on the code but I managed to correct the if-else-if and I think I'm beginning to understand the use of the ';'
Thanks for your advice, they helped me to give me an idea, already compiles the program but I still need to correct the outputs of some orphaned variables

Code: Pascal  [Select][+][-]
  1. program puff;
  2. uses crt;
  3. var
  4.         mercurio, tierra, marte, jupiter, saturno, urano, neptuno, neptunoB, neptunoK, neptunoM, neptunoG, neptunoT, neptunoP, neptunoMB, neptunoMK, neptunoMM, neptunoMG, neptunoMT, neptunoMP, pluton : int64;               
  5.         venus : string;
  6. begin
  7.         writeln('Hello there!');
  8.         writeln('Ingrese la cantidad de bytes a convertir: ');
  9.         readln(mercurio);
  10.  
  11.         writeln('');
  12.         writeln('¿A que unidad los quiere convertir?');
  13.         writeln(' k = kilobytes, m = megabytes, g = gigabytes, t = terabytes, p = petabytes, c = exacto / -v, e = salir ');
  14.         readln(venus);
  15.  
  16.         case venus of
  17.  
  18.                 { Convertir a kilobytes }      
  19.  
  20.                 'k' : begin
  21.                         tierra := mercurio div 1024;
  22.                         {pluton := tierra mod 1024;}
  23.                         writeln('El resultado de convertir ',mercurio,' bytes a kilobytes es: ',tierra,'.');
  24.                 end;
  25.  
  26.                 { Convertir a megabytes }
  27.  
  28.                 'm' : begin
  29.                         marte := mercurio div 1048576;
  30.                         {pluton := tierra mod 1024;}
  31.                         writeln('El resultado de convertir ',mercurio,' bytes a megabytes es: ',marte,'.');
  32.                 end;
  33.  
  34.                 { Convertir a gigabytes }
  35.  
  36.                 'g' : begin
  37.                         jupiter := mercurio div 1073741824;
  38.                         {pluton := tierra mod 1024;}
  39.                         writeln('El resultado de convertir ',mercurio,' bytes a megabytes es: ',jupiter,'.');
  40.                 end;
  41.  
  42.                 { Convertir a terabytes }
  43.  
  44.                 't' : begin
  45.                         saturno := mercurio div 1099511627776;
  46.                         {pluton := tierra mod 1024;}
  47.                         writeln('El resultado de convertir ',mercurio,' bytes a terabytes es: ',saturno,'.');
  48.                 end;
  49.  
  50.                 { Convertir a petabytes }
  51.  
  52.                 'p' : begin
  53.                         urano := mercurio div 1125899906842624;
  54.                         {pluton := tierra mod 1024;}
  55.                         writeln('El resultado de convertir ',mercurio,' bytes a petabytes es: ',urano,'.');
  56.                 end;
  57.  
  58.                 { Conversion exacta / -v }
  59.  
  60.                 'c' : begin
  61.  
  62.         { /*************************************************************************/ }
  63.                                 { Petabytes Root }
  64.         { /*************************************************************************/ }
  65.  
  66.                         if (mercurio >= 1125899906842624) then
  67.                         begin
  68.                                 neptunoP := mercurio div 1125899906842624;
  69.                                 neptunoMP := mercurio mod 1125899906842624;
  70.  
  71.                                 { Solo para verificar que no se ingreso una cantidad exacta de petabytes }
  72.                                 if (neptunoMP > 0) then
  73.                                 begin
  74.                                         { Terabyte }
  75.                                         if (neptunoMP >= 1099511627776) and (neptunoMP < 1125899906842624) then
  76.                                         begin
  77.                                                 neptunoT := neptunoMP div 1099511627776;
  78.                                                 neptunoMT := neptunoMP mod 1099511627776;                              
  79.                                                
  80.                                                 { Gigabyte }
  81.                                                 if (neptunoMT >= 1073741824) then
  82.                                                 begin                                          
  83.                                                         {codigo si el residuo es mayor que un terabyte}
  84.                                                         neptunoG := neptunoMT div 1073741824;
  85.                                                         neptunoMG := neptunoMT mod 1073741824;
  86.                                                        
  87.                                                         { Megabyte }    
  88.                                                         if (neptunoMG > 1048576) then
  89.                                                         begin                                          
  90.                                                                 {codigo si el residuo es mayor que un gigabyte}
  91.                                                                 neptunoM := neptunoMG div 1048576;
  92.                                                                 neptunoMM := neptunoMG mod 1048576;
  93.                                                                
  94.                                                                 { Kilobyte }           
  95.                                                                 if (neptunoMM >= 1024) then
  96.                                                                 begin
  97.                                                                         {codigo si el residuo es mayor que un megabyte}
  98.                                                                         neptunoK := neptunoMM div 1024;
  99.                                                                         neptunoMK := neptunoMM mod 1024;
  100.                                                                        
  101.                                                                         {  Byte }                      
  102.                                                                         if (neptunoMK < 1024) then
  103.                                                                         begin                                                          
  104.                                                                                 {codigo si el residuo es mayor que un kilobyte}
  105.                                                                                 neptunoB := neptunoMK;
  106.                                                                                 {neptunoMB := neptunoMK mod 1024;}
  107.                                                                         end;
  108.                                                                 end
  109.                                                                
  110.                                                                 { Byte }                       
  111.                                                                 else if (neptunoMM < 1024) then
  112.                                                                 begin
  113.                                                                         {codigo si el residuo es mayor que un kilobyte}
  114.                                                                         neptunoB := neptunoMM;
  115.                                                                         {neptunoMB := neptunoMM mod 1024;}
  116.                                                                 end
  117.                                                         end
  118.                                                        
  119.                                                         { Kilobyte }           
  120.                                                         else if (neptunoMG >= 1024) then
  121.                                                         begin                                                  
  122.                                                                 {codigo si el residuo es mayor que un megabyte}
  123.                                                                 neptunoK := neptunoMG div 1024;
  124.                                                                 neptunoMK := neptunoMG mod 1024;
  125.                                                
  126.                                                                 { Byte }                       
  127.                                                                 if (neptunoMK < 1024) then
  128.                                                                 begin
  129.                                                                         {codigo si el residuo es mayor que un kilobyte}
  130.                                                                         neptunoB := neptunoMK;
  131.                                                                 end;
  132.                                                         end
  133.                                                        
  134.                                                         { Byte }                       
  135.                                                         else if (neptunoMG < 1024) then
  136.                                                         begin
  137.                                                                 neptunoB := neptunoMG;
  138.                                                         end
  139.                                                 end
  140.                                                
  141.                                                 { Megabyte }    
  142.                                                 else if (neptunoMT > 1048576) then
  143.                                                 begin
  144.                                                         {codigo si el residuo es mayor que un gigabyte}
  145.                                                         neptunoM := neptunoMT div 1048576;
  146.                                                         neptunoMM := neptunoMT mod 1048576;
  147.                                                
  148.                                                         { Kilobyte }           
  149.                                                         if (neptunoMM >= 1024) then
  150.                                                         begin
  151.                                                                 {codigo si el residuo es mayor que un megabyte}
  152.                                                                 neptunoK := neptunoMM div 1024;
  153.                                                                 neptunoMK := neptunoMM mod 1024;
  154.                                                
  155.                                                                 { Byte }                       
  156.                                                                 if (neptunoMK < 1024) then
  157.                                                                 begin                                                  
  158.                                                                         {codigo si el residuo es mayor que un kilobyte}
  159.                                                                         neptunoB := neptunoMK;
  160.                                                                         {neptunoMB := neptunoMK mod 1024;}
  161.                                                                 end;
  162.                                                         end
  163.                                                        
  164.                                                         { Byte }                       
  165.                                                         else if (neptunoMM < 1024) then
  166.                                                         begin                                                  
  167.                                                                 {codigo si el residuo es mayor que un kilobyte}
  168.                                                                 neptunoB := neptunoMM;
  169.                                                                 {neptunoMB := neptunoMM mod 1024;}
  170.                                                         end
  171.                                                 end
  172.                                                        
  173.                                                 { Kilobyte }           
  174.                                                 else if (neptunoMT > 1024) then
  175.                                                 begin                                          
  176.                                                         {codigo si el residuo es mayor que un megabyte}
  177.                                                         neptunoK := neptunoMT div 1024;
  178.                                                         neptunoMK := neptunoMT mod 1024;
  179.                                        
  180.                                                         {  Byte }                      
  181.                                                         if (neptunoMK < 1024) then
  182.                                                         begin                                          
  183.                                                                 {codigo si el residuo es mayor que un kilobyte}
  184.                                                                 neptunoB := neptunoMK;
  185.                                                                 {neptunoMB := neptunoMK mod 1024;}
  186.                                                         end;
  187.                                                 end
  188.                                                
  189.                                                 { Byte }                       
  190.                                                 else if (neptunoMT < 1024) then
  191.                                                 begin
  192.                                                         {codigo si el residuo es mayor que un kilobyte}
  193.                                                         neptunoB := neptunoMT;
  194.                                                         {neptunoMB := neptunoMT mod 1024;}
  195.                                                 end
  196.                                         end
  197.                                                                                
  198.                                         { Gigabytes d Petabytes}
  199.                                         else if (neptunoMP > 1073741824) then
  200.                                         begin                                  
  201.                                                 {codigo si el residuo es mayor que un Gigabyte}
  202.                                                 neptunoG := neptunoMP div 1073741824;
  203.                                                 neptunoMG := neptunoMP mod 1073741824;
  204.                                            
  205.                                                 { Megabyte }    
  206.                                                 if (neptunoMG > 1048576) then
  207.                                                 begin                                  
  208.                                                         {codigo si el residuo es mayor que un Megabyte}
  209.                                                         neptunoM := neptunoMG div 1048576;
  210.                                                         neptunoMM := neptunoMG mod 1048576;
  211.                                        
  212.                                                         { Kilobyte }           
  213.                                                         if (neptunoMM >= 1024) then
  214.                                                         begin                                          
  215.                                                                 {codigo si el residuo es mayor que un Kilobyte}
  216.                                                                 neptunoK := neptunoMM div 1024;
  217.                                                                 neptunoMK := neptunoMM mod 1024;
  218.                                        
  219.                                                                 {  Byte }                      
  220.                                                                 if (neptunoMK < 1024) then
  221.                                                                 begin                                                  
  222.                                                                         {codigo si el residuo es mayor que un Byte}
  223.                                                                         neptunoB := neptunoMK;
  224.                                                                         {neptunoMB := neptunoMK mod 1024;}
  225.                                                                 end;
  226.                                                         end
  227.                                                        
  228.                                                         {  Byte }                      
  229.                                                         else if (neptunoMM < 1024) then
  230.                                                         begin
  231.                                                                 {codigo si el residuo es mayor que un kilobyte}
  232.                                                                 neptunoB := neptunoMM;
  233.                                                                 {neptunoMB := neptunoMM mod 1024;}
  234.                                                         end;
  235.                                                 end
  236.  
  237.                                                 { Kilobyte }           
  238.                                                 else if (neptunoMG >= 1024) then
  239.                                                 begin                                          
  240.                                                         {codigo si el residuo es mayor que un megabyte}
  241.                                                         neptunoK := neptunoMG div 1024;
  242.                                                         neptunoMK := neptunoMG mod 1024;
  243.                                        
  244.                                                         {  Byte }                      
  245.                                                         if (neptunoMK < 1024) then
  246.                                                         begin                                          
  247.                                                                 {codigo si el residuo es mayor que un kilobyte}
  248.                                                                 neptunoB := neptunoMK;
  249.                                                                 {neptunoMB := neptunoMK mod 1024;}
  250.                                                         end;
  251.                                                 end
  252.                                                
  253.                                                 { Byte }                       
  254.                                                 else if (neptunoMG < 1024) then
  255.                                                 begin
  256.                                                         {codigo si el residuo es mayor que un kilobyte}
  257.                                                         neptunoB := neptunoMG;
  258.                                                         {neptunoMB := neptunoMG mod 1024;}
  259.                                                 end;
  260.                                         end
  261.                                        
  262.                                         { Megabyte d Petabytes }    
  263.                                         else if (neptunoMP > 1048576) then
  264.                                         begin                                  
  265.                                                 {codigo si el residuo es mayor que un gigabyte}
  266.                                                 neptunoM := neptunoMP div 1048576;
  267.                                                 neptunoMM := neptunoMP mod 1048576;
  268.                                        
  269.                                                 { Kilobyte }           
  270.                                                 if (neptunoMM >= 1024) then
  271.                                                 begin
  272.                                                         {codigo si el residuo es mayor que un megabyte}
  273.                                                         neptunoK := neptunoMM div 1024;
  274.                                                         neptunoMK := neptunoMM mod 1024;
  275.                        
  276.                                                         {  Byte }                      
  277.                                                         if (neptunoMK < 1024) then
  278.                                                         begin                                          
  279.                                                                 {codigo si el residuo es mayor que un kilobyte}
  280.                                                                 neptunoB := neptunoMK;
  281.                                                                 {neptunoMB := neptunoMK mod 1024;}
  282.                                                         end;
  283.                                                 end
  284.                                                
  285.                                                 {  Byte }                      
  286.                                                 else if (neptunoMM < 1024) then
  287.                                                 begin                                          
  288.                                                         {codigo si el residuo es mayor que un kilobyte}
  289.                                                         neptunoB := neptunoMM;
  290.                                                         {neptunoMB := neptunoMM mod 1024;}
  291.                                                 end;
  292.                                         end
  293.                                                
  294.                                         { Kilobyte d Petabytes}        
  295.                                         else if (neptunoMP > 1024) then
  296.                                         begin
  297.                                                 {codigo si el residuo es mayor que un megabyte}
  298.                                                 neptunoK := neptunoMP div 1024;
  299.                                                 neptunoMK := neptunoMP mod 1024;
  300.                                
  301.                                                 { Byte }                       
  302.                                                 if (neptunoMK < 1024) then
  303.                                                 begin
  304.                                                         neptunoB := neptunoMK;
  305.                                                 end;
  306.                                         end
  307.                                        
  308.                                         { Byte d Petabytes }                   
  309.                                         else if (neptunoMP < 1024) then
  310.                                         begin
  311.                                                 {codigo si el residuo es menor que un kilobyte}
  312.                                                 neptunoB := neptunoMP
  313.                                                 {neptunoMB := neptunoMP mod 1024;}
  314.                                         end;
  315.                                 end
  316.  
  317.                                 else if (mercurio <1) then
  318.                                 begin
  319.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoP,' Petabytes')
  320.                                 end;
  321.                         end
  322.  
  323.         { /*************************************************************************/ }
  324.  
  325.         { aqui termina el universo de terabyte y comienza el surgimiento del reino de Terabyte }
  326.  
  327.         { /*************************************************************************/ }
  328.                                 { Terabyte Root }
  329.         { /*************************************************************************/ }
  330.  
  331.                         else if (mercurio >= 1099511627776) then
  332.                         begin
  333.                                 neptunoT := mercurio div 1099511627776;
  334.                                 neptunoMT := mercurio mod 1099511627776;                              
  335.  
  336.                                 { Solo para verificar que no se ingreso una cantidad exacta de Terabytes }
  337.                                 if (neptunoMT > 0) then
  338.                                 begin
  339.                
  340.                                         { Gigabyte }
  341.                                         if (neptunoMT > 1073741824) then
  342.                                         begin
  343.                                                 {codigo si el residuo es mayor que un terabyte}
  344.                                                 neptunoG := neptunoMT div 1073741824;
  345.                                                 neptunoMG := neptunoMT mod 1073741824;
  346.                                            
  347.                                                 { Megabyte }    
  348.                                                 if (neptunoMG > 1048576) then
  349.                                                 begin
  350.                                                         {codigo si el residuo es mayor que un gigabyte}
  351.                                                         neptunoM := neptunoMG div 1048576;
  352.                                                         neptunoMM := neptunoMG mod 1048576;
  353.                                        
  354.                                                         { Kilobyte }           
  355.                                                         if (neptunoMM >= 1024) then
  356.                                                         begin
  357.                                                                 {codigo si el residuo es mayor que un megabyte}
  358.                                                                 neptunoK := neptunoMM div 1024;
  359.                                                                 neptunoMK := neptunoMM mod 1024;
  360.                                                
  361.                                                                 {  Byte }                      
  362.                                                                 if (neptunoMK < 1024) then
  363.                                                                 begin
  364.                                                                         {codigo si el residuo es mayor que un kilobyte}
  365.                                                                         neptunoB := neptunoMK;
  366.                                                                         {neptunoMB := neptunoMK mod 1024;}
  367.                                                                 end;
  368.                                                         end
  369.                                                        
  370.                                                         { Byte }                       
  371.                                                         else if (neptunoMM < 1024) then
  372.                                                         begin
  373.                                                                 {codigo si el residuo es mayor que un kilobyte}
  374.                                                                 neptunoB := neptunoMM;
  375.                                                                 {neptunoMB := neptunoMM mod 1024;}
  376.                                                         end;
  377.                                                 end
  378.  
  379.                                                 { Kilobyte }           
  380.                                                 else if (neptunoMG >= 1024) then
  381.                                                 begin
  382.                                                         {codigo si el residuo es mayor que un megabyte}
  383.                                                         neptunoK := neptunoMG div 1024;
  384.                                                         neptunoMK := neptunoMG mod 1024;
  385.                                                        
  386.                                                         {  Byte }                      
  387.                                                         if (neptunoMK < 1024) then
  388.                                                         begin
  389.                                                                 {codigo si el residuo es mayor que un kilobyte}
  390.                                                                 neptunoB := neptunoMK;
  391.                                                                 {neptunoMB := neptunoMK mod 1024;}
  392.                                                         end;
  393.                                                 end
  394.                                                
  395.                                                 {  Byte }                      
  396.                                                 else if (neptunoMG < 1024) then
  397.                                                 begin                          
  398.                                                         {codigo si el residuo es mayor que un kilobyte}
  399.                                                         neptunoB := neptunoMG;
  400.                                                         {neptunoMB := neptunoMG mod 1024;}
  401.                                                 end;
  402.                                         end
  403.                                        
  404.                                         { Megabyte }    
  405.                                         else if (neptunoMT > 1048576) then
  406.                                         begin
  407.                                                 {codigo si el residuo es mayor que un gigabyte}
  408.                                                 neptunoM := neptunoMT div 1048576;
  409.                                                 neptunoMM := neptunoMT mod 1048576;
  410.                                        
  411.                                                 { Kilobyte }           
  412.                                                 if (neptunoMM >= 1024) then
  413.                                                 begin
  414.                                                         {codigo si el residuo es mayor que un megabyte}
  415.                                                         neptunoK := neptunoMM div 1024;
  416.                                                         neptunoMK := neptunoMM mod 1024;
  417.                                                
  418.                                                         { Byte }                       
  419.                                                         if (neptunoMK < 1024) then
  420.                                                         begin
  421.                                                                 {codigo si el residuo es mayor que un kilobyte}
  422.                                                                 neptunoB := neptunoMK;
  423.                                                                 {neptunoMB := neptunoMK mod 1024;}
  424.                                                         end;
  425.                                                 end
  426.                                                
  427.                                                 {  Byte }                      
  428.                                                 else if (neptunoMM < 1024) then
  429.                                                 begin
  430.                                                         {codigo si el residuo es mayor que un kilobyte}
  431.                                                         neptunoB := neptunoMM;
  432.                                                         {neptunoMB := neptunoMM mod 1024;}
  433.                                                 end;
  434.                                         end
  435.                                                                
  436.                                         { Kilobyte }           
  437.                                         else if (neptunoMT > 1024) then
  438.                                         begin
  439.                                                 {codigo si el residuo es mayor que un megabyte}
  440.                                                 neptunoK := neptunoMT div 1024;
  441.                                                 neptunoMK := neptunoMT mod 1024;
  442.                                
  443.                                                 {  Byte }                      
  444.                                                 if (neptunoMK < 1024) then
  445.                                                 begin
  446.                                                         {codigo si el residuo es mayor que un kilobyte}
  447.                                                         neptunoB := neptunoMK;
  448.                                                         {neptunoMB := neptunoMK mod 1024;}
  449.                                                 end;
  450.                                         end
  451.                                        
  452.                                         { Byte }                       
  453.                                         else if (neptunoMT < 1024) then
  454.                                         begin
  455.                                                 {codigo si el residuo es mayor que un kilobyte}
  456.                                                 neptunoB := neptunoMT;
  457.                                                 {neptunoMB := neptunoMT mod 1024;}
  458.                                         end;
  459.                                 end
  460.                                 else
  461.                                 begin
  462.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoT,' Terabytes');
  463.                                 end;
  464.                         end
  465.  
  466.         { /*************************************************************************/ }
  467.                                 { Gigabytes Root }
  468.         { /*************************************************************************/ }
  469.  
  470.                         else if (mercurio > 1073741824) then
  471.                         begin
  472.                                 {codigo si el residuo es mayor que un terabyte}
  473.                                 neptunoG := mercurio div 1073741824;
  474.                                 neptunoMG := mercurio mod 1073741824;
  475.  
  476.                                 { Solo para verificar que no se ingreso una cantidad exacta de Gigabytes }
  477.                                 if (neptunoMG > 0) then                            
  478.                                 begin
  479.                                         { Megabyte }    
  480.                                         if (neptunoMG > 1048576) then
  481.                                         begin
  482.                                                 {codigo si el residuo es mayor que un gigabyte}
  483.                                                 neptunoM := neptunoMG div 1048576;
  484.                                                 neptunoMM := neptunoMG mod 1048576;
  485.                                
  486.                                                 { Kilobyte }           
  487.                                                 if (neptunoMM >= 1024) then
  488.                                                 begin
  489.                                                         {codigo si el residuo es mayor que un megabyte}
  490.                                                         neptunoK := neptunoMM div 1024;
  491.                                                         neptunoMK := neptunoMM mod 1024;
  492.                                
  493.                                                         {  Byte }                      
  494.                                                         if (neptunoMK < 1024) then
  495.                                                         begin
  496.                                                                 {codigo si el residuo es mayor que un kilobyte}
  497.                                                                 neptunoB := neptunoMK;
  498.                                                                 {neptunoMB := neptunoMK mod 1024;}
  499.                                                         end;
  500.                                                 end
  501.  
  502.                                                 { Byte }                       
  503.                                                 else if (neptunoMM < 1024) then
  504.                                                 begin
  505.                                                         {codigo si el residuo es mayor que un kilobyte}
  506.                                                         neptunoB := neptunoMM;
  507.                                                         {neptunoMB := neptunoMM mod 1024;}
  508.                                                 end;
  509.                                         end
  510.  
  511.                                         { Kilobyte }           
  512.                                         else if (neptunoMG >= 1024) then
  513.                                         begin
  514.                                                 {codigo si el residuo es mayor que un megabyte}
  515.                                                 neptunoK := neptunoMG div 1024;
  516.                                                 neptunoMK := neptunoMG mod 1024;
  517.                                
  518.                                                 {  Byte }                      
  519.                                                 if (neptunoMK < 1024) then
  520.                                                 begin
  521.                                                         {codigo si el residuo es mayor que un kilobyte}
  522.                                                         neptunoB := neptunoMK;
  523.                                                         {neptunoMB := neptunoMK mod 1024;}
  524.                                                 end;
  525.                                         end
  526.                                        
  527.                                         {  Byte }                      
  528.                                         else if (neptunoMG < 1024) then
  529.                                         begin
  530.                                                 {codigo si el residuo es mayor que un kilobyte}
  531.                                                 neptunoB := neptunoMG;
  532.                                                 {neptunoMB := neptunoMG mod 1024;}
  533.                                         end;
  534.                                 end
  535.                                 else
  536.                                 begin
  537.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoT,' gIGAbytes');
  538.                                 end;
  539.                         end
  540.  
  541.         { /*************************************************************************/ }
  542.                                 { Megabyte Root }    
  543.         { /*************************************************************************/ }
  544.  
  545.                         else if (mercurio > 1048576) then
  546.                         begin
  547.                                 {codigo si el residuo es mayor que un gigabyte}
  548.                                 neptunoM := mercurio div 1048576;
  549.                                 neptunoMM := mercurio mod 1048576;
  550.                                
  551.                                 { Solo para verificar que no se ingreso una cantidad exacta de petabytes }
  552.                                 if (neptunoMP > 0) then
  553.                                 begin
  554.                                         { Kilobyte }           
  555.                                         if (neptunoMM >= 1024) then
  556.                                         begin
  557.                                                 {codigo si el residuo es mayor que un megabyte}
  558.                                                 neptunoK := neptunoMM div 1024;
  559.                                                 neptunoMK := neptunoMM mod 1024;
  560.                
  561.                                                 {  Byte }                      
  562.                                                 if (neptunoMK < 1024) then
  563.                                                 begin
  564.                                                         {codigo si el residuo es mayor que un kilobyte}
  565.                                                         neptunoB := neptunoMK;
  566.                                                         {neptunoMB := neptunoMK mod 1024;}
  567.                                                 end;
  568.                                         end
  569.                                        
  570.                                         { Byte }                       
  571.                                         else if (neptunoMM < 1024) then
  572.                                         begin
  573.                                                 {codigo si el residuo es mayor que un kilobyte}
  574.                                                 neptunoB := neptunoMM;
  575.                                                 {neptunoMB := neptunoMM mod 1024;}
  576.                                         end;
  577.                                 end
  578.                                 else
  579.                                 begin
  580.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoT,' Gigabytes');
  581.                                 end;
  582.                         end
  583.  
  584.         { /*************************************************************************/ }                                
  585.                         { Kilobyte Root }
  586.         { /*************************************************************************/ }
  587.                
  588.                         else if (mercurio >= 1024) then
  589.                         begin
  590.                                 {codigo si el residuo es mayor que un megabyte}
  591.                                 neptunoK := mercurio div 1024;
  592.                                 neptunoMK := mercurio mod 1024;
  593.                
  594.                                 {  Byte }                      
  595.                                 if (neptunoMK < 1024) then
  596.                                 begin
  597.                                         {codigo si el residuo es mayor que un kilobyte}
  598.                                         neptunoB := neptunoMK;
  599.                                         {neptunoMB := neptunoMK mod 1024;}
  600.                                 end;
  601.                         end
  602.                
  603.         { /*************************************************************************/ }
  604.                         { Byte Root }                  
  605.         { /*************************************************************************/ }
  606.  
  607.                         else if (mercurio < 1024) then
  608.                         begin
  609.                                 {codigo si el residuo es mayor que un kilobyte}
  610.                                 neptunoB := mercurio;
  611.                                 {neptunoMB := mercurio mod 1024;}
  612.                         end
  613.                 {/*******************************************************/}
  614.  
  615.  
  616.                         { Rara ocasion en la que ingresen una cantidad exacta de bytes de un petabyte }
  617.                         else
  618.                         begin
  619.                                 writeln('La conversion de ',mercurio,' bytes es: ',neptunoP,' petabytes exactos.');    
  620.                         end;
  621.                 end;
  622.                
  623.                 'e' : begin
  624.                         writeln('Hasta luego!')
  625.                 end;
  626.         end;
  627.  
  628.         writeln('END_OF_FILE__');
  629. end.
  630.  


I realized that when I compile exactly the same code, in archlinux without specifying the target system compiles it and returns the executable while in CentOs specifying the target system (by default since I do not remember moving anything to the configuration) it returns an error and does not generate the executable.


BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: i don't know why
« Reply #23 on: May 20, 2018, 10:55:39 pm »
Did you try to use procedures and/or functions?.
Your code will more easy to read and understand.
For example, in case 'C' entry yoy have a long code. Put in a procedure, as I said will be more easy to read, understand and debug.

/BlueIcaro

NightWing

  • Newbie
  • Posts: 5
Re: i don't know why
« Reply #24 on: May 21, 2018, 12:33:08 am »
Did you try to use procedures and/or functions?.
Your code will more easy to read and understand.
For example, in case 'C' entry yoy have a long code. Put in a procedure, as I said will be more easy to read, understand and debug.

/BlueIcaro

Of course, but I was looking to learn how to nest several if-else-if & the general use of the ' ; ' . Since its use is different than other languages i.e. 'C' or 'Matlab'.
I'm new to pascal but not in programming.

You do not learn to run before you walk.. ¿right?  8-)

NightWing

  • Newbie
  • Posts: 5
Re: A lot If-Else-If
« Reply #25 on: May 31, 2018, 03:35:39 am »
This code works, not as the syntax says it works but it works.

Thanks for the help

P.S.  I exceeded the character limit, that's why I post it in document
P.S2. I say bits no bytes

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: A lot If-Else-If
« Reply #26 on: May 31, 2018, 05:06:32 am »
I tested the code, it compiles and runs. It seems you now already understand how to use nested if-then-else. So, if you want to improve your skill further you should re-read the posts from the beginning. All of their advices are true, the most notable problem is, you're doing a simple thing using a not simple solution. It makes the code harder to maintain and prone to bugs.

For your information, this is how to attach code to the forum:
Copy all the files into a new folder, except: the binary, *.exe, *.bak and the lib folder. Compress that folder and send the zip file to the forum.

And these are my recommend reading materials for beginners:

Lazarus tutorial for beginners:
http://wiki.lazarus.freepascal.org/Lazarus_Tutorial

List of tutorials with wide range of topics (graphics, database, printer, web, etc):
http://wiki.freepascal.org/Lazarus_Documentation#Lazarus.2FFPC_.28Free_Pascal.29
http://wiki.freepascal.org/Category:Tutorials

(Free) Pascal reference guide:
http://freepascal.org/docs-html/current/ref/ref.html

If you're looking for step-by-step Pascal tutorial, try these: http://www.pascal-programming.info/index.php
http://www.taoyue.com/tutorials/pascal/index.html

Some cool Pascal tricks:
http://lazplanet.blogspot.co.id/

Pascal video tutorials:
https://devstructor.com/index.php?page=tutorials

Lazarus IDE tricks (some are with animation):
http://wiki.freepascal.org/New_IDE_features_since

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: A lot If-Else-If
« Reply #27 on: May 31, 2018, 10:52:03 am »
Ok, you fixed it, but I have an advise to make:

It is not a good idea to write so much code inside a CASE.  Instead you should create several PROCEDUREs or FUNCTIONs to do the work and call them from the CASE.  That will make things easier to read and you'll avoid this problem in the future.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: A lot If-Else-If
« Reply #28 on: May 31, 2018, 11:20:38 am »
Well, correct, but the existing code can also be simplified by using ranges in the case statements.
Example:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$J+}
  2. var
  3.   neptunoMP:qword = 1125899906842627;
  4. begin
  5.   // if (neptunoMP >= 1099511627776) and (neptunoMP < 1125899906842624) then
  6.   case neptunoMP of
  7.   1099511627776..1125899906842624:; // range
  8.   // etc
  9.   end;
  10. end.

If you do that everywhere the code becomes much more readable without all the ifs.
This is a feature of case blocks that many programmers forget about.
« Last Edit: May 31, 2018, 11:24:13 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018