On Jun 22, 10:50 am, "function(phil)" <[email protected]> wrote: > Thanks for your reply. > > Okay. Let's discuss language. In the paragraph below you us the term > initialization. What does that mean? > Does it mean the same as I've heard instantiate a variable and declare > a variable? Then you use the term "index variable" what does that > mean? I know what a variable is > but what do you mean by "index"? > > Regarding the while statement. The following code doesn't work as > planned but it shows my understanding of the > use of the "while loop". It's a real world example. I think the > courses should use real world examples. The use of displaying names > 20 times doesn't make much since to me. > > <pre><code> > import javax.swing.JOptionPane; > /* > * To change this template, choose Tools | Templates > * and open the template in the editor. > */ > > /** > * > * @author Phillip > */ > public class MyTestProject { > > /** > * @param args the command line arguments > */ > public static void main(String[] args) { > > int temp = 0; > > String findTemp=JOptionPane.showInputDialog("Enter the temp!"); > > while(temp > 0) { > if (temp <102 && temp > 78) { > > JOptionPane.showMessageDialog(null, "The tempurature is " + temp + > ". Turn the air conditioning on!"); > > } > else if (temp > 102) { > JOptionPane.showMessageDialog(null, "The tempurature is " + > temp + > ". Let's go North!"); > } > else { > JOptionPane.showMessageDialog(null, "The tempurature is " + > temp + ". We don't need air conditioning! We need something else!"); > } > > } > > } > </code></pre> > Regarding the "for loop": > > What I understand the word "for" would be a situation like this. > For? What does that mean in a real world situation? > If someone said to me, "for" each "item" you will pay "this amount". I > would understand that. > > I don't understand the program language that says "for" each "1 item" > add another 1 to it until you get to 20. What application does that > have? > Can you give me a real world application for the use of for? That should ideally be the case. However, not easy to come up with. I guess, author did not do this for fear of introducing more advance concepts and language constructs. For example: // An array of item. User defined object and array are introduced here. Item[] items={new Item("Apple",0.20), new Item("Banana",0.30), new Item ("Coconut",0.40)}; // For each item, print it's name and price. for(int i=0; i < items.length; i++){ Item item=items[i]; System.out.println(item.name+"="+item.price); }
Above example is nearer to real world, but at the same time, more complex. That could be a reason author use brief example. > Thanks for your time > > This is education not copying and pasting code to make beleive > knowledge. I agree and can understand your frustration. > > On Jun 20, 3:36 pm, Robert Childers <[email protected]> > wrote: > > > > > First off a little pseudo code. > > for (int i=1;i<limit;i++) { > > statment block > > > } > > > int i=1; > > while (i<limit) { > > statment block > > i++;} > > > In the for loop, the initialization of the variable is handled in the > > structure of the loop. But if you looked at that actual code that is > > produced, you would see the the for loop consists of a initialization > > of the index variable, followed by a comparison and branch statement > > past the end of the block, finally you would have the block, an > > increment of your variable and a jump back to the comparison test at > > the start of your loop. > > > All of this is being done explicitly with the while loop as statements > > before you enter the loop, and the while loop construct. Since the > > variable has to be initialized outside of the looped statements it > > occurs before the while () statement. in the for() statement the > > initialization is done prior to the looping of the repeated statements > > by the for loop construct. > > > Since a while loop's construct is only for a test to stop the looping, > > you have to put your increment inside the body of the loop, usually as > > the last statement of the looped statements. > > > For loops are much clearer to read when dealing with a loop that has a > > index that is incremented and tested. > > > While loops on the other hand are useful for non indexed as well as > > indexed types of loops > > > Example > > EOF=false; > > While (~EOF) { > > read one line and process} > > > Here EOF is some variable that is set to false and is changed to true > > when the end of the file is reached. and my statement block is to read > > the file one line at a time. There is no way to know the length of the > > file you are reading before hand like you can know the length of an > > array, so you can't use a for loop. Thus a while loop is more general, > > but you have to handle your initialization and setting of the variable > > used in the loop test. > > > On Jun 19, 10:12 pm, "function(phil)" <[email protected]> wrote: > > > > Howdy again. Okay. I've looked over the process of changing a "for > > > loop" to a "while loop". I've seen examples that break the variable i > > > and the statement i++ out of parameters of the initial for block of > > > code. I can write the code but understanding why those two areas are > > > removed outside the block is not explaned. Can someone discuss with me > > > why the for and while loops are different in this way. What is the > > > logic and which is better to use in what situations? I refuse to go > > > through this course copying code to complete the home work > > > assignments, without understanding what is going on! > > > Thanks > > > > Phil- Hide quoted text - > > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
