Navigate
JavaScript Tutorial for Beginners
The Ultimate CSS Tutorial for Beginners
Data Structure Tutorial for Beginners
ReactJS Tutorial for Beginners
Java Tutorial for Beginners
Node.js Tutorial for Beginners
SQL Tutorial for Beginners
Control Flow Statements
Content
- If-Else-Statement
- Switch Statement
- Loops
- Activity
- Quiz
- Updated on 10/09/2024
- 450 Views
If-Else Statement
Used to execute code based on a condition. Syntax: if (condition) { // code } else { // code }
Switch Statement
Allows multiple possible execution paths based on the value of a variable. Syntax: switch (variable) { case value1: // code; break; case value2: // code; break; default: // code; }
Loops
For Loop :
Executes code a fixed number of times.
Executes code as long as a condition is true.
Executes code at least once before checking the condition.
While Loop :
Do-While Loop :
Activity
Write a Java program using different control flow statements to print numbers from 1 to 20.
Quiz
1. Which control flow statement is used to execute code based on a condition?
- a) For Loop
- b) If-Else
- c) Switch
- d) Do-While Loop
2. What does the break statement do in a switch case?
- a) It ends the program.
- b) It skips to the next case.
- c) It exits the switch statement.
- d) It repeats the case.
3. What is the output of the following code?
- a) 0 1 2
- b) 1 2 3
- c) 0 1 2 3
- d) 1 2
4. Which loop is guaranteed to execute at least once?
- a) For Loop
- b) While Loop
- c) Do-While Loop
- d) None
5. How many times will the following code print 'Hello'?
- a) 4
- b) 5
- c) 6
- d) 1