> Do I use a for statement or a while statement for this program?

Do I use a for statement or a while statement for this program?

Posted at: 2014-12-18 
Yes, a FOR loop would work well for that. It could be something like this (psuedocode):

//get the starting integer from the user

int starter = getUserInput();

//start the for loop

for (int x = starter; x > 0; x--) {

//need a separate condition for 0 since it needs to print 'none'

if (x == 0) {

print "There is none.";

} else {

print "There is " + x + ".";

}

}

That should get it done.

Language? In Python 2, you could have something like this:

#!/usr/bin/python

number = int(input("Enter a number in the range 2 - 10: "))

i = number

while i>=0:

.... if i>0:

.... .... print "There is",i

.... else:

.... .... print "There is none"

.... i -= 1

#Note: I've used "...." to represent tabs, which Y!Answers removes.



The same code but this time in Java:

import java.util.Scanner;

public class CountingDown {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter a number between 2 and 10: ");

int number = in.nextInt();

int i = number;

while (i>=0) {

if (i>0) {

System.out.println("There is "+i);

} else {

System.out.println("There is none");

} // end if

i--;

} // end while

} // end main()

} // end class CountingDown

either one, both will work.

cin>>number;

for (int i = number, i > 0, i--) cout<<"There is "<
cout<<"There is none";

A for loop is good for when you want your program to loop a given number of times. In this case, the given number is the inputted number.

For example, I need a range of 2 - 10.

I ask a user to enter a number in that range.

User enters 3

The output should like this:



There is 3

There is 2

There is 1

There is none

What do i do?