人生有度,好在适度
C programming Looping Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on various looping statements like while, do dhile, for nested looping etc.
C编程循环能力问题和解答:在本节中,您将在各种循环语句(如while,do dhile,嵌套循环等)中找到C能力倾向问题和解答。
#include < stdio.h >
void main()
{ unsigned char var=0;
for(var=0;var<=255;var++)
{
printf("%d ",var);
}
}
0 1 2 ... 255
The range of unsigned char is 0 to 255 and when the value of var will cross over 255, value will be 0 and again same process will happen.
0 1 2 ... 255
无符号char的范围是0到255,并且当var的值超过255时,value将是0,并且再次发生相同的过程。
#include <stdio.h>
void main()
{
char cnt=0;
for(;cnt++;printf("%d",cnt)) ;
printf("%d",cnt);
}
1
Before entering into the for loop the CHECK CONDITION is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).
1个
在进入for循环之前,将对“ 检查条件 ”进行“评估”。 在这里,它的值为0(假)并退出循环,并且i递增(请注意for循环后的分号)。
#include <stdio.h>
void main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i==5)
goto print;
i++;
}
}
fun()
{
print:
printf("includehelp.com");
}
Compiler error: Undefined label ‘print’ in function main.
Labels have functions scope, in other words the scope of the labels is limited to functions. The label ‘print’ is available in function fun() Hence it is not visible in function main.
编译器错误:函数main中的未定义标签'print'。
标签具有功能范围 ,换句话说,标签的范围仅限于功能。 标签'print'在fun()函数中可用,因此在main函数中不可见。
#include < stdio.h >
int main()
{
int tally=0;
for(;;)
{
if(tally==10)
break;
printf("%d ",++tally);
}
return 0;
}
1 2 3 4 5 6 7 8 9 10
for(; ;) it is possible in c, there is no need to place condition with in the for(), you can place condition with in the body of the loop.
1 2 3 4 5 6 7 8 9 10
for(;;)可能在c中,不需要在for()中放置条件,您可以在循环体中放置条件。
#include <stdio.h>
void main()
{
int tally;
for(tally=0;tally<10;++tally)
{
printf("#");
if(tally>6)
continue;
printf("%d",tally);
}
}
#0#1#2#3#4#5#6###
#0#1#2#3#4#5#6 ###
#include <stdio.h>
void main()
{
int i,j,charVal='A';
for(i=5;i>=1;i--)
{
for(j=0;j< i;j++)
printf("%c ",(charVal+j));
printf("\n");
}
}
#include <stdio.h>
void main()
{
int i,j,charVal='A';
for(i=5;i>=1;i--)
{
for(j=0;j< i;j++)
printf("%c ",(charVal+j));
printf("\n");
}
}
-
A B C D A B C D A B C D A B C D
-
A B C D E A B C D A B C A B A
#include <stdio.h>
void main()
{
int cnt=1;
do
{
printf("%d,",cnt);
cnt+=1;
}while(cnt>=10);
printf("\nAfter loop cnt=%d",cnt);
printf("\n");
}
1,
After loop cnt=2
do while is an exit controlled loop, here loop body executed first, then condition will be checked.
1,
循环后cnt = 2
do while是一个退出控制的循环,此处首先执行循环主体,然后将检查条件。
#include <stdio.h>
void main()
{
int cnt=1;
while(cnt>=10)
{
printf("%d,",cnt);
cnt+=1;
}
printf("\nAfter loop cnt=%d",cnt);
printf("\n");
}
1,
After loop cnt=1
Here condition will be checked first.
#include <stdio.h>
#define TRUE 1
int main()
{
int loop=10;
while(printf("Hello ") && loop--);
}
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (10 times)
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (11 times)
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (11 times)
你好你好你好你好你好你好你好你好你好你好你好你好(11次)
#include <stdio.h>
int main()
{
int i=1;
while(i<=10 && 1++)
printf("Hello");
}
Error: lvalue required as increment operand.
翻译自: https://www.includehelp.com/c-programs/c-looping-aptitude-questions-and-answers.aspx
人生有度,好在适度