Jump to content

java help with while loop

- - - - -

  • Please log in to reply
6 replies to this topic

#1
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Rewrite the following code fragment, replacing for loop with equivalent while loop.
public static void main(String[] args)
{
for (String arg : args)
{
System.out.println(arg);
}
}
What I did was as follows:

public static void main(String[] args)
{
while (String arg : args)
{
System.out.println(arg);
}


#2
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
need help sooooooon please

#3
corecase

corecase

    Newbie

  • Members
  • Pip
  • 4 posts
What you're doing makes no sense first of all. "arg" is not a string so you can't use it as a string.

This is how a loop works.


public static void main(String [] args)

{

     for(int count = 0; count < 10; count++)

          System.out.println("this is a for loop example");

}


public static void main(String [] args)

{

     while(int count < 10)

     {

           System.out.println("this is the same with a while loop");

           count++;

     }

}


These two examples do the same thing except one is using a for loop and the other is using a while loop. I hope this helps! If i was helpful please give me kudos!(or w/e its called :c-grin:)

#4
corecase

corecase

    Newbie

  • Members
  • Pip
  • 4 posts
^^ i meant reputation points. sorry for double post

#5
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
I don't understand your question. A while loop has the increment/decrement inside the loop.


for(int i = 0; i < 10; i++)

    System.out.println(i);


while(int i < 10)

{

    System.out.println(i);

    i++;

}


#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Here you go:

int i=0;
while (i<args.length)
    System.out.println(args[i++]);


#7
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Thank you all




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users