4D Blog

Home Product Loops, loops and … more loops!

Loops, loops and … more loops!

April 19, 2018

Product

Like many other programming languages, 4D provides the ability to iterate over numbers with a For(…) / End for statement, or over a condition thanks to the While(…)/End while and Repeat/Until(…) keywords. These iterations (or loops) are very useful, but not the most appropriate way to iterate over advanced data types such as collections and entity selections. That’s why the new For each(…)/End for each iterator has been created, to help ease your coding!

Database example

Looping THROUGH collection items

The For each / End for each statement can be used in several cases. One case is to looping through items of a collection. No need to have advance knowledge of how many items belong to the collection before entering the loop. At each iteration, the variable passed as first parameter (item) will be filled with the current item of the collection (col).

col:=New collection("charlie";"delta";"uniform";"foxtrot";"quebec")
$count:=0
For each (item;col) 
 If (Length(item)>5)
  $count:=$count+1
 End if
End for each
ALERT(String($count)+" words found!")

Looping through entities

The second case is also the newest one: looping through entities of an entity selection. Entities and entity selections are part of a new concept in 4D, described in this blog post. The main thing you need to know is that entities are similar to records, and entity selections can be compared to traditional selections, but they’re both declared as objects!

Again, no need for prior knowledge of how many entities belong to the entity selection before entering the loop. At each iteration, the entity variable passed as first parameter will be automatically filled with the current entity of the entity selection.

UK_emps:=ds.Employees.query("country='UK'")
// Browse the entity selection
For each (emp;UK_emps)
  emp.salary:=emp.salary*1.03  // raise the salary
  emp.save()  // save the result
End for each

Looping Through object properties

The For each / End for each statement can also be used in another case: looping through object properties. For example, it can be very useful in case you want to write some generic code, without knowing the object property names in advance.

Again, no need to know beforehand how many properties an object has before entering the loop! All of its properties’ names will be parsed one by one! At each iteration, the variable passed as first parameter (property in the example below) will be filled with the attribute name of the object ($contact in the example below).

For each ($property;$contact)
 If (Value type($contact[$property])=Is text)
  //Change to uppercase each attribute which is a text
  $contact[$property]:=Uppercase($contact[$property])
 End if
End for each

Discuss

Tags Collections, Objects, Programming, v17

Latest related posts

  • December 3, 2025

    Give AI to a 30 years old 4D application

  • November 28, 2025

    ORDA – Handle an event-driven logic during database operations

  • November 27, 2025

    ORDA – Permissions – Restrict / allow web access to the resources in one click

Roland Lannuzel
Roland Lannuzel
• Product Owner & 4D Expert • After studying electronics, Roland went into industrial IT as a developer and consultant, building solutions for customers with a variety of databases and technologies. In the late 80’s he fell in love with 4D and has used it in writing business applications that include accounting, billing and email systems.Eventually joining the company in 1997, Roland’s valuable contributions include designing specifications, testing tools, demos as well as training and speaking to the 4D community at many conferences. He continues to actively shape the future of 4D by defining new features and database development tools.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • AI
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy