Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

Creating a conditional loop with outputs.

Home/ Questions/Q 332
Next
Answered
Creating a conditional loop with outputs.
hs.geektavern
hs.geektavern Begginer

I’m trying to create a conditional loop with outputs in my code, but I seem to be missing something. Here is a simplified version of what I currently have:

function printNumbers(){
let numArr = [1, 2, 3, 4, 5];
for(let i=0; i
This code works as expected and outputs the numbers 1-5 along with whether they are odd or even. However, what I want to do is add a condition where, if the number is even, it also outputs the next number in the array. So for example, the output would be "2 is even, the next number is 3" for the number 2.
I've tried adding another if statement inside the existing one like this:

function printNumbers(){
let numArr = [1, 2, 3, 4, 5];
for(let i=0; i
But now the output shows the next number for every even number, even if it's the last number in the array. How can I modify my code to only show the next number for even numbers that aren't the last? Any help would be greatly appreciated!

codeconditional loopoutputprogrammingsyntax
  • 71
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lucy_lavigne84 Teacher
    2017-01-20T11:41:51+00:00Added an answer about 6 years ago

    Hey there, coding can be tricky at times, but I’m glad to be of assistance to you. I’ve had experiences in several coding languages and I think I can help you out with your current problem.
    From your question, it seems like you’re trying to create a conditional loop in Python that outputs the sum of the numbers until the number 13 is reached, excluding the number 13 from the output. Here’s how I would approach this issue.
    First, I would create a function to perform the required computation. In this function, I would define a variable to hold the sum of the numbers, and then create a loop that iterates through the numbers from 1 to 13. Inside the loop, I would add each number to the variable. However, if the loop reaches the number 13, the iteration would skip that number and continue. Once the loop completes, the function would return the sum of the numbers.
    Here’s the code for the function:
    “`
    def sum_of_numbers():
    sum = 0
    for i in range(1,14):
    if i == 13:
    continue
    sum += i
    return sum
    “`
    Now that we have our function, we can call it and output the result. Here’s an example:
    “`
    print(sum_of_numbers())
    “`
    This should output the sum of the numbers from 1 to 12, which is 78.
    I hope that helps you out. If you have any further questions, feel free to ask. Happy coding!

    • 98
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. jmhastey Teacher
    2017-01-31T06:10:44+00:00Added an answer about 6 years ago

    To solve your problem with creating a conditional loop in your code, you can use the “while” loop. The “while” loop will continue to execute the code block as long as the specified condition is true. For example, if you want to loop through a list and print each item until you reach a certain condition, you can use a “while” loop as follows:

    “`
    mylist = [“item1”, “item2”, “item3”, “item4”, “item5”]
    i = 0
    while i < len(mylist) and mylist[i] != "item3": print(mylist[i]) i += 1 ``` In this code snippet, the "while" loop starts at the first item in the list ("item1") and continues to loop through the list as long as the index "i" is less than the length of the list and the current item is not "item3". Once "item3" is reached in the list, the loop stops and the remaining items are not printed. I hope this helps you solve your problem with creating a conditional loop in your code!

    • 92
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.