---
title: "Best Example for COBOL SEARCH and WHEN Condition"
description: "The top differences between Search and When condition in COBOL."
url: https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/
date: 2016-01-06
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2016/01/cobol-search.jpg?fit=1361%2C896&ssl=1
categories: ["COBOL"]
tags: ["Search in COBOL"]
type: post
lang: en
---

# Best Example for COBOL SEARCH and WHEN Condition

The below flow chart shows how **Search in COBOL** works. So, SEARCH is a serial search. [SEARCH ALL](https://srinimf.com/2016/01/07/5-top-limitations-of-cobol-search-all/) is a binary search.

Related[Why do We need to Sort Table Prior to COBOL SEARCH ALL](https://srinimf.com/2016/11/08/why-sorting-is-needed-for-search-all/)

## Search in COBOL

![Search](https://i0.wp.com/srinimf.com/wp-content/uploads/2016/01/search.png?resize=416%2C448&ssl=1)

### Keypoints

- Multiple WHEN conditions you can give
- AND , ‘=’ , ‘>’, ‘<‘ operators you can give in WHEN condition
- For ‘OR’ operator, the below way you can do coding in COBOL program

```
Search 
. . 
 WHEN A 
 WHEN B
 imperative sentence . . 
End-search
```

**Read more**: [Best tutorial for COBOL serial search](http://www-01.ibm.com/support/knowledgecenter/SSQ2R2_9.1.0/com.ibm.etools.cbl.win.doc/topics/tptbl36.htm)

- ![COBOL Search](https://i0.wp.com/srinimf.com/wp-content/uploads/2018/05/cobol-search.jpg?resize=1200%2C700&ssl=1)COBOL Search
- ![How to Tune COBOL Program](https://i0.wp.com/srinimf.com/wp-content/uploads/2015/11/cobol-comp-data-type.jpg?resize=1200%2C700&ssl=1)How to Tune COBOL Program
- ![COBOL Examples](https://i0.wp.com/srinimf.com/wp-content/uploads/2017/03/cobol-examples.jpg?resize=1200%2C700&ssl=1)COBOL Examples

## Example for SEARCH:

```
01  TABLE-ONE.     
 05 TABLE-ENTRY1 OCCURS 10 TIMES INDEXED BY TE1-INDEX.        
  10 TABLE-ENTRY2 OCCURS 10 TIMES INDEXED BY TE2-INDEX.
   15 TABLE-ENTRY3 OCCURS 5 TIMES  ASCENDING KEY IS KEY1 INDEXED BY TE3-INDEX.   
    20 KEY1                 PIC X(5).              
    20 KEY2                 PIC X(10)
. . . . 
PROCEDURE DIVISION.     
. . .     
SET TE1-INDEX TO 1     
SET TE2-INDEX TO 4     
SET TE3-INDEX TO 1     

MOVE “A1234” TO KEY1 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2)     
MOVE “AAAAAAAA00” TO KEY2 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2)    
 . . .
SEARCH TABLE-ENTRY3       
AT END         
 MOVE 4 TO RETURN-CODE.       
  WHEN TABLE-ENTRY3(TE1-INDEX, TE2-INDEX, TE3-INDEX) = “A1234AAAAAAAA00”         
 MOVE 0 TO RETURN-CODE     
END-SEARCH
```

Related

- [COBOL SEARCH: How to Use IF Logic Explained](https://srinimf.com/2018/05/25/cobol-real-time-example-to-write-if-logic-in-search-statement/)DateMay 25, 2018
- [5 Top Limitations of COBOL SEARCH ALL](https://srinimf.com/2016/01/07/5-top-limitations-of-cobol-search-all/)DateJanuary 7, 2016
- [5 COBOL Frequently Asked Interview Questions](https://srinimf.com/2014/08/07/5-best-cobol-questions-and-solutions/)DateAugust 7, 2014

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2016%2F01%2F06%2Fbest-example-for-cobol-search-and-when-condition%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2016%2F01%2Fcobol-search.jpg%3Ffit%3D1200%252C790%26ssl%3D1&description=Best%20Example%20for%20COBOL%20SEARCH%20and%20WHEN%20Condition)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2016/01/06/best-example-for-cobol-search-and-when-condition/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Best%20Example%20for%20COBOL%20SEARCH%20and%20WHEN%20Condition&body=https%3A%2F%2Fsrinimf.com%2F2016%2F01%2F06%2Fbest-example-for-cobol-search-and-when-condition%2F&share=email)
-

### Like this:

Like Loading…
