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);
swift
|
show 8 more comments
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);
swift
Why do you need to check fori == count
inside the loop, the loop will already be doing this. Since you're starting at0
, you really should be looping tocount - 1
... or start the loop at1
. Eitherfor i in 0..<count{
orfor 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, thefor-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
|
show 8 more comments
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);
swift
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
swift
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 fori == count
inside the loop, the loop will already be doing this. Since you're starting at0
, you really should be looping tocount - 1
... or start the loop at1
. Eitherfor i in 0..<count{
orfor 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, thefor-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
|
show 8 more comments
Why do you need to check fori == count
inside the loop, the loop will already be doing this. Since you're starting at0
, you really should be looping tocount - 1
... or start the loop at1
. Eitherfor i in 0..<count{
orfor 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, thefor-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
|
show 8 more comments
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! :)
add a comment |
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!);
no I need to somehow incorporate the control transfer statements into the function printMessage
– vikram mohan
Nov 11 at 23:33
add a comment |
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! :)
add a comment |
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! :)
add a comment |
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! :)
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! :)
answered Nov 12 at 17:18
George_E
9601525
9601525
add a comment |
add a comment |
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!);
no I need to somehow incorporate the control transfer statements into the function printMessage
– vikram mohan
Nov 11 at 23:33
add a comment |
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!);
no I need to somehow incorporate the control transfer statements into the function printMessage
– vikram mohan
Nov 11 at 23:33
add a comment |
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!);
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!);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Why do you need to check for
i == count
inside the loop, the loop will already be doing this. Since you're starting at0
, you really should be looping tocount - 1
... or start the loop at1
. Eitherfor i in 0..<count{
orfor 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