Autor Beitrag
Pepp3r
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82



BeitragVerfasst: So 12.09.10 15:40 
Hallo liebe Delphicommunity,
Ich habe mal wieder ein merkwürdiges Problem. In meinem Programm wird ein FOR-Schleife öffter durchlaufen als sie sollte.
Die Schleife wird durchlaufen bei
ausblenden Delphi-Quelltext
1:
 for i := 1 to 1 do					

was ja eigentlich nicht sein dürfte.

Der Quellcode:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
  PeinFilm = ^TeinFilm;

  TeinFilm = record
    cover,
    titel,
    jahr,
    regie,
    genre,
    alternativ,
    kommentar,
    bewertung,
    lokal,
    id: String;
    next,
    prev: PeinFilm;
  end;

  TFilm = class(TObject)
  private
    first, dummy: PeinFilm;
    function readFilm(nummer: Word): PeinFilm;
  public
    property getFilmData [ nummer: Word ]: PeinFilm read readFilm;
  end;

function TFilm.readFilm(nummer: Word): PeinFilm;
  var
  i: Word;
begin
  dummy := first;
  for i := 1 to nummer do //<-- wenn ich hier "1" übergebe, wird die Schleife einmal durchlaufen 
  begin
    if not (dummy.next = nilthen
      dummy := dummy.next;
    else
    begin
      result := dummy;
      exit;
    end;
  end;
  result := dummy;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    showmessage(Filmliste.getFilmData[1].titel);

end;


Vllt bin ich zu doof ich weiß es nicht.
Danke für jede Hilfe.
Gruß
Jonas
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 12.09.10 15:48 
Was auch korrekt ist. Die Schleife wird einmal mit dem Index i = 1 durchlaufen. Analog dazu wird die Schleife for i := 1 to 2 zwei Mal durchlaufen. Entsprechend mit den Indizes 1 und 2. :)

Grüße
Marc
Pepp3r Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82



BeitragVerfasst: So 12.09.10 15:53 
Meiner Erfahrung nach wird die Schleife so oft durchlaufen bis for und to den selben Wert haben. Demnach müsste die schleife garnicht durchlaufen werden?!?
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: So 12.09.10 15:59 
Dann müsstest du eine While-Schleife nehmen

_________________
Markus Kinzler.
Pepp3r Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82



BeitragVerfasst: So 12.09.10 16:00 
Also stimmts, dass for i:=1 to 1 genau einmal durchlaufen wird?
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: So 12.09.10 16:01 
Ja.

_________________
Markus Kinzler.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: So 12.09.10 16:02 
Anscheinend richten sich die Delphi-Entwickler nicht nach deiner Erfahrung. Meiner Erfahrung nach ist die Abbruchbedingung immer Grenze < Laufvariable und nicht <=.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Tranx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 648
Erhaltene Danke: 85

WIN 2000, WIN XP
D5 Prof
BeitragVerfasst: So 12.09.10 16:03 
Leider funktioniert das nicht so, wie Du willst.

For i := a to x do ...


läuft i solange durch bis i = x ist. also 1x, falls x und a = 1 sind. Wilst Du die Schleife nie durchlaufen, muss x kleiner als a sein :

Dann muss Du schreiben:

For i := 1 to nummer -1 do ...
Pepp3r Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82



BeitragVerfasst: So 12.09.10 16:06 
Okay ich danke euch. :)
Edit:// richtige schleifenwahl war hier die lösung, mit for i:=1 to nummer-1 gabs ebenfalls probleme. mit der while schleife klappts nun.

Habe nun noch zwei weitere Fragen für die ich einen Thread spare:

1.: Gibt es für Funktionen einen Resultbefehl der einen exit beinhaltet?
2.: Gibt es in Delphi ein this-keywort wie bei java?
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 12.09.10 16:13 
Auch ein Blick in den Debugger-Code hätte geholfen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
00464808 B801000000       mov eax,$00000001 // Unit1.pas.30: for i := 0 to 0 do
004648090               nop // Unit1.pas.32: nop (Befehle innerhalb der Schleife)
004648048               dec eax // Unit1.pas.30: for i := 0 to 0 do
004648075FC             jnz $0046480d // NOP
00464811 C3               ret // Unit1.pas.34: end;

Gruß
Marc
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: So 12.09.10 16:27 
Zitat:
1.: Gibt es für Funktionen einen Resultbefehl der einen exit beinhaltet?
Nein.
Zitat:
2.: Gibt es in Delphi ein this-keywort wie bei java?
Ja: self

_________________
Markus Kinzler.
Pepp3r Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82



BeitragVerfasst: So 12.09.10 16:44 
user profile iconmkinzler hat folgendes geschrieben Zum zitierten Posting springen:
Zitat:
1.: Gibt es für Funktionen einen Resultbefehl der einen exit beinhaltet?
Nein.
Zitat:
2.: Gibt es in Delphi ein this-keywort wie bei java?
Ja: self


self macht soweit ich weiß nicht die variablen im prozedurkopf sichtbar?!
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: So 12.09.10 16:48 
Darf ich dich auf die Regel "Neue Frage, neues Topic" hinweisen. Ist für die Übersicht besser und auch falls jemand die gleiche Frage mal haben sollte, erleichtert diese Regel die Suche.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: So 12.09.10 17:11 
Hallo,

Auch nochmal von offizieller Stelle:
bitte erstelle für jede deiner Fragen ein separates Topic, da die Diskussion mehrerer Fragen in einem Topic für gewöhnlich für Verwirrung und Durcheinander sorgt. Daher:

*geschlossen*


Die bereits gegeben Antwort lasse ich mal drin, sollten weitere Fragen sein, erstelle bitte getrennte Themen dazu.

Viele Grüße,
Martok

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Dieses Thema ist gesperrt, Du kannst keine Beiträge editieren oder beantworten.

Das Thema wurde von einem Team-Mitglied geschlossen. Wenn du mit der Schließung des Themas nicht einverstanden bist, kontaktiere bitte das Team.