Control - Structures - Loops - Conditionals Part 9
Control - Structures - Loops - Conditionals Part 9
• Java:
• int age = 18;
• if (age >= 18) {
• System.out.println('You are an adult');
• } else {
• System.out.println('You are a minor');
• }
Loops in Programming
• Loops allow repeated execution of a block of
code.
• Types of Loops:
• - for loop: Iterates a fixed number of times.
• - while loop: Repeats as long as a condition is
true.
• - do-while loop: Executes at least once before
checking the condition.
Example: for Loop
• Python:
• for i in range(5):
• print('Iteration:', i)
• Java:
• for (int i = 0; i < 5; i++) {
• System.out.println('Iteration: ' + i);
• }
Example: while Loop
• Python:
• count = 0
• while count < 5:
• print('Count:', count)
• count += 1
• Java:
• int count = 0;
• while (count < 5) {
• System.out.println('Count: ' + count);
• count++;
• }
Conclusion
• Control structures are essential for directing
program execution.