Autor Beitrag
IlLuMiNaTi
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Do 03.04.08 19:58 
hiho,

in meinem Programm, was ich gerade schreibe, ist es nötig, dass eine For-Schleife nur jedes dritte i benutzt.
Aber da ich keine Ahnung habe, wie ich das schreiben muss, hoffe ich, dass mir hier wer helfen kann.

Ich hab bisher nur:

For i := 2 To (textlaenge-7) Do
...

und wie schreibe ich, dass er nur 2,5,8,11,14 usw benutzt?

Vielen Dank im Vorraus

MfG
Illu
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Do 03.04.08 20:02 
Hi! und :welcome: im Forum! :)
ausblenden Delphi-Quelltext
1:
if ((i + 1) mod 3 = 0) then...					

Mod gibt dir den Rest einer Divison zurück. ;)

Bitte benutze künftig für Deinen Code die Delphi-Tags. [delphi] code [/delphi]

Cheers,
Marc.
IlLuMiNaTi Threadstarter
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Do 03.04.08 20:29 
Danke =)

Ich werde das nächste mal dann den Delphi-Tag benutzen :)

Danke für die schnelle Hilfe. Und gleich stoße ich auf das nächste Problem, aber das werde ich selber lösen^^
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Do 03.04.08 20:35 
Sicherlich performanter wäre hier eine while-Schleife mit einer Zählvariablen, da bei der for-Schleife eigentlich für jede Zahl die If-Abfrage ausgeführt wird. Bei kleinen Zahlen mag der Unterschied vielleicht nicht auffallen, trotzdem sollte man sich sowas nicht angewöhnen. ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
i := 2;
while i <= textlaenge - 7 do
begin
  // ...
  Inc(i, 3);
end;
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Do 03.04.08 21:27 
Hi,

:shock: Was sind denn das für lösungen???

ausblenden Delphi-Quelltext
1:
2:
3:
for i := 0 to (max div 3) do
  doSomeThing(3 * i);
end;


meines Ermessens nach günstiger...

und wenn schon das erste, dann bitte so:
ausblenden Delphi-Quelltext
1:
if i mod 3 = 1 then...					


mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Do 03.04.08 21:34 
user profile iconHidden hat folgendes geschrieben:
und wenn schon das erste, dann bitte so:
ausblenden Delphi-Quelltext
1:
if i mod 3 = 1 then...					

:gruebel:
das i mod 3 = 0 stimmt doch :?:

Gruß
alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Do 03.04.08 21:36 
Hi,

user profile iconMarc. hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
if ((i + 1) mod 3 = 0) then...					



:arrow: i mod 3 = 1

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Do 03.04.08 21:45 
user profile iconHidden hat folgendes geschrieben:
Hi,

user profile iconMarc. hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
if ((i + 1) mod 3 = 0) then...					



:arrow: i mod 3 = 1

€: Jup, hattest recht. Die Lösung sollte für einen Anfänger auch leicht verständlich sein. ;)
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Do 03.04.08 22:00 
mir ging es eher um die optimierung auf wenig Rechenschritte :wink:

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
IlLuMiNaTi Threadstarter
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Do 03.04.08 22:06 
aber ob man nur bei 2 anfängt und dafür i+1 nimmt, oder bei 1 aber dafür der rest 1 sein muss, ist ja im Grunde egal, oder?
Oder ist das eine schneller? Klar, dass die heutigen PC das sowieso in Pikosekunden machen, aber nur rein theoretisch?

Ist ja auch egal^^ Ihr habt mir geholfen und das ist mir z.Z. am wichtigsten^^

Edit:
Tja, da war die Antwort schneller als die Frage.. echt erstaunlich xD