/* This program serves to demonstrate while and do-while loops. * Loops are repetitive structures that are a cornerstone of programming */ import java.util.Scanner; class LoopExercise { public static void main(String [] args) { Scanner input = new Scanner(System.in); int num,times,limit; //Here, num holds a number, times serves as a counter for the number of times something has been done. //Limit is the upper limit for the counter. //This part prints the multiplication tables for a number entered by the user, // for 'limit' number of lines. System.out.print("Enter the number for the tables: "); num = input.nextInt(); System.out.print("Enter the upper limit: "); limit = input.nextInt(); times = 1; while(times<=limit) { System.out.println(num +" * "+times+" = "+num*times); times++; // This line is called the increment line. This is very important. If this is missing, we get an infinte loop. } /* The above structure is a while loop. The program checks if the conditon is true, the statements inside the loop, * called the "body of the loop", are executed. Then the program checks the condition again. This process is repeated * until the condition becomes false. The program then moves on to the first line after the loop. */ //We can use loops to add numbers from 1 till the limit. //We use the same limit that we used for the previous loop. int count = 1; //This us another loop counter. We can also reuse 'times' and 'num' int sum = 0; //This is the accumulator variable. This keeps a running total of the numbers we've encountered so far. while(count<=limit) { sum = sum + count; //Add the current number to count count++; } System.out.println("The sum of all natural numbers up to "+limit+" is "+sum); //We can use if statements inside loops. // In this part we calculate the sum of all the odd natural numbers up to 'limit' // Here, we start of at 1, and increment the counter by 2 at each iteration of the loop. count =1; sum =0; //Reset the values for count and sum while(count<=limit) { sum = sum+count; count = count + 2; //Since we start off with an odd number and keep adding 2, count will always be odd. } System.out.println("The sum of all odd natural numbers up to "+limit+" is "+sum); /* Loops can be used in Stings as well. Here, we read in one letter at a time and concatenate the letter we just * read in to the string we have so far to make one long string. We use strings to read in the letters because * using chars would require some conversions. */ String text =""; //We declare an empty string String letter=""; count =0; // We are building a string of 'limit' number of letters while(count < limit) { System.out.print("Enter a letter: "); letter = input.next(); text=text+letter; count++; } System.out.println("The string we just constructed is "+text); /* do-while loops are very similar to while loops, except where while loops run a minimum of 0 times, do-while loops run * at least once. */ // Here, we calulate an alternating sum. // 1 - 2 + 3 - 4 + 5 - 6 .... //So, if the current number is odd, we add that number to the sum. Otherwise (it's even), subtract. count =1; sum=0; do { if(count%2==1) // number is odd if, after division by 2, we have a remainder of 1 sum = sum + count; else sum = sum - count; count++; }while(count<=limit); System.out.println("The alternating sum is: "+ sum); } }