Need help with code

Hi i need some help with my code. is an ld c-style line, and i have problem to convert it to swift 4.

func ausgabeBestand () {
for var index = 0; index <- Bestand.count; index++ {
print(Bestand[index])
print("Insgesamt befinden sich (index+1) Kisten im Lager.")
}

thanks for your help

Hi Peter,

Welcome to the Swift forums! Rather than someone writing the code for you, I’d recommend checking out the Swift books section on loops. It’ll be a lot more helpful to have a solid understanding of how loops and ranges work in Swift. https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID121

2 Likes

It's hard to get an idea of what you were trying to build, but maybe this will help you:

func ausgabeBestand() {
  for index in 0 ..< bestand.count {
    print(bestand[index])
    print("Insgesamt befinden sich \(index + 1) Kisten im Lager.")
  }
}

If you wish for more help you should post code written in English (translating using Google Translator would be enough). To better showcase an issue consider using markdown for better readability. Have a good day coding ;)

PS: C-Style for loops were removed from the language two years ago in favor of a modern for in (where) loops.

1 Like

For me the example in C does not make sense because the summary ("Insgesamt befinden sich...) will be printed after every item (german Kisten in this case) of bestand, so I moved this out of the loop.
And assuming that bestand is an array (or any other sequence) the for loop can be simplified:

func ausgabeBestand() {
  for kiste in bestand {
    print(kiste)
  }
  print("Insgesamt befinden sich \(bestand.count) Kisten im Lager.")
}
1 Like

I kept it as is since the example was ill-formed anyway and I did not want to guess it, but in terms of functionality your example is the right way to go.

@DevAndArtist, nah, that would just degrade the communication further. There are others here who do speak (and code in) German.


@beatheaven, @jazzbox hat recht. Ich glaube, er hat genau das geschrieben, was Sie wollten.

Well I wouldn't like to see this forum to turn into a multilingual mess. It's counterproductive if you want to help or contribute to a discussion but do not know the language the people in that particular thread speak. I can speak three languages myself, including German, but I'd appreciate if the community of this forums would stick to the international language. :handshake:

That said, speaking in a different language is not banned here, but I guess it's fair if we kindly advertise that English is still preferred. However I only speak for myself.

3 Likes

in the spirit of competitive member benchmarking

func ausgabeBestand() 
{
    for (i, value):(Int, Bestand) in bestand.enumerated() 
    {
        print(value)
        print("Insgesamt befinden sich \(i + 1) Kisten im Lager.")
    }
}
2 Likes

Thanks a lot that works ;-)

This is about an exam question of a German online programming course, that pops up from time to time in German forums.


Terms of Service

Privacy Policy

Cookie Policy