Assignment - Class 8 - Decision Control Structure

Assignment — Decision Control Structure (With Solutions)
Practice, Solve and Master Decision Making! This complete assignment guide includes all questions from Section A (Theory), Section B (Programs), and Section C (Output Prediction) — with detailed solutions, explanations, and Java code!
Assignment Overview
- Total Marks: 100
- Section A (Theory): 30 marks
- Section B (Programs): 50 marks
- Section C (Output Prediction): 20 marks
- Language: Java (BlueJ IDE)
Motivation
This assignment is designed to test all aspects of decision control structures. Complete each section carefully, use the solutions to check your answers, and learn from any mistakes!
Objectives
Learning Goals
- ✅ Understand decision statements.
- ✅ Write Java if programs.
- ✅ Use logical operators.
- ✅ Implement nested if.
- ✅ Create else-if ladder.
- ✅ Debug and test programs.
Section A: Theory Questions (30 Marks)
Q1. What is a decision control statement? Give one example. (6 marks)
A decision control statement is a programming construct that allows a program to make decisions and execute different code based on conditions.
Example:
if (marks >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
If marks are 50 or above, the program prints "Pass"; otherwise, it prints "Fail".
Q2. Explain if, if-else and nested if with syntax. (6 marks)
1. if Statement: Executes code only if condition is true.
if (condition) {
// code to execute
}
// Example:
if (age >= 18) {
System.out.println("Can vote");
}
2. if-else Statement: Executes one block if true, another if false.
if (condition) {
// code if true
} else {
// code if false
}
// Example:
if (num % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
3. Nested if: An if statement inside another if statement.
if (condition1) {
if (condition2) {
// code if both conditions true
}
}
// Example:
if (age >= 18) {
if (hasID) {
System.out.println("Can vote");
}
}
Q3. What is the difference between if-else and else-if ladder? (6 marks)
| Feature | if-else | else-if ladder |
|---|---|---|
| Number of conditions | Only one condition | Multiple conditions in sequence |
| Number of blocks | 2 blocks (if + else) | Multiple blocks |
| Use case | Two-way decision | Multiple choices |
| Example | Even/Odd check | Grade calculation (A/B/C/D/F) |
if-else Example:
if (num > 0)
System.out.println("Positive");
else
System.out.println("Non-positive");
else-if ladder Example:
if (marks >= 90) grade = "A";
else if (marks >= 75) grade = "B";
else if (marks >= 60) grade = "C";
else grade = "F";
Q4. Explain logical operators (&&, ||, !) with examples. (6 marks)
Logical operators combine multiple conditions in decision statements.
1. AND (&&): Returns true if BOTH conditions are true.
// Example: Check voting eligibility
if (age >= 18 && citizen == true) {
System.out.println("Can vote");
}
2. OR (||): Returns true if AT LEAST ONE condition is true.
// Example: Check if day is weekend
if (day.equals("Saturday") || day.equals("Sunday")) {
System.out.println("Weekend");
}
3. NOT (!): Reverses the condition (true becomes false, false becomes true).
// Example: Check if not zero
if (!(num == 0)) {
System.out.println("Non-zero");
}
Truth Table Summary:
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| T | T | T | T | F |
| T | F | F | T | F |
| F | T | F | T | T |
| F | F | F | F | T |
Q5. What are the common errors in decision statements? (6 marks)
Common errors in decision statements include:
- Using = instead of ==:
- Wrong:
if (x = 5) - Correct:
if (x == 5)
- Wrong:
- Missing braces { }:
- Only first line executes, causing bugs.
- Missing else block:
- Default case not handled.
- Wrong operator precedence:
- Not using parentheses in complex conditions.
- Case-sensitive issues:
- Java is case-sensitive;
Ifis notif.
- Java is case-sensitive;
- Comparing strings with ==:
- Wrong:
if (name == "John") - Correct:
if (name.equals("John"))
- Wrong:
- Deep nesting:
- Too many nested ifs make code unreadable.
- Not testing edge cases:
- Missing 0, negative, or boundary values.
Section B: Program Writing (50 Marks)
P1. Program to check if a number is positive, negative, or zero. (10 marks)
import java.util.Scanner;
public class CheckNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n > 0) {
System.out.println("Positive");
} else if (n < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
}
}
- Input: 5 → Output: Positive
- Input: -3 → Output: Negative
- Input: 0 → Output: Zero
P2. Program to find the greatest of three numbers. (10 marks)
import java.util.Scanner;
public class GreatestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (a >= b && a >= c) {
System.out.println(a + " is the greatest");
} else if (b >= a && b >= c) {
System.out.println(b + " is the greatest");
} else {
System.out.println(c + " is the greatest");
}
}
}
- Input: 5 10 3 → Output: 10 is the greatest
- Input: 20 15 8 → Output: 20 is the greatest
- Input: 3 6 9 → Output: 9 is the greatest
P3. Program to check if a year is a leap year. (10 marks)
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();
// A year is a leap year if:
// - divisible by 4 AND not divisible by 100
// - OR divisible by 400
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a Leap Year");
} else {
System.out.println(year + " is Not a Leap Year");
}
}
}
- Input: 2024 → Output: 2024 is a Leap Year
- Input: 2023 → Output: 2023 is Not a Leap Year
- Input: 2000 → Output: 2000 is a Leap Year
- Input: 1900 → Output: 1900 is Not a Leap Year
P4. Program to calculate grade based on marks (A/B/C/D/F). (10 marks)
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your marks (0-100): ");
int marks = sc.nextInt();
// Validate input
if (marks < 0 || marks > 100) {
System.out.println("Invalid marks!");
return;
}
String grade;
if (marks >= 90) {
grade = "A"; // Excellent
} else if (marks >= 75) {
grade = "B"; // Very Good
} else if (marks >= 60) {
grade = "C"; // Good
} else if (marks >= 40) {
grade = "D"; // Pass
} else {
grade = "F"; // Fail
}
System.out.println("Grade: " + grade);
}
}
- Input: 95 → Output: Grade: A
- Input: 80 → Output: Grade: B
- Input: 65 → Output: Grade: C
- Input: 45 → Output: Grade: D
- Input: 30 → Output: Grade: F
P5. Simple calculator using else-if ladder (+, -, *, /). (10 marks)
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char op = sc.next().charAt(0);
System.out.print("Enter second number: ");
double b = sc.nextDouble();
double result = 0;
boolean valid = true;
if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
} else if (op == '*') {
result = a * b;
} else if (op == '/') {
if (b != 0) {
result = a / b;
} else {
System.out.println("Error: Cannot divide by zero!");
valid = false;
}
} else {
System.out.println("Invalid operator!");
valid = false;
}
if (valid) {
System.out.println("Result: " + result);
}
}
}
- Input: 10 + 5 → Output: Result: 15.0
- Input: 20 - 8 → Output: Result: 12.0
- Input: 6 * 4 → Output: Result: 24.0
- Input: 15 / 3 → Output: Result: 5.0
- Input: 10 / 0 → Output: Error: Cannot divide by zero!
Section C: Output Prediction (20 Marks)
Q1. Predict the output. (4 marks)
int x = 10, y = 20;
if (x > y)
print("X");
else
print("Y");
Explanation: x = 10, y = 20. The
condition x > y becomes
10 > 20, which is FALSE. So the else
block executes, printing "Y".
Q2. Predict the output. (4 marks)
int a = 5;
if (a > 0 && a < 10)
print("In range");
Explanation: a = 5. Check
a > 0 (TRUE) AND a < 10
(TRUE). Both true, so it prints "In range".
Q3. Predict the output. (4 marks)
int marks = 45;
if (marks >= 60)
print("Pass");
else
print("Fail");
Explanation: marks = 45.
marks >= 60 becomes
45 >= 60, which is FALSE. So the else
block executes, printing "Fail".
Q4. Predict the output. (4 marks)
int x = 7;
if (x % 2 == 0)
print("Even");
else
print("Odd");
Explanation: x = 7.
x % 2 equals 1 (remainder when 7 is
divided by 2). 1 == 0 is FALSE. So the
else block executes, printing "Odd".
Q5. Predict the output. (4 marks)
int age = 17;
if (age >= 18)
print("Adult");
else
print("Minor");
Explanation: age = 17.
age >= 18 becomes 17 >= 18,
which is FALSE. So the else block executes, printing
"Minor".
Output Prediction Summary
| Question | Answer | Reason |
|---|---|---|
| Q1 | Y | 10 > 20 is FALSE |
| Q2 | In range | 5 > 0 AND 5 < 10 both TRUE |
| Q3 | Fail | 45 >= 60 is FALSE |
| Q4 | Odd | 7 % 2 = 1, not 0 |
| Q5 | Minor | 17 >= 18 is FALSE |
Submission Format
How to Submit
- ✅ Handwritten in notebook OR typed in A4 sheets.
- ✅ Java code with proper indentation.
- ✅ Include test cases and outputs.
- ✅ Clean handwriting/formatting.
- ✅ Submit on time.
- ✅ Include your name, roll number, and class.
- ✅ Number each question clearly.
- ✅ Underline final answers.
Evaluation Criteria (Total: 100 Marks)
| Section | Marks |
|---|---|
| Section A (Theory) | 30 |
| Section B (Programs) | 50 |
| Section C (Output) | 20 |
| Total | 100 Marks |
Tips for Success
Best Practices
- 📖 Read questions carefully.
- 📝 Write neat code.
- 🧪 Test with different inputs.
- 📐 Use proper indentation.
- 💬 Comment your code.
- 🎯 Handle edge cases.
- 📏 Follow syntax rules.
- 🔄 Practice daily.
Common Mistakes to Avoid
Watch Out for These!
- ❌ Missing semicolons.
- ❌ Wrong braces.
- ❌ Using = instead of ==.
- ❌ Missing else block.
- ❌ Incorrect logical operators.
- ❌ Not testing all conditions.
- ❌ Case-sensitive issues (Java).
- ❌ Deep nesting instead of else-if ladder.
Did You Know?
Fun Fact
Decision statements are used in every software! From games to banking apps, from social media to search engines — everything uses if-else to make choices! Every "if you do this, then that happens" is a decision statement in action.
Frequently Asked Questions
Q1. Can I use my own variable names in programs?
Yes! Use meaningful names. Just make sure your code works correctly.
Q2. How do I show test cases?
Include a table showing Input → Output for each program. Test at least 3 different cases.
Q3. Should I add comments in my code?
Yes! Comments earn marks and help teachers understand your logic.
Q4. What if my program has a small error?
Partial marks are awarded if logic is correct. Focus on getting the logic right.
Q5. Can I use Switch Case instead of else-if ladder?
For P5 (Calculator), Switch Case is a good alternative. But since the topic is if-else, else-if ladder is preferred.
Q6. How much time should I spend on each section?
Section A: 30 min | Section B: 60 min | Section C: 15 min. Adjust based on your speed.
Q7. What if I don't get the exact output?
Explain your reasoning. Partial marks are given for correct approach even if output is slightly different.
Q8. Should I show sample runs?
Yes! Show at least 2-3 sample runs with different inputs to demonstrate your program works correctly.
Key Takeaways
- Complete all 3 sections for full marks.
- Section A: 5 theory questions (30 marks).
- Section B: 5 programs (50 marks).
- Section C: 5 output predictions (20 marks).
- Use meaningful code and comments.
- Test with edge cases.
- Follow proper indentation.
- Handle all conditions.
- Submit on time.
- Learn from your mistakes!
Key Takeaway
Practice makes perfect! The more you
solve assignments, the stronger your programming becomes!
This assignment tests all aspects of decision control
structures — from theory to practical programming.
Complete it thoroughly and you'll be ready for any
programming challenge!
Complete all sections. Give your best effort.
You will excel in programming!
⭐ CODE SMART, THINK CLEAR, WRITE BETTER! ⭐
Best of Luck! Practice daily, understand
deeply, and code confidently! 👍😊
Flowchart → Visual Thinking → Smart Solutions → Better
Results! 🚀
Home & Online Tuition
Learn from an experienced tutor with personalized guidance.
Available Locations
Expert Home & Online Tuition
Personalized one-to-one tuition that focuses on concept building, practical learning, problem-solving skills, and excellent academic performance. Suitable for school students looking for structured, interactive, and result-oriented learning.
Subjects We Teach
Why Choose Our Tuition?
✅ Concept-Based Learning
✅ Practical Examples
✅ Weekly Tests
✅ Doubt Solving Sessions
✅ Practice Worksheets
✅ MCQ & Assignments
✅ Exam Preparation
✅ Flexible Class Timings