• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

loop question

I am trying to write a cod fragment that reads and prints integer values entered by a user until a particular sntinel value (stored in SENTINEL) is entered. I don't want to print the sentinel value.In java


and
I am trying to write a for loop to print the multiples of 3 from 300 down to 3.In java
0
Ansary100
Asked:
Ansary100
  • 4
  • 3
  • 2
1 Solution
 
Harisha M GCommented:
Hi Ansary100,

Hope this works...
_____________________________    
    boolean x;
    x = False;
    do
    {
        // Read
        if(x == False)
            x = True;
        else
            // Print
       
    }
__________________________
300 to 3...

    for(i = 300; i >= 3; i = i-3)
        System.out.println(i);

Bye
---
Harish
0
 
Harisha M GCommented:
Ansary100,

    Sorry... I left the while part in the first code... this would be better...

    // Read val
    while(val != SENTINEL)
    {
        // Print val
        // Read val
    }

0
 
PreeceCommented:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<script language="JavaScript" type="text/javascript">
<!--
function Loop3()  {
  var lY=0;  
  for (lY=300; lY>=4; lY=lY-3)  {
        alert(lY);
      }
}
//-->
</script>
<body>
<input type=text onMouseOver="Javascript: Loop3(); return false;"></input>
</body>
</html>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PreeceCommented:
Oh, crap, I must be tired!  I was thinking javascript, not java!  Pls disregard!

Preece
0
 
InteractiveMindCommented:
use a do-while loop instead, like so:

do
{
   // Read val
   // Print val
} while ( val != SENTINEL )

or, you may find this code easier to understand:

for (;;)                                   // Infinite loop
{
   // Read val
   if ( val == SENTINEL )
      break;
   // Print val
}
0
 
Harisha M GCommented:
Hi Rob,
  The first code won't work for him...

>> I don't want to print the sentinel value

  He appears to be a beginner and you have put an infinite loop in the second !!
0
 
InteractiveMindCommented:
Hi Harish,

Fair point about the first code I posted, but the second will work, as it breaks the infinite loop if ( val == SENTINEL )

Regards;
0
 
Harisha M GCommented:
I didn't say that it won't work!!!
0
 
InteractiveMindCommented:
Ah! I see what you mean now. lol.. Sorry Harish :o\
I got the impression that you were implying that it won't work, because I'm using an infinite loop.. but I'm with you now. :-)

You're right.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now