Home Work questions for class 05 conditional statements
-
Basic Loops:
- Question: Write a
forloop that prints the numbers from 10 to 1 in descending order. - Hint: Use a loop counter that starts at 10 and decrements by 1 until it reaches 1.
- Question: Write a
-
While Loop:
- Question: Write a
whileloop that prints the first 5 even numbers starting from 0. - Hint: Use a loop counter initialized to 0 and increment it by 2 in each iteration.
- Question: Write a
-
Do-While Loop:
- Question: Write a
do-whileloop that prints the numbers from 1 to 5. - Hint: Ensure the condition is checked after the loop body has executed.
- Question: Write a
-
Break Statement:
- Question: Write a
forloop that prints numbers from 0 to 10, but stops the loop when the number 7 is reached. - Hint: Use an
ifstatement to check if the loop counter equals 7 and then usebreak.
- Question: Write a
-
Continue Statement:
- Question: Write a
forloop that prints numbers from 0 to 10, but skips the number 5. - Hint: Use an
ifstatement to check if the loop counter equals 5 and then usecontinue.
- Question: Write a
-
If-Else Statement:
- Question: Write an
if-elsestatement that checks if a given numberxis positive, negative, or zero, and prints an appropriate message. - Hint: Use nested
ifstatements to handle all three conditions.
- Question: Write an
-
Switch Statement:
- Question: Write a
switchstatement that takes a variableday(with values from 0 to 6) and prints the corresponding day of the week (e.g., 0 for Sunday, 1 for Monday, etc.). - Hint: Each
caseshould correspond to a day of the week.
- Question: Write a
-
Ternary Operator:
- Question: Use the ternary operator to check if a given number
yis even or odd and print "Even" or "Odd" accordingly. - Hint: Use the modulus operator
%to determine if the number is even or odd.
- Question: Use the ternary operator to check if a given number
-
Combining Loops and Conditions:
- Question: Write a
forloop that prints numbers from 1 to 20. For multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz", and for multiples of both 3 and 5, print "FizzBuzz". - Hint: Use
if,else if, andelsestatements to check the conditions inside the loop.
- Question: Write a
-
Complex Condition:
- Question: Write an
if-elsestatement that takes a variabletemperatureand prints "Cold" if the temperature is below 15, "Warm" if it’s between 15 and 25, and "Hot" if it’s above 25. - Hint: Use compound conditions with logical operators
&&to check for the range between 15 and 25.
- Question: Write an