default Keyword
The default
branch in a switch
runs when none of the case
labels match.
It is optional but recommended to handle unexpected values.
Syntax
switch (expr) {
case v1:
// ...
break;
// ...
default:
// fallback logic
}
Examples
// তাপমাত্রা ব্যান্ড
const তাপ = 37;
বিকল্প (true) {
অবস্থা তাপ < 0:
কনসোল.সতর্ক("বরফ জমার মতো ঠাণ্ডা");
থামুন;
অবস্থা তাপ >= 0 && তাপ < 30:
কনসোল.লগ("স্বাভাবিক/সহনীয়");
থামুন;
অন্যথায়:
কনসোল.লগ("গরম বা হ্যান্ডল করা হয়নি");
}
// ডিফল্ট যেকোনো জায়গায় থাকতে পারে (break না দিলে fall-through হবে)
const কোড = "X";
বিকল্প (কোড) {
অন্যথায়:
কনসোল.লগ("অজানা কোড");
// এখানে থামুন নেই, তাই পরের ব্লকে পড়বে
অবস্থা "A":
কনসোল.লগ("A হ্যান্ডলড");
থামুন;
}
Notes
- Only one
default
is allowed per switch. default
can appear anywhere; execution jumps there only when nocase
matches.- If you place
default
before other cases and omitbreak
, execution will fall through to the next case.