Definition: Scrutinee

Scrutinee refers to the expression or value being examined or analyzed in a pattern matching construct, such as a switch statement, if-else, or a pattern-matching expression. Scrutinee is typically compared against patterns or conditions to determine the appropriate action.

Uses of scrutinee

In any programming language, it can be used in multiple places, but the prominent ones are:

  • Switch statement

  • If-else

  • Match in modern programming languages (Rust, F#)

  • Destructuring assignments

  • Error handling

Know more about switch statement, if-else, match, and destructuring assignments.

Scrutinee in switch statement

In the example below, the variable "day" is a scrutinee that is evaluated against different cases in the switch statement. The corresponding code block is executed if the value matches a specific case. If none of the cases match, the code within the default block is executed.

// Following code is in dart Language
void main() {
final day = 2; // here day is scrutinee
// In the switch statement, day is being evaluated
// based on its value and specific code will be executed.
switch (day) {
case 1:
print("Monday");
break;
case 2:
print("Tuesday");
break;
case 3:
print("Wednesday");
break;
// ...
default:
print("Invalid day");
}
}

Scrutinee in if-else

In the example below, the variable "age" is a scrutinee that is being evaluated in an if-else statement and executes the code based on the result.
If age is less than 18, the first if condition is true, and the code within that block is executed. If age is between 18 and 65, the second condition is true, and its associated code block is executed. If neither of the previous conditions is true, the else block is executed.

// Following code is in dart Language
void main() {
final age = 67; // here age is scrutinee
// In the if-else statement, age is being evaluated
// based on its value and specific code will be executed.
if (age < 18) {
print("You are a teenager");
} else if (age >= 18 && age < 65) {
print("You are an adult");
} else {
print("You are a senior citizen");
}
}

Conclusion

Scrutinee is a crucial element in programming. These were just a few examples to illustrate the scrutinee concept. The specific usage and terminology may vary across programming languages and frameworks, but the fundamental idea of examining a value or object remains consistent. Scrutinee allows developers to make decisions, perform actions, or extract data based on the characteristics of the value being scrutinized.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved