The document discusses Java's decision control structures, including looping control structures like for, while, and do-while loops. It explains the purpose of the default case in switch statements, the syntax of if statements, and provides a comparison between do-while and while loops. Additionally, it details the workings of the for loop with an example demonstrating its use to display a message multiple times.
The document discusses Java's decision control structures, including looping control structures like for, while, and do-while loops. It explains the purpose of the default case in switch statements, the syntax of if statements, and provides a comparison between do-while and while loops. Additionally, it details the workings of the for loop with an example demonstrating its use to display a message multiple times.
1. Java provides three basic looping control structures. 2. The for loop is also called an Entry controlled loop. 3. The do while first executes the block of statements and then checks the condition. 4. The order in which statements are executed in a running program is called the Flow of control. Answer the following: 1.What is the purpose of using default case in switch statement? Ans: The default case can be used for performing a task , when none of the cases is true. 2.What is an if statement ? Write its syntax. Ans : The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. Syntax : if(condition) { // Statements to execute if // condition is true }
3.Explain the situation where the switch statement is used.
Ans : If we have conditions where values are fixed or we have options like 1,2,3,4 and so on. for example programs for automated complaint services where options are there and you have to choose any one according to our problems. In such situations using the SWITCH statement in the program is the best. 4.Give the difference between do-while loop and while loop. Ans : While Do-while 1. Condition is checked first then 1. Statement(s) is executed at statement(s) is executed. least once, thereafter the condition is checked. 2. No semicolon at the end of 2. Semicolon at the end of while. while. while(condition); while(condition) 3. Brackets are always required. 3. If there is a single statement, brackets are not required. 4. do-while loop is an exit 4. while loop is entry controlled controlled loop. loop. 5. Variable in condition is 5. variables may be initialized initialized before the before or within the loop. execution of the loop.
5. Explain the working of the for loop with an appropriate example.
Ans : For loop is used to run a block of code for a certain number of times. The syntax of For loop is: for (initialExpression; testExpression; updateExpression) { // body of the loop }
1. The initialExpression initializes and/or declares variables and
executes only once. 2. The condition is evaluated. If the condition is true, the body of the for loop is executed. 3. The updateExpression updates the value of initialExpression. 4. The condition is evaluated again. The process continues until the condition is false. Example : Display a text five times class Main { public static void main(String[] args) { int n = 5; // for loop for (int i = 1; i <= n; ++i) { System.out.println("Java is fun"); } } }