


Does a for loop execute the loop body statement first and then evaluate the expression?
No, the for loop first judges the expression and then executes the loop body statement. The general form is "for (expression 1; expression 2; expression 3) {loop body}"; first execute "expression 1"; then execute "expression 2", if its value is true (non-0), Then execute the loop body, otherwise end the loop; execute "expression 3" after executing the loop body.
The general form of a for loop is:
for(表达式1; 表达式2; 表达式3){ 语句块 }
Its running process is:
1) Execute "Expression 1" first.
2) Execute "expression 2" again. If its value is true (non-0), execute the loop body, otherwise end the loop.
3) Execute "expression 3" after executing the loop body.
4) Repeat steps 2) and 3) until the value of "expression 2" is false, then end the loop.
In the above steps, 2) and 3) are a loop and will be executed repeatedly. The main function of the for statement is to continuously execute steps 2) and 3).
"Expression 1" is only executed during the first loop and will not be executed again in the future. This can be considered an initialization statement. "Expression 2" is generally a relational expression, which determines whether to continue the next loop, which is called the "loop condition". "Expression 3" is often an expression with an increment or decrement operation, so that the loop condition gradually becomes "not true".
The execution process of the for loop can be represented by the following figure:
Let’s analyze the “calculation of adding from 1 to 100 and" code:
#include <stdio.h> int main(){ int i, sum=0; for(i=1; i<=100; i++){ sum+=i; } printf("%d\n",sum); return 0; }
Running result:
5050
Code analysis:
1) When executing the for statement, first assign an initial value to i 1. Determine whether i
2) During the second loop, the value of i is 2, i
3) Repeat step 2) until the 101st loop. At this time, the value of i is 101, and i
From this we can summarize the general form of the for loop:
for(初始化语句; 循环条件; 自增或自减){ 语句块 }
The three expressions in the for loop
" in the for loop "Expression 1 (initialization condition)", "Expression 2 (loop condition)" and "Expression 3 (self-increment or self-decrement)" are all optional and can be omitted (but the semicolon;
must be retained).
1) Modify the code for "sum from 1 to 100" and omit "expression 1 (initialization condition)":
int i = 1, sum = 0; for( ; i<=100; i++){ sum+=i; }
You can see that i=1
Moved outside the for loop.
2) If "expression 2 (loop condition)" is omitted, it will become an infinite loop if no other processing is done. For example:
for(i=1; ; i++) sum=sum+i;
is equivalent to:
i=1; while(1){ sum=sum+i; i++; }
The so-called infinite loop means that the loop condition is always true, and the loop will continue and never end. Infinite loops are very harmful to the program and must be avoided.
3) If "expression 3 (self-increment or self-decrement)" is omitted, the variables in "expression 2 (loop condition)" will not be modified. In this case, the modified variables can be added to the loop body statement. For example:
for( i=1; i<=100; ){ sum=sum+i; i++; }
4) "Expression 1 (initialization statement)" and "Expression 3 (self-increment or self-decrement)" are omitted. For example:
for( ; i<=100 ; ){ sum=sum+i; i++; }
is equivalent to:
while(i<=100){ sum=sum+i; i++; }
5) 3 expressions can be omitted at the same time. For example:
for( ; ; ) 语句
is equivalent to:
while(1) 语句
6) "Expression 1" can be an initialization statement or other statements. For example:
for( sum=0; i<=100; i++ ) sum=sum+i;
7) "Expression 1" and "Expression 3" can be a simple expression or a comma expression.
for( sum=0,i=1; i<=100; i++ ) sum=sum+i;
or:
for( i=0,j=100; i<=100; i++,j-- ) k=i+j;
8) "Expression 2" is generally a relational expression or a logical expression, but it can also be a numerical value or character. As long as its value is non-zero, the loop body will be executed. . For example:
for( i=0; (c=getchar())!='\n'; i+=c );
Another example:
for( ; (c=getchar())!='\n' ; ) printf("%c",c);
Related recommendations: "c Language Tutorial"
The above is the detailed content of Does a for loop execute the loop body statement first and then evaluate the expression?. For more information, please follow other related articles on the PHP Chinese website!

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software