Swift - Program to print and repeat message based on counter











up vote
0
down vote

favorite












I have a work related learning course and struck with a hands-on question on Swift programming - Control Transfer Statements like break, continue, fallthrough, return, & throw.



Write a function named printMessage that takes two parameters - a string message and an integer count. The message should print and repeat the message as specified in the count parameter.



Message:"Hello , How are You"



For instance take Count as 8



This should print Message:"Hello , How are You" 8 times consecutively.



So far I got the below code working fine on Xcode, should use control transfer Statements, so tried this. But some extra eyes/other best practice way would help. As I am stuck with this hands-on and it is stopping me to complete the course. The way its designed is it gets input on the text box on the web page and runs the code on the coding area and gets output, if it matches the expected output, it let you submit, but that does not means I am successful as it tests the code with their answer key and my code don't match the answer key and I am failing. Please help



func printMessage(message: String, count: Int){
for i in 0...count{
if( i == count){
break;
} else {
print(message);
continue;
}
}
}
let message: String = readLine()!;
let c = readLine();
let count: Int = Int(c!)!;
printMessage(message: message, count: count);









share|improve this question
























  • Why do you need to check for i == count inside the loop, the loop will already be doing this. Since you're starting at 0, you really should be looping to count - 1 ... or start the loop at 1. Either for i in 0..<count{ or for i in 1...count{. And Swift doesn't need ; at the end of the lines ;)
    – MadProgrammer
    Nov 11 at 22:30












  • @MadProgrammer I use the if case, so that I can use the conditional statement in the code, I dont find other ways to include conditional statements in the funtion :(
    – vikram mohan
    Nov 11 at 22:32










  • Well, technically, the for-loop is a conditional statement, but I might be misunderstanding your requirement
    – MadProgrammer
    Nov 11 at 22:33










  • Sorry, Its should be control transfer statements like break-statement­, continue-statement­, fallthrough-statement­, return-statement­ & throw-statement­. I will edit the question @MadProgrammer
    – vikram mohan
    Nov 11 at 22:35












  • Why did you delete your previous question and post it again? All the help you received on that other one is now lost. Don't do that. Edit as needed is much better.
    – rmaddy
    Nov 11 at 22:41















up vote
0
down vote

favorite












I have a work related learning course and struck with a hands-on question on Swift programming - Control Transfer Statements like break, continue, fallthrough, return, & throw.



Write a function named printMessage that takes two parameters - a string message and an integer count. The message should print and repeat the message as specified in the count parameter.



Message:"Hello , How are You"



For instance take Count as 8



This should print Message:"Hello , How are You" 8 times consecutively.



So far I got the below code working fine on Xcode, should use control transfer Statements, so tried this. But some extra eyes/other best practice way would help. As I am stuck with this hands-on and it is stopping me to complete the course. The way its designed is it gets input on the text box on the web page and runs the code on the coding area and gets output, if it matches the expected output, it let you submit, but that does not means I am successful as it tests the code with their answer key and my code don't match the answer key and I am failing. Please help



func printMessage(message: String, count: Int){
for i in 0...count{
if( i == count){
break;
} else {
print(message);
continue;
}
}
}
let message: String = readLine()!;
let c = readLine();
let count: Int = Int(c!)!;
printMessage(message: message, count: count);









share|improve this question
























  • Why do you need to check for i == count inside the loop, the loop will already be doing this. Since you're starting at 0, you really should be looping to count - 1 ... or start the loop at 1. Either for i in 0..<count{ or for i in 1...count{. And Swift doesn't need ; at the end of the lines ;)
    – MadProgrammer
    Nov 11 at 22:30












  • @MadProgrammer I use the if case, so that I can use the conditional statement in the code, I dont find other ways to include conditional statements in the funtion :(
    – vikram mohan
    Nov 11 at 22:32










  • Well, technically, the for-loop is a conditional statement, but I might be misunderstanding your requirement
    – MadProgrammer
    Nov 11 at 22:33










  • Sorry, Its should be control transfer statements like break-statement­, continue-statement­, fallthrough-statement­, return-statement­ & throw-statement­. I will edit the question @MadProgrammer
    – vikram mohan
    Nov 11 at 22:35












  • Why did you delete your previous question and post it again? All the help you received on that other one is now lost. Don't do that. Edit as needed is much better.
    – rmaddy
    Nov 11 at 22:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a work related learning course and struck with a hands-on question on Swift programming - Control Transfer Statements like break, continue, fallthrough, return, & throw.



Write a function named printMessage that takes two parameters - a string message and an integer count. The message should print and repeat the message as specified in the count parameter.



Message:"Hello , How are You"



For instance take Count as 8



This should print Message:"Hello , How are You" 8 times consecutively.



So far I got the below code working fine on Xcode, should use control transfer Statements, so tried this. But some extra eyes/other best practice way would help. As I am stuck with this hands-on and it is stopping me to complete the course. The way its designed is it gets input on the text box on the web page and runs the code on the coding area and gets output, if it matches the expected output, it let you submit, but that does not means I am successful as it tests the code with their answer key and my code don't match the answer key and I am failing. Please help



func printMessage(message: String, count: Int){
for i in 0...count{
if( i == count){
break;
} else {
print(message);
continue;
}
}
}
let message: String = readLine()!;
let c = readLine();
let count: Int = Int(c!)!;
printMessage(message: message, count: count);









share|improve this question















I have a work related learning course and struck with a hands-on question on Swift programming - Control Transfer Statements like break, continue, fallthrough, return, & throw.



Write a function named printMessage that takes two parameters - a string message and an integer count. The message should print and repeat the message as specified in the count parameter.



Message:"Hello , How are You"



For instance take Count as 8



This should print Message:"Hello , How are You" 8 times consecutively.



So far I got the below code working fine on Xcode, should use control transfer Statements, so tried this. But some extra eyes/other best practice way would help. As I am stuck with this hands-on and it is stopping me to complete the course. The way its designed is it gets input on the text box on the web page and runs the code on the coding area and gets output, if it matches the expected output, it let you submit, but that does not means I am successful as it tests the code with their answer key and my code don't match the answer key and I am failing. Please help



func printMessage(message: String, count: Int){
for i in 0...count{
if( i == count){
break;
} else {
print(message);
continue;
}
}
}
let message: String = readLine()!;
let c = readLine();
let count: Int = Int(c!)!;
printMessage(message: message, count: count);






swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:35









Carsten Hagemann

19313




19313










asked Nov 11 at 22:14









vikram mohan

195




195












  • Why do you need to check for i == count inside the loop, the loop will already be doing this. Since you're starting at 0, you really should be looping to count - 1 ... or start the loop at 1. Either for i in 0..<count{ or for i in 1...count{. And Swift doesn't need ; at the end of the lines ;)
    – MadProgrammer
    Nov 11 at 22:30












  • @MadProgrammer I use the if case, so that I can use the conditional statement in the code, I dont find other ways to include conditional statements in the funtion :(
    – vikram mohan
    Nov 11 at 22:32










  • Well, technically, the for-loop is a conditional statement, but I might be misunderstanding your requirement
    – MadProgrammer
    Nov 11 at 22:33










  • Sorry, Its should be control transfer statements like break-statement­, continue-statement­, fallthrough-statement­, return-statement­ & throw-statement­. I will edit the question @MadProgrammer
    – vikram mohan
    Nov 11 at 22:35












  • Why did you delete your previous question and post it again? All the help you received on that other one is now lost. Don't do that. Edit as needed is much better.
    – rmaddy
    Nov 11 at 22:41


















  • Why do you need to check for i == count inside the loop, the loop will already be doing this. Since you're starting at 0, you really should be looping to count - 1 ... or start the loop at 1. Either for i in 0..<count{ or for i in 1...count{. And Swift doesn't need ; at the end of the lines ;)
    – MadProgrammer
    Nov 11 at 22:30












  • @MadProgrammer I use the if case, so that I can use the conditional statement in the code, I dont find other ways to include conditional statements in the funtion :(
    – vikram mohan
    Nov 11 at 22:32










  • Well, technically, the for-loop is a conditional statement, but I might be misunderstanding your requirement
    – MadProgrammer
    Nov 11 at 22:33










  • Sorry, Its should be control transfer statements like break-statement­, continue-statement­, fallthrough-statement­, return-statement­ & throw-statement­. I will edit the question @MadProgrammer
    – vikram mohan
    Nov 11 at 22:35












  • Why did you delete your previous question and post it again? All the help you received on that other one is now lost. Don't do that. Edit as needed is much better.
    – rmaddy
    Nov 11 at 22:41
















Why do you need to check for i == count inside the loop, the loop will already be doing this. Since you're starting at 0, you really should be looping to count - 1 ... or start the loop at 1. Either for i in 0..<count{ or for i in 1...count{. And Swift doesn't need ; at the end of the lines ;)
– MadProgrammer
Nov 11 at 22:30






