🧪
Lab 3 — Active Decision & Branching Logic
Lab 3: Intro 🏠 Home
PSUC Lab 3 — Conditional Branching

Active Logic: Decision Control Structures

Master simple if, two-way if-else, multi-way if-else-if ladders, relational operators, and logical operators (&&, ||, !).

C Branching Constructs & Operators

Construct / OperatorSyntaxKey Rule & Trap
Equality Operatora == bAssignment a = b is a bug! Use ==
Logical ANDcond1 && cond2Both conditions MUST be true
Logical ORcond1 || cond2At least ONE condition MUST be true
If-Else Ladderif ... else if ... elseFirst true branch executes; rest skipped
Nested Ifif (c1) { if (c2) ... }Evaluates inner condition only if outer is true

🎯 Learning Philosophy: Active Logic Building

Test your decision-making logic across 3 Interactive Learning Modes:

  1. 🧠 Tab 1 (Logic & Hints): Decision rules & truth table breakdowns.
  2. 🧩 Tab 2 (Logic Puzzle): Fill in missing boolean operators (==, &&, ||) & run live testers.
  3. 💻 Tab 3 (C Solution): Reference verified C code solutions.
Use the mode tabs on each slide to solve the branching puzzle before peeking at code!
Program 1 of 6

Odd / Even Check & Multiples Verification

Determine if a number is even (num % 2 == 0) or odd, and verify if integer A is a multiple of integer B (a % b == 0).

Modulus Branching Logic

  • Even Condition: n % 2 == 0 (Remainder when divided by 2 is 0)
  • Odd Condition: n % 2 != 0 (Remainder is 1 or -1)
  • Multiple Condition: a % b == 0 (No remainder when A is divided by B)

Interactive Logic Hints

💡 Hint 1: Why must we write == instead of = in if()? ▼
In C, if (n % 2 = 0) causes a compilation syntax error! = is assignment, == is comparison!
💡 Hint 2: What happens if B = 0 in multiples check? ▼
Division by zero error! Always ensure divisor B is non-zero before evaluating a % b!

🧩 Complete the Even/Odd & Multiples Logic

int num = 24, a = 30, b = 5;
if (num % 2 0) {
  printf("EVEN");
}
if (a b 0) {
  printf("A is multiple of B");
}
Select operators above and click Check!

🔢 Live Even/Odd & Divisibility Tester

Result will show here...
even_multiples.cReference C Code
#include <stdio.h> int main() { int num, a, b; printf("Enter number: "); scanf("%d", &num); if (num % 2 == 0) { printf("%d is EVEN\n", num); } else { printf("%d is ODD\n", num); } printf("Enter A and B: "); scanf("%d %d", &a, &b); if (b != 0 && a % b == 0) { printf("%d IS a multiple of %d\n", a, b); } else { printf("%d is NOT a multiple of %d\n", a, b); } return 0; }
Program 2 of 6

Leap Year Detection (The Century Rule)

A year is a leap year if it is divisible by 400 OR (divisible by 4 AND NOT divisible by 100).

Leap Year Decision Logic

  • Rule 1 (Century Years): Year % 400 == 0 (e.g. 2000 is leap)
  • Rule 2 (Regular Years): Year % 4 == 0 AND Year % 100 != 0 (e.g. 2024 is leap, 1900 is NOT)
  • Combined: (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)

Interactive Logic Hints

💡 Hint 1: Why is 1900 NOT a leap year even though 1900 % 4 == 0? ▼
Because century years (ending in 00) must be divisible by 400! 1900 % 100 == 0, but 1900 % 400 != 0.

🧩 Complete the Leap Year Compound Boolean Logic

if ((year % == 0) (year % 4 == 0 year % 100 0)) {
  printf("Leap Year");
}
Select operators above and click Check!

📅 Leap Year Rule Evaluator

Result will show here...
leap_year.cReference C Code
#include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { printf("%d IS a Leap Year (366 days)\n", year); } else { printf("%d is NOT a Leap Year (365 days)\n", year); } return 0; }
Program 3 of 6

Roots of a Quadratic Equation (ax2 + bx + c = 0)

Classifies discriminant D = b2 - 4ac using multi-way if-else-if ladder (D > 0, D == 0, D < 0).

Discriminant Conditions

  • disc > 0: Real & Distinct roots: (-b ± √D) / (2a)
  • disc == 0: Real & Equal roots: -b / (2a)
  • disc < 0: Complex Imaginary roots: -b/(2a) ± i √(-D)/(2a)

Interactive Logic Hints

💡 Hint 1: What happens if a = 0? ▼
If a = 0, division by 2*a causes undefined division by zero! (It becomes a linear equation, not quadratic).

🧩 Complete Discriminant Ladder Conditions

disc = b*b - 4*a*c;
if (disc 0) {
  printf("Real & Distinct");
} else if (disc 0) {
  printf("Real & Equal");
} else {
  printf("Imaginary");
}
Select operators above and click Check!

🧮 Interactive Quadratic Solver

