# Stack data queries

Bitcoin Script operates on a stack-based execution model, but sometimes you need information about the stack itself rather than the data it contains. How many items are on the stack? How large is the top item? **Stack data query opcodes** provide these essential introspection capabilities, enabling scripts to make decisions based on stack state and data characteristics.

Understanding stack queries is fundamental for developers building robust Bitcoin Script applications. Whether you're validating input formats, implementing flexible payment conditions, or creating scripts that adapt to different input patterns, these opcodes provide the diagnostic tools you need. From simple item counting to sophisticated conditional logic based on data size, mastering stack queries unlocks more intelligent script design.

#### In this course, you will explore:

* How **OP\_DEPTH** counts stack items for conditional branching
* Practical applications for stack depth checking
* Using **OP\_SIZE** to validate data length requirements
* Combining stack queries with conditional logic
* Input validation patterns for user-submitted data
* Best practices for building adaptive scripts

#### By the end of this chapter, learners will:

* Understand the purpose and operation of each stack query opcode
* Apply stack depth checks to create flexible validation logic
* Implement data size requirements for protocol compliance
* Design scripts that adapt to different input patterns
* Build robust input validation using stack queries


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bsv-opcodes/stack-data-queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
