0% found this document useful (0 votes)
24 views

Control flow Statements

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Control flow Statements

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Control flow Statements

1. What is control flow in Java, and why is it important in programming?

Answer: Control flow refers to the order in which statements in a program are executed. It’s essential for
defining the logic and sequence of operations in a program.

2. What are the primary categories of control flow in Java?

Answer: The primary categories of control flow in Java are conditional statements, loops, and method
calls.

3. Explain the ‘if’ statement in Java and its syntax.

Answer: The ‘if’ statement is used for conditional execution. Syntax:


if (condition) {
// Code to execute if condition is true
}
4.explain ‘if-else’ statement in java and its syntax?
Answer: if-else Statement is an extension of the if statement, which uses another block of code i.e., else
block. If the condition inside if statement is true, code executed inside if statement. Otherwise, else block
is executed.
Syntax:
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
4. else{
5. statement 2; //executes when condition is false
6. }

5. explain ‘if-else-if’ ladder statement in java and its syntax?


Answer: The if-else-if statement contains the if-statement followed by multiple else-if statements.
Syntax:
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. }
4. else if(condition 2) {
5. statement 2; //executes when condition 2 is true
6. }
7. else {
8. statement 2; //executes when all the conditions are false
9. }
7. Explain the nested ‘if’ statement in Java and its syntax?

Answer: The nested if statement represents the if block within another if block. Here, the inner if block
condition executes only when outer if block condition is true.
Syntax:
1. if(condition){
2. //code to be executed
3. if(condition){
4. //code to be executed
5. }
6. }

8. Explain the ‘switch’ statement in Java and its syntax?


Answer: The Java switch statement executes one statement from multiple conditions. It is like if-else-
if ladder statement. If all cases in switch statement are not matched, then the default case to be executed.
Syntax:
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }

9. What is the purpose of the ‘break’ statement in a ‘switch’ statement?

Answer: The ‘break’ statement is used to exit the ‘switch’ statement after a case is executed.

10. What is the ‘for’ loop in Java, and how is it used?


Answer: The Java for loop is used to iterate a part of the program several times. If the number of iterations
is fixed, then for loop is best to use.
1. for(initialization; condition; increment/decrement){
2. //statement or code to be executed
3. }

11. What is the ‘nested-for’ loop in Java?


Answer: If we have a for loop inside the another for loop, it is known as nested for loop.
12.What is the ‘for-each’ loop in Java?
Answer: The for-each loop is used to traverse array or collection in Java.
Syntax:
1. For (datatype variable : array_name){
2. //code to be executed
3. }

13. What is the infinite for loop in Java?


Answer: If you use only two semicolons ; in the for loop, it will be infinitive for loop.
14. Explain the ‘while’ loop in Java and its syntax?
Answer: The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean
condition is true. The loop is exit if the condition is false.
Syntax:
1. while (condition){
2. //code to be executed
3. Increment / decrement statement
4. }

15. Explain the ‘do-while’ loop in Java and its syntax?

Answer: The Java do-while loop is used to iterate a part of the program repeatedly, until the specified
condition is true. But The Java do-while loop is executed at least once because condition is checked after
loop body.

Syntax:

1. do{
2. //code to be executed / loop body
3. //update statement
4. }while (condition);

16. Explain the ‘break’ statement in Java and its role in control flow?

Answer: The Java break statement is used to break loop or switch statement. It breaks the current flow
of the program at specified condition. In case of inner loop, it breaks only inner loop.

17. What is the ‘continue’ statement in Java, and how does it affect loop execution?

Answer: The ‘continue’ statement is used to skip the current iteration of a loop and proceed to the next
iteration without executing the remaining code in the current iteration.
18. What is a return statement in Java?

Answer: the return statement is used for returning a value when the execution of the block is completed.

19.what is java comments?

Answer: The Java comments are the statements in a program that are not executed by the compiler and
interpreter.

20. Why do we use comments in a code?


Answer: Comments are used to make the program more readable by adding the details of the code. It
makes easy to maintain the code and to find the errors easily.

21. What are the types of comments?

Answer: There are three types of comments in Java.

1. Single Line Comment – these comments are used to comment only one line. Single line comments
starts with two forward slashes (//).
2. Multi Line Comment -are used to comment multiple line of code. Multi-line comments are placed
between /* and */.
3. Documentation Comment – is used to create documentation API. To create documentation API, You
need to use Javadoc tool. The documentation comments are placed between /** and */.

You might also like