30-12-24 ---------- Q)What do you know about iteration statements in Java. =>Whenever any task performing code is required to be executed repeatedly, we go for iteration statements. =>There are 4 repetetion/looping/iteration statements in Java 1)for statement 2)while statement 3)do while statement 4)enhanced for statement(for each statement) Q)Explain about for statement(for loop) =>For statement has 4 parts. 1.)Loop counter initialization 2.)condition 3.)body 4.)loop counter updation(incrementation/decrementation) Note:- A variable used in condition is known as a loop counter. =>Loop counter initialization happens only once. =>Condition is verified 'n' number of times. =>As long as the condition is true, body is executed repeatedly. FLOW CHART --------------- Q)Java program to print first 10 natural numbers using for loop,while loop and do while loop. class OneToTen{ public static void main(String[] args){ for(int i=1;i<=10;i++){ System.out.println(i); }//for }//main }//class ************************* class OneToTen{ public static void main(String[] args){ int i=1;//loop counter initialization while(i<=10){ System.out.println(i); i++; }//while }//main }//class ****************************************** Q)When to use for loop and when to use while loop? =>There is no hard and fast rule about which loop to use. We can use any loop to solve repetetion problem. =>Whenever repetetion is for a fixed number of times, we better go for for loop for coding convenience. =>If we don't know in advance how many repetetions are required to solve the given problem, we go for while loop. Q)What are the things to know to solve a computing problem? 1)IPO 2)flow control structure to be used Q)Java application to find the sum of the digits of a number. import java.util.Scanner; class SumOfDigits{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); System.out.println("Enter a number:"); int n=scanner.nextInt(); int sum_of_digits=0,remainder; while(n>0) { remainder=n%10; sum_of_digits=sum_of_digits+remainder; n=n/10; }//while System.out.println("The sum of the digits is "+sum_of_digits); }//main }//class