0

Ive created program that runs while loop and will execute loop until it reaches 150 "rebels eliminated". I utilize break statement to end the while loop from turning into infinite loop. I want to add another block code which is else if statement that once loop reaches 150 "rebels eliminated" it will execute that specific block code! Im having no luck and would like seek guidance. Should I utilize function? I also created array for program allow user pick blaster. After the else if Statement I want array be executed. Whats best way go about this?

import UIKit

var health = 5
var stormTrooperBlasterReload = false
var magazineCaptivity = 0
var rebelsEliminated = 0
var sprinting = 0
var speed = 0
var switchBlaster: String? = ""
var inventory: [String] = ["blasterPistol", "blasterRifle", "blasterShotgun"]


//ends the loop once it reaches 800
while health > 0 {
    if rebelsEliminated == 150 {
        print("The emperor has rised!")
        while sprinting > 50 {
            if sprinting < 50 {
                print("you need to speed up")
            }else if
                sprinting == 50 || sprinting > 50{
                print("you are moving fast")
            }else{
                print("Sprint!")
                break
            }
        }
  }
    if stormTrooperBlasterReload {
        print("you are reloading, Take cover!")
        sleep(7)
        print("reloading almost complete!")
        sleep(2)
        print("aiming your blaster")
        stormTrooperBlasterReload = false
        magazineCaptivity = 0
    }
    if magazineCaptivity > 50 {
        stormTrooperBlasterReload = true
        continue
    }
    print("Firing")
    magazineCaptivity += 1
    rebelsEliminated += 1
2
  • "I want array be executed" Which array are you talking about? You have a guy named 'inventory,' which has not been utilized if that's what you are talking about. Commented Oct 5, 2021 at 1:18
  • The best way forward is to use a debugger and/or print statements to understand what is happening. But it looks to me that you need to improve your loops, both loops has a condition using variables that never changes. Commented Oct 5, 2021 at 6:24

1 Answer 1

2

Check this block of code.

      while sprinting > 50 {
            if sprinting < 50 {
                print("you need to speed up")
            }else if
                sprinting == 50 || sprinting > 50{
                print("you are moving fast")
            }else{
                print("Sprint!")
                break
            }
        }

Here, your while loop condition first check if sprinting is greater than 50. So it will only execute this part.

if sprinting == 50 || sprinting > 50{
   print("you are moving fast")
}

So your loop will never reach it

        }else{
            print("Sprint!")
            break
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you and i'm learning the language still so this was very beneficial for me! I appreciate your answer and ill def work on my logical operator utilization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.