Result will show here...
quadratic_roots.cReference C Code
#include <stdio.h> #include <math.h> int main() { double a, b, c, disc, r1, r2, realPart, imagPart; printf("Enter coefficients a, b, c: "); scanf("%lf %lf %lf", &a, &b, &c); disc = b*b - 4*a*c; if (disc > 0) { r1 = (-b + sqrt(disc)) / (2*a); r2 = (-b - sqrt(disc)) / (2*a); printf("Real & Distinct: %.2lf, %.2lf\n", r1, r2); } else if (disc == 0) { r1 = -b / (2*a); printf("Real & Equal: %.2lf\n", r1); } else { realPart = -b / (2*a); imagPart = sqrt(-disc) / (2*a); printf("Imaginary: %.2lf ± %.2lfi\n", realPart, imagPart); } return 0; }
Program 4 of 6

Grades Ladder & Triangle Inequality Validation

Assigns letter grades using if-else-if ladder and validates triangle inequality (a + b > c && a + c > b && b + c > a).

Triangle Inequality Theorem

  • The sum of any two sides MUST be strictly greater than the third side!
  • Condition: (a + b > c) && (a + c > b) && (b + c > a)
  • If valid, semi-perimeter s = (a+b+c)/2.0 & Heron's Area sqrt(s*(s-a)*(s-b)*(s-c))

Interactive Logic Hints

💡 Hint 1: Why must we use && instead of comma or separate ifs? ▼
All 3 side conditions MUST hold simultaneously for a valid 2D triangle. Logical AND && enforces all 3!

🧩 Complete Triangle Inequality & Grade Logic

if (marks >= 90) printf("A+");
else if (marks >= 80) printf("A");

if (a + b c a + c > b && b + c > a) {
  s = (a + b + c) / 2.0;
  area = sqrt(s * (s-a) * (s-b) * (s-c));
}
Select operators above and click Check!

📊 Grade & Triangle Tester

Result will appear here...
grade_triangle.cReference C Code
#include <stdio.h> #include <math.h> int main() { float marks, a, b, c, s, area; printf("Enter marks: "); scanf("%f", &marks); if (marks >= 90) printf("Grade: A+\n"); else if (marks >= 80) printf("Grade: A\n"); else if (marks >= 70) printf("Grade: B\n"); else if (marks >= 60) printf("Grade: C\n"); else printf("Grade: F\n"); printf("Enter sides a b c: "); scanf("%f %f %f", &a, &b, &c); if (a+b > c && a+c > b && b+c > a) { s = (a + b + c) / 2.0; area = sqrt(s * (s-a) * (s-b) * (s-c)); printf("Valid Triangle! Area = %.2f\n", area); } else { printf("Invalid Triangle!\n"); } return 0; }
Program 5 of 6

Days in a Month Calculator (Leap Year Aware)

Groups months with 31 days, 30 days, and handles February (28 vs 29 days based on leap year).

Month Days Rules

  • 31 Days: Month 1, 3, 5, 7, 8, 10, 12 (Use OR ||)
  • 30 Days: Month 4, 6, 9, 11
  • February (Month 2): 29 days if leap year, 28 days otherwise!

Interactive Logic Hints

💡 Hint 1: Why use || (OR) operator for month grouping? ▼
A month can only be ONE number at a time (e.g. month == 1 OR month == 3). Using && would be impossible!

🧩 Complete Month Grouping Conditions

if (m == 1 m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
  printf("31 Days");
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
  printf("30 Days");
} else if (m 2) {
  // February leap check...
}
Select operators above and click Check!

📆 Month Days Calculator

Result will show here...
month_days.cReference C Code
#include <stdio.h> int main() { int month, year; printf("Enter month (1-12) & year: "); scanf("%d %d", &month, &year); if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { printf("31 Days\n"); } else if (month == 4 || month == 6 || month == 9 || month == 11) { printf("30 Days\n"); } else if (month == 2) { if ((year%400==0) || (year%4==0 && year%100!=0)) printf("29 Days (Leap Year)\n"); else printf("28 Days\n"); } else { printf("Invalid Month Number!\n"); } return 0; }
Program 6 of 6

Character Classifier (Vowel, Consonant, Digit, Special)

Classifies input character using vowel checks and <ctype.h> helper functions (isalpha, isdigit).

Classification Rules

  • Header required: #include <ctype.h>
  • Vowels: ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u' (also uppercase A,E,I,O,U)
  • Alphabet Check: isalpha(ch)
  • Digit Check: isdigit(ch)

Interactive Logic Hints

💡 Hint 1: Why space before %c in scanf(" %c", &ch)? ▼
The leading space in " %c" instructs scanf to skip any leftover newline or whitespace characters!

🧩 Complete Character Classifier Logic

#include <>

if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') printf("Vowel");
else if ((ch)) printf("Consonant");
else if ((ch)) printf("Digit");
Select functions above and click Check!

🔤 Interactive Character Classifier

Result will appear here...
char_classifier.cReference C Code
#include <stdio.h> #include <ctype.h> int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'|| ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { printf("'%c' is a VOWEL\n", ch); } else if (isalpha(ch)) { printf("'%c' is a CONSONANT\n", ch); } else if (isdigit(ch)) { printf("'%c' is a DIGIT\n", ch); } else { printf("'%c' is a SPECIAL SYMBOL\n", ch); } return 0; }
Knowledge Check

Test Your Control Structures Mastery

Question 1 of 5
Loading question...