Talking about the for loop and foreach loop in C,

Source: Internet
Author: User

Talking about the for loop and foreach loop in C,

In fact, the for loop and the foreach loop can be considered as subordinate, that is, the foreach loop can be converted into a for loop, but the for loop may not be converted into a foreach loop.

The following describes two types of loops:

1. for Loop

Code Format:

For (expression 1; loop condition; expression 2)

{

Loop body

}

Code Description:

First, run expression 1;

Then judge whether the condition is true. If it is true, execute the loop body and then run expression 2;

Then judge the cyclic condition ...... The loop ends only when the cyclic condition is false.

Note:

Expression 1: Any code can be executed only once;

Expression 2: Any code can be executed after the loop body is executed.

2. foreach Loop

Code Format:

Foreach (in array or set of data type variables)
{
Loop body

}

Code Description:

Extract the data of each item from an array or set in sequence, and assign the data to the loop variable. After each value is assigned, the loop body runs once.

Note:

Foreach loops can only be used to traverse arrays and collections;

A foreach loop is also called a read-only loop. in the loop body, the set or array cannot be changed;

The data type must be the same as the data type of each item in an array or set.

 

However, what are the differences and advantages between a foreach loop and a for loop? The following is a brief summary:

Foreach loop for Loop

Can only be used for traversal; can be used for any form of repeated behavior;

You cannot change the cycle target. You can perform any operations in the loop body;

Fast traversal and high execution efficiency. The traversal speed is slow and the execution efficiency is low.

Conclusion: If you want to traverse a set or array, and you only need to read but not change it during the traversal process, it is most appropriate to use a foreach loop. Otherwise, select another loop as needed.

I don't understand many things for beginners. Thank you for your help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.