function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhanu joshi 10Bhanu joshi 10 

Doubt on for Loop

I have written following Code
integer count  0;
condition 1:
for(Account a :[Select name from account limit 10]){
count++;
System.debug('Count   '+count);
}

condition 2:
​for(list<Account> a :[Select name from account limit 10]){
count++;
System.debug('Count   '+count);
}

On condition 1 i get output as 10 and for condition 2 i  get output 1.  Why?
Any help will be appricated
Thanks
Salesforce DeveloperSalesforce Developer
Yes that is correct, when you use list of sObjects in  Soql For loops then you get 200 records in one loop. And in your second condition you have a limit of 10 records hence the code inside the loop  is running only once. 

In condition one you are using one sObject record to store data returned by query and hence loop is running for each record returned.
Amit Chaudhary 8Amit Chaudhary 8
Because in condition 1: you are getting 10 record one by one.
but in condition 2: you are getting List of 10 record in one time.
 
Bhanu joshi 10Bhanu joshi 10
Thanks guys for the response..