- 01Setting up (what you'll need)
- 02Your first output (Console.WriteLine)
- 03Using variables
- 04Branching (the If statement)
- 05Repeating (the For statement)
- 06Writing a function (a procedure)
- 07Working with arrays
- 08Repeating (the While statement)
- 09Working with classes (object orientation)
- 10Error handling (Try...Catch)
- 11Sorting an array
- 12Recursive functions
- 13Using the standard library
- 14The Select Case statement
- 15Conditional expressions (the If operator)
- 16Inheritance (extending a class)
- 17Writing comments
- 18Logical operators (AndAlso, OrElse, Not)
- 19Constants (Const)
- 20Splitting and joining strings (Split and Join)
- 21Writing null-safe code (the If operator)
- 22Searching an array (Array.IndexOf)
- 23Transforming an array (map)
- 24Two-dimensional arrays (tabular data)
- 25Creating your own exception class
- 26Default arguments (Optional parameters)
- 27Working with HashSet (sets)
- 28Checking correctness with Debug.Assert (a first step towards testing)
- 29Higher-order functions (passing a function as an argument)
- 30Stacks and queues (the basics of data structures)
- 31The basics of type conversion (casting)
- 32An introduction to regular expressions (pattern matching)
- 33The binary search algorithm
- 34Building a Caesar cipher (a letter-shifting cipher)
- 35Understanding how bubble sort works
- 36Assembling and displaying a date
- 37Writing several tests together (multiple test cases)
- 38Speeding up calculation with memoization (caching)
- 39Normalizing strings (Trim and case)
- 40Shallow copies versus deep copies
- 41The basics of enums
- 42Flattening a list
- 43Reversing a string and testing for a palindrome
- 44Pairing up two arrays (the zip operation)
- 45Rounding numbers (Floor, Ceiling, Round)
- 46Multi-line text (building it with vbCrLf)
- 47Returning several values from a function (tuples)
- 48Finding the GCD and LCM (the Euclidean algorithm)
- 49Formatting numbers (aligning digits and decimal places)
- 50Cleaning up with Try / Catch / Finally
- 51Writing general functions with generics
- 52The basics of reading and writing files
- 53Generating random numbers
- 54Bitwise operations (And, Or, Xor, shifts)
- 55Receiving command-line arguments
- 56Pausing for a set time (Thread.Sleep)
- 57Watch out for floating-point error
- 58Reading a value from standard input
- 59FizzBuzz (the classic exercise)
- 60Testing whether a number is prime
- 61Set operations with HashSet (union, intersection, difference)
- 62Converting between bases (binary and hexadecimal)
- 63Checking that brackets match (an application of stacks)
- 64Testing whether two words are anagrams
- 65Testing whether a year is a leap year
- 66Converting temperatures (Celsius and Fahrenheit)
- 67Finding the prime factors
- 68[Project] Build a simple inventory system
Testing whether a year is a leap year
This lesson covers testing for a leap year in VB.NET, with the aim of understanding how to write logic combining several conditions.
A leap year follows a slightly awkward rule: divisible by 4 but not by 100, or divisible by 400. Testing only for multiples of 4 gets some years wrong.
The sample code expresses that whole rule in the single expression (year Mod 4 = 0 AndAlso year Mod 100 <> 0) OrElse year Mod 400 = 0. 2024 divides by 4 and not by 100, so it is a leap year; 1900 divides by 100 but not by 400, so it is not.
A common early stumble is testing only divisibility by 4. Exceptions such as 1900 — a multiple of 4 that is nonetheless not a leap year — mean the 100 and 400 conditions have to be checked as well.
In professional work, this logic is essential to accurate day counts when implementing date calculations or calendar features.
๐งช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
