Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言编程中的vs?_C_Loops_For Loop_While Loop - Fatal编程技术网

在C语言编程中的vs?

在C语言编程中的vs?,c,loops,for-loop,while-loop,C,Loops,For Loop,While Loop,C中有三个循环:for,while,以及do while。他们之间有什么区别 例如,几乎所有while语句都可以被For语句替换,对吗?那么,使用while有什么好处呢?为了可读性while循环总是首先评估条件 while (condition) { //gets executed after condition is checked } do/while循环将始终执行 首先是do{}块中的代码 然后评估情况 do { //gets executed at least once } w

C中有三个循环:
for
while
,以及
do while
。他们之间有什么区别


例如,几乎所有
while
语句都可以被
For
语句替换,对吗?那么,使用
while
有什么好处呢?

为了可读性while循环总是首先评估条件

while (condition) {
  //gets executed after condition is checked
}
do/while循环将始终执行 首先是
do{}
块中的代码 然后评估情况

do {
  //gets executed at least once
} while (condition); 
循环的允许您在一行中启动计数器变量、检查条件和增加计数器的方法

for (int x = 0; x < 100; x++) {
   //executed until x >= 100
}
要编写为while循环,必须执行以下操作:

int count = 0;
while (count < 100) {
  //do stuff
  count++;
}
继续将终止当前迭代并继续下一个迭代

//will run "do stuff" until x >= 100 except for when x = 2
for (int x = 0; x < 100; x++) {
  if (x == 2) {
    continue;
  }
  //do stuff
}
//将运行“do stuff”直到x>=100,x=2时除外
对于(int x=0;x<100;x++){
如果(x==2){
继续;
}
//做事
}

请注意,在for循环中,
continue
对(part1;part2;part3)的
part3
表达式求值;相反,在while循环中,它只是跳转以重新计算循环条件。

如果您希望在条件为真时执行循环,而不是在一定次数的迭代中执行循环,则其他人更容易理解:

do {
  //gets executed at least once
} while (condition); 
while (cond_true)
而不是像这样:

for (; cond_true ; )

它们都是可互换的;您可以选择一种类型,并且永远只使用它,但通常一种类型对于给定的任务更方便。这就像说“为什么要切换,你可以只使用一堆if语句”--没错,但是如果检查一个变量是否有一组值是一种常见的模式,那么如果有一种语言功能可以做到这一点,那么它就很方便,也很容易阅读

A
做。。。而
是在每次都必须检查某个条件时使用的构造(除了一些类似索引的构造,请参见上文)。它们在第一次执行条件检查的时间上有所不同


您可以使用任何一种构造,但它们都有各自的优点和缺点,具体取决于您的用例。

它们所做的工作都是相同的。你可以用它们中的任何一个做同样的事情。但是在可读性、可用性、方便性等方面,它们有所不同。

while和do-while的区别在于while检查循环条件,如果这是真的,则执行主体并再次检查条件。do while在执行主体后检查条件,因此do while至少执行一次主体


当然,您可以将while循环编写为do-while和vv,但这通常需要一些代码重复。

记住,for
循环本质上是一个奇特的
while
循环它们是一样的。

while <some condition is true> {
   // do some stuff
   // possibly do something to change the condition
}


for ( some var, <some condition is true>; increment var ) {

}

您应该使用这样一个循环,它最完全地符合您的需求。 例如:

int x = 1;
while( x != 10 ) {
  if ( some condition )
     x = 10;
  else 
     x += 5;
}
for(int i = 0; i < 10; i++)
{
    print(i);
}

//or

int i = 0;
while(i < 10)
{
    print(i);
    i++;
}
for(int i=0;i<10;i++)
{
印刷品(一);
}
//或
int i=0;
而(i<10)
{
印刷品(一);
i++;
}
显然,在这种情况下,“for”比“while”看起来更好。 当必须在检查循环条件之前完成某些操作时,应使用“do while”


对不起,我的英语不好。

while的一个特点是,while之后需要一个分号来完成。在宏定义中,它通常用于在限制宏影响的同时只执行一次多个语句。如果宏定义为块,则可能会发生某些解析错误

For循环(至少考虑到C99)优于while循环,因为它们限制了递增变量的范围


当条件依赖于某些输入时,Do while循环非常有用。它们是三种循环类型中最不常用的。

介于for和while之间:
while
不需要初始化或更新语句,因此它看起来更好、更优雅
的code>for可能缺少语句,一条、两条或全部语句,因此,如果在循环之前需要初始化、循环条件和“更新”,这是最灵活和最明显的。如果只需要循环条件(在循环开始时测试),则
while
更优雅


在for/while和do-while之间:在
do-while
中,在循环结束时评估条件。如果循环必须至少执行一次,则更为方便。

WHILE更为灵活。FOR在其适用的情况下更为简洁

FOR对于具有某种计数器的循环非常有用,例如

for (int n=0; n<max; ++n)
等等

但是,当“下一次通过循环”逻辑变得更加复杂时,会出现故障。考虑:

initializeList();
while (listHasMoreElements())
{
  n=getCurrentElement();
  int status=processElement(n);
  if (status>0)
  {
    skipElements(status);
    advanceElementPointer();
  }
  else
  {
    n=-status;
    findElement(n);
  }
}
也就是说,如果根据处理过程中遇到的条件,前进的过程可能不同,则FOR语句是不切实际的。是的,有时您可以使用足够复杂的表达式,使用三元?:运算符等,但这通常会使代码的可读性降低,而不是提高


实际上,我的大多数循环要么是通过数组或某种结构单步执行的,在这种情况下,我使用FOR循环;或者从数据库中读取文件或结果集,在这种情况下,我使用WHILE循环(“WHILE(!eof())”或类似的东西)。

它们几乎相同,除了
do WHILE
循环。当您有一个
计数器
类型的变量时,
for
循环是好的。这很明显<代码>while
循环在检查标志的情况下有意义,如下所示:

while (!done) {
   if (some condtion) 
      done = true;
}

我所看到的
/
对于
循环的一个常见误解是它们的效率不同<代码>While
循环和
for
循环
initializeList();
while (listHasMoreElements())
{
  n=getCurrentElement();
  int status=processElement(n);
  if (status>0)
  {
    skipElements(status);
    advanceElementPointer();
  }
  else
  {
    n=-status;
    findElement(n);
  }
}
while (!done) {
   if (some condtion) 
      done = true;
}
int main(int argc, char* argv[])
{
    int i;
    char x[100];

    // "FOR" LOOP:
    for (i=0; i<100; i++ )
    {
        x[i] = 0;
    }

    // "WHILE" LOOP:
    i = 0;
    while (i<100 )
    {
        x[i++] = 0;
    }

    // "DO-WHILE" LOOP:
    i = 0;
    do
    {
        x[i++] = 0;
    }
    while (i<100);

    return 0;
}
    010013C8  mov         dword ptr [ebp-0Ch],0
    010013CF  jmp         wmain+3Ah (10013DAh)

  for (i=0; i<100; i++ )
  {
      x[i] = 0;
    010013D1  mov         eax,dword ptr [ebp-0Ch]  <<< UPDATE i
    010013D4  add         eax,1
    010013D7  mov         dword ptr [ebp-0Ch],eax
    010013DA  cmp         dword ptr [ebp-0Ch],64h  <<< TEST
    010013DE  jge         wmain+4Ah (10013EAh)     <<< COND JUMP
    010013E0  mov         eax,dword ptr [ebp-0Ch]  <<< DO THE JOB..
    010013E3  mov         byte ptr [ebp+eax-78h],0
    010013E8  jmp         wmain+31h (10013D1h)     <<< UNCOND JUMP
  }
  i = 0;
  010013EA  mov         dword ptr [ebp-0Ch],0
  while (i<100 )
  {
      x[i++] = 0;
    010013F1  cmp         dword ptr [ebp-0Ch],64h   <<< TEST
    010013F5  jge         wmain+6Ah (100140Ah)      <<< COND JUMP
    010013F7  mov         eax,dword ptr [ebp-0Ch]   <<< DO THE JOB..
    010013FA  mov         byte ptr [ebp+eax-78h],0
    010013FF  mov         ecx,dword ptr [ebp-0Ch]   <<< UPDATE i
    01001402  add         ecx,1
    01001405  mov         dword ptr [ebp-0Ch],ecx
    01001408  jmp         wmain+51h (10013F1h)      <<< UNCOND JUMP
  }
i = 0;
.  0100140A  mov         dword ptr [ebp-0Ch],0
  do
  {
      x[i++] = 0;
    01001411  mov         eax,dword ptr [ebp-0Ch]   <<< DO THE JOB..
    01001414  mov         byte ptr [ebp+eax-78h],0
    01001419  mov         ecx,dword ptr [ebp-0Ch]   <<< UPDATE i
    0100141C  add         ecx,1
    0100141F  mov         dword ptr [ebp-0Ch],ecx
    01001422  cmp         dword ptr [ebp-0Ch],64h   <<< TEST
    01001426  jl          wmain+71h (1001411h)      <<< COND JUMP
  }
  while (i<100);
static int intStartWith = 100;
while my money is greater than 1 bucks 
  select chocolate
  pay 1 bucks to the shopkeeper
  money = money - 1
end
#include<stdio.h>
int main(){
  int money = 5;

  while( money >= 1){   
    printf("inside the shopk and selecting chocolate\n");
    printf("after selecting chocolate paying 1 bucks\n");
    money = money - 1 ;  
    printf("my remaining moeny = %d\n", money);
    printf("\n\n");
  }

  printf("dont have money cant go inside the shop, money = %d",  money);

  return 0;
} 
while( codition ){ // condition will always true ....infinite loop
  statement(s)
}
for my money is greater than equal to 1 bucks   0      money >= 1
  select chocolate
  pay 1 bucks to the shopkeeper
  money = money - 1  1-1 => 0
end
#include<stdio.h>
int main(){

  int money = 5;
  for( ; money >= 1; ){     0>=1  false 
    printf("select chocolate \n");
    printf("paying 1 bucks to the shopkeeper\n");
    money = money - 1;    1-1 = 0
    printf(" remaining money =%d\n", money);
    printf("\n\n");
  }

  return 0;
}