Concept Flow - Constant patterns
Start
Match value with constant pattern
End
The program checks if a value matches a constant pattern. If yes, it runs matched code; if no, it runs other code or skips.
int number = 5; if (number is 5) { Console.WriteLine("Number is five"); } else { Console.WriteLine("Number is not five"); }
| Step | Variable 'number' | Condition 'number is 5' | Branch Taken | Output |
|---|---|---|---|---|
| 1 | 5 | True | If branch | Number is five |
| 2 | 5 | N/A | End | Program ends |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| number | 5 | 5 | 5 |
Constant patterns check if a value equals a fixed constant. Syntax: 'variable is constantValue'. If true, matched code runs. If false, else or other code runs. Useful for clear, readable condition checks.
switch statement?if statement in C#?is followed by the constant value to check equality.if (x is 10), which is correct. Others have syntax errors.int number = 5;
switch (number)
{
case 3:
Console.WriteLine("Three");
break;
case 5:
Console.WriteLine("Five");
break;
default:
Console.WriteLine("Other");
break;
}object obj = 10;
if (obj is 10)
{
Console.WriteLine("Ten");
}obj is 10 correctly to check if obj equals 10. This is valid syntax and logic.var numbers = new List<int> {1, 7, 3, 7, 5};
int count = 0;
foreach (var n in numbers)
{
// Fill in the blank
}
Console.WriteLine(count);if (n is 7) which checks if n equals 7, then increments count.Pass the test and this pattern moves unknown