Why do you need to check for i == count inside the loop, the loop will already be doing this. Since you're starting at 0, you really should be looping to count - 1 ... or start the loop at 1. Either for i in 0..<count{ or for i in 1...count{. And Swift doesn't need ; at the end of the lines ;)
– MadProgrammer
Nov 11 at 22:30














@MadProgrammer I use the if case, so that I can use the conditional statement in the code, I dont find other ways to include conditional statements in the funtion :(
– vikram mohan
Nov 11 at 22:32




@MadProgrammer I use the if case, so that I can use the conditional statement in the code, I dont find other ways to include conditional statements in the funtion :(
– vikram mohan
Nov 11 at 22:32












Well, technically, the for-loop is a conditional statement, but I might be misunderstanding your requirement
– MadProgrammer
Nov 11 at 22:33




Well, technically, the for-loop is a conditional statement, but I might be misunderstanding your requirement
– MadProgrammer
Nov 11 at 22:33












Sorry, Its should be control transfer statements like break-statement­, continue-statement­, fallthrough-statement­, return-statement­ & throw-statement­. I will edit the question @MadProgrammer
– vikram mohan
Nov 11 at 22:35






Sorry, Its should be control transfer statements like break-statement­, continue-statement­, fallthrough-statement­, return-statement­ & throw-statement­. I will edit the question @MadProgrammer
– vikram mohan
Nov 11 at 22:35














Why did you delete your previous question and post it again? All the help you received on that other one is now lost. Don't do that. Edit as needed is much better.
– rmaddy
Nov 11 at 22:41




Why did you delete your previous question and post it again? All the help you received on that other one is now lost. Don't do that. Edit as needed is much better.
– rmaddy
Nov 11 at 22:41












2 Answers
2






active

oldest

votes

















up vote
0
down vote













The following piece of code works as it is meant to, in a while loop and also incorporates the Control Transfer Statements, like you asked:



func printMessage(message: String, count: Int) {
var i: Int = 0
while true {
if i < count {
print(message)
i += 1
} else { break }
}
}


let message: String = "Hello!"
let count: Int = 3
printMessage(message: message, count: count)


If there are any more specifications, let me know. Also, it would be helpful to have a link to the lesson/tutorial. Hope this helps! :)






share|improve this answer




























    up vote
    -1
    down vote













    If you want to do it with for loop:



    func PrintMessage (message: String, count: Int) {
    for _ in 0..<count {
    print(message)
    }
    }

    let message = readLine()
    let count = Int(readLine()!)

    PrintMessage(message: message!, count: count!);





    share|improve this answer





















    • no I need to somehow incorporate the control transfer statements into the function printMessage
      – vikram mohan
      Nov 11 at 23:33











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253774%2fswift-program-to-print-and-repeat-message-based-on-counter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The following piece of code works as it is meant to, in a while loop and also incorporates the Control Transfer Statements, like you asked:



    func printMessage(message: String, count: Int) {
    var i: Int = 0
    while true {
    if i < count {
    print(message)
    i += 1
    } else { break }
    }
    }


    let message: String = "Hello!"
    let count: Int = 3
    printMessage(message: message, count: count)


    If there are any more specifications, let me know. Also, it would be helpful to have a link to the lesson/tutorial. Hope this helps! :)






    share|improve this answer

























      up vote
      0
      down vote













      The following piece of code works as it is meant to, in a while loop and also incorporates the Control Transfer Statements, like you asked:



      func printMessage(message: String, count: Int) {
      var i: Int = 0
      while true {
      if i < count {
      print(message)
      i += 1
      } else { break }
      }
      }


      let message: String = "Hello!"
      let count: Int = 3
      printMessage(message: message, count: count)


      If there are any more specifications, let me know. Also, it would be helpful to have a link to the lesson/tutorial. Hope this helps! :)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The following piece of code works as it is meant to, in a while loop and also incorporates the Control Transfer Statements, like you asked:



        func printMessage(message: String, count: Int) {
        var i: Int = 0
        while true {
        if i < count {
        print(message)
        i += 1
        } else { break }
        }
        }


        let message: String = "Hello!"
        let count: Int = 3
        printMessage(message: message, count: count)


        If there are any more specifications, let me know. Also, it would be helpful to have a link to the lesson/tutorial. Hope this helps! :)






        share|improve this answer












        The following piece of code works as it is meant to, in a while loop and also incorporates the Control Transfer Statements, like you asked:



        func printMessage(message: String, count: Int) {
        var i: Int = 0
        while true {
        if i < count {
        print(message)
        i += 1
        } else { break }
        }
        }


        let message: String = "Hello!"
        let count: Int = 3
        printMessage(message: message, count: count)


        If there are any more specifications, let me know. Also, it would be helpful to have a link to the lesson/tutorial. Hope this helps! :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 17:18









        George_E

        9601525




        9601525
























            up vote
            -1
            down vote













            If you want to do it with for loop:



            func PrintMessage (message: String, count: Int) {
            for _ in 0..<count {
            print(message)
            }
            }

            let message = readLine()
            let count = Int(readLine()!)

            PrintMessage(message: message!, count: count!);





            share|improve this answer





















            • no I need to somehow incorporate the control transfer statements into the function printMessage
              – vikram mohan
              Nov 11 at 23:33















            up vote
            -1
            down vote













            If you want to do it with for loop:



            func PrintMessage (message: String, count: Int) {
            for _ in 0..<count {
            print(message)
            }
            }

            let message = readLine()
            let count = Int(readLine()!)

            PrintMessage(message: message!, count: count!);





            share|improve this answer





















            • no I need to somehow incorporate the control transfer statements into the function printMessage
              – vikram mohan
              Nov 11 at 23:33













            up vote
            -1
            down vote










            up vote
            -1
            down vote









            If you want to do it with for loop:



            func PrintMessage (message: String, count: Int) {
            for _ in 0..<count {
            print(message)
            }
            }

            let message = readLine()
            let count = Int(readLine()!)

            PrintMessage(message: message!, count: count!);





            share|improve this answer












            If you want to do it with for loop:



            func PrintMessage (message: String, count: Int) {
            for _ in 0..<count {
            print(message)
            }
            }

            let message = readLine()
            let count = Int(readLine()!)

            PrintMessage(message: message!, count: count!);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 23:32









            emreoktem

            1,8281123




            1,8281123












            • no I need to somehow incorporate the control transfer statements into the function printMessage
              – vikram mohan
              Nov 11 at 23:33


















            • no I need to somehow incorporate the control transfer statements into the function printMessage
              – vikram mohan
              Nov 11 at 23:33
















            no I need to somehow incorporate the control transfer statements into the function printMessage
            – vikram mohan
            Nov 11 at 23:33




            no I need to somehow incorporate the control transfer statements into the function printMessage
            – vikram mohan
            Nov 11 at 23:33


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253774%2fswift-program-to-print-and-repeat-message-based-on-counter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?