0
0
C Sharp (C#)programming~10 mins

Relational patterns in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Relational patterns
Start with a value
Apply relational pattern
Check if value matches condition
Yes No
Execute True
End
Relational patterns check if a value meets a condition like >, <, >=, <=, ==, or != and then choose what to do based on that.
Execution Sample
C Sharp (C#)
int x = 7;
if (x is > 5 and < 10)
{
    Console.WriteLine("x is between 6 and 9");
}
This code checks if x is greater than 5 and less than 10, then prints a message.
Execution Table
StepVariable xCondition CheckedResultBranch TakenOutput
17x > 5TrueContinue
27x < 10TrueExecute True branchx is between 6 and 9
37End---
💡 Both conditions true, so the true branch runs and then execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
x7777
Key Moments - 2 Insights
Why do we check both conditions separately in the relational pattern?
Because the pattern uses 'and', both conditions must be true for the whole pattern to be true, as shown in steps 1 and 2 of the execution_table.
What happens if the first condition is false?
The second condition is not checked and the false branch runs, but in this example, the first condition is true, so both are checked.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the condition 'x > 5' at step 1?
ANot evaluated
BTrue
CFalse
DError
💡 Hint
Check the 'Result' column at step 1 in the execution_table.
At which step does the program decide to execute the true branch?
AStep 2
BStep 1
CStep 3
DBefore step 1
💡 Hint
Look at the 'Branch Taken' column in the execution_table.
If x was 4, how would the execution_table change at step 1?
ACondition 'x > 5' would be True
BCondition 'x < 10' would be False
CCondition 'x > 5' would be False
DOutput would be 'x is between 6 and 9'
💡 Hint
Refer to the 'Condition Checked' and 'Result' columns for step 1.
Concept Snapshot
Relational patterns check if a value meets a condition like >, <, >=, <=, ==, or !=.
Syntax example: if (x is > 5 and < 10) { ... }
Both conditions must be true if combined with 'and'.
Used to simplify multiple comparisons in one pattern.
Executes code based on whether the pattern matches.
Full Transcript
Relational patterns in C# let you check if a value meets conditions like greater than or less than. The code example shows checking if x is greater than 5 and less than 10. The execution flow first checks if x > 5, which is true for x=7, then checks if x < 10, also true. Because both are true, the program runs the true branch and prints the message. Variables stay the same during checks. If any condition fails, the false branch runs instead. This helps write clear and simple condition checks.