首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

复习的时候遇到的有关问题

2012-03-04 
复习的时候遇到的问题while(){if()continue语句}问题一:continue不是只能在循环语句里么?上面难道不是在

复习的时候遇到的问题
while()
{if()
  continue;  
 语句;
}
问题一:continue不是只能在循环语句里么?上面难道不是在if语句中?
还有continue跳过本次循环,跳过的为什么不是if语句?书上说continue应该跳出嵌套在里面的循环而跳到外面

//////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <conio.h>
int main(void)
{float length,width;
 printf("Enter the length of the rectangle:\n");
 while(scanf("%f",&length)==1)
 {
  printf("Length=%0.2f:\n",length);
  printf("Enter its width:\n");
  if(scanf("%f",&width)!=1) //问题二:在上一语句printf以后,不管输入是否正确都要执行if中的条件判断??
  break;
  printf("Width=%0.2f:\n",width);
  printf("Area=%0.2f:\n",length*width);
  printf("Enter the length of the rectangle:\n");
 }
 printf("Done.\n");
 getch();
 return 0;
}

/////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main(void)
{char ch;
 printf("111111111111.\n");
 while((ch=getchar())!='#')
 {if('\n'==ch) 
  continue;
  if(islower(ch))
  switch(ch)
  {
  case 'a':
  printf("argali,a wild sheep of Asia\n");
  break;
  default:
  printf("That's a stumper!\n");
  }
  else
  printf("nnnnnnnnnnnnn.\n");
  while(getchar()!='\n') //问题三:这里的while作用我实在不明白,去掉我执行了,没变化
  continue;
  printf("2222222222222.\n");
 }
 printf("BYE!\n");
 

 getch();
 return 0;
}

[解决办法]
while(getchar()!='\n') //问题三:这里的while作用我实在不明白,去掉我执行了,没变化 
continue; 
-------------------------
知道输入是换行才会结束循环
[解决办法]

探讨
引用楼主 yunhaiC 的帖子:
while()
{if()
continue;
语句;
}
问题一:continue不是只能在循环语句里么?上面难道不是在if语句中?
还有continue跳过本次循环,跳过的为什么不是if语句?书上说continue应该跳出嵌套在里面的循环而跳到外面

[解决办法]
语法上continue,break作用于与它最近的 while,for语句.可以看一下编译原理的语法分析.
MSDN:
The continue statement passes control to the next iteration of the do, for, or while statement in which it appears, bypassing any remaining statements(当然包括if) in the do, for, or while statement body. A typical use of the continue statement is to return to the start of a loop from within a deeply nested loop.

热点排行