First steps in programming: loops

If you haven’t read my other articles on getting started with coding you should do so before proceeding: I will assume that you can open a JavaScript console and run a basic program , know how to use variables andconditionals.

In the last post we learnt how to make the computer execute commands conditionally , printing a different message if our user entered a blank name, for example.

function Program() {
    var name = prompt("What is your name?")
    if (name == "") {
        console.log("Hello stranger")
    } else {
        console.log("Hello")
        console.log(name)
    }
}

This program says “Hello stranger” if no name is provided and moves on, but what if we really, really need the user to enter a name before proceeding? Is there a way of “sending” the user back to the prompt so they can repeat the input? This can we done with loops, and in particular with a statement called while .

function Program() {
    var name = "";
    input: while(1) {
        name = prompt("What is your name?")
        if (name != "") break input
    }
    console.log("Hello")
    console.log(name)
}

This works by creating a loop using while . This loop is called input . When the input is empty, we continue the loop. Otherwise we break the loop. The while is followed by a 1 to tell the computer to loop indefinitely, until we tell it to stop with break .

Since we rarely want loop to go on indefinitely, the 1 following the while can be substituted with a condition . This explains the name: while a condition holds, the loop will continue. We can take advantage of that and simplify the code.

function Program() {
    var name = ""
    while(name == "") {
        name = prompt("What is your name?")
    }
    console.log("Hello")
    console.log(name)
}

We can now apply this technique to the Apollo 11 program, changing this version

function Program() {
    console.log("ignition sequence start")
    var countdown = 6
    console.log(countdown)
    countdown = countdown - 1
    console.log(countdown)
    countdown = countdown - 1
    console.log(countdown)
    countdown = countdown - 1
    console.log(countdown)
    countdown = countdown - 1
    console.log(countdown)
    countdown = countdown - 1
    console.log(countdown)
    countdown = countdown - 1
    console.log(countdown)
    console.log("All engines running")
}

into this, much shorter, one

function Program() {
    console.log("ignition sequence start")
    var countdown = 6
    while (countdown >= 0) {
        console.log(countdown)
        countdown = countdown - 1
    }
    console.log("All engines running")
}

Great!

This concludes this mini series on getting started in programming. Let’s recap what we learnt:

  • programs are sequences of instructions or commands
  • variables are boxes that contain values so the computer can remember it
  • indirection allows us to refer to a variable instead of its value
  • conditionals are commands that allow the computer to chose different sequences of instructions based on a condition
  • loops are sequences of instructions which are repeated until a condition is met, or until we explicitly break out

These very few concepts are the basis of all programming. Once you master them you can literally write any program possible—it’s mathematically proven !

稿源:Sklivvz (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » First steps in programming: loops

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录

登录

忘记密码 ?

切换登录

注册