c语言中while循环_C中的while循环–第2部分

c语言中while循环

Read previous tutorial  while loop in C – Part 1.

In the last tutorial we have learnt about the while loops. Loops are used very frequently while writing big programs. And in the last tutorial we have learnt only the basics of the loop control structure. In this tutorial I will not write any program. Instead of it I will tell you about some tips and traps of using while loop in C.

If you have write any program using while loop for the first time. Then it is quite possible that you will get many errors. This tutorial mainly focuses to make you confident while writing loop control structure in programs.

Tips and traps of while loop

1. As I told you earlier the general form of while loop is.

initialise loop counter; 
while(condition)
{  do this; 
  and this;  
  increment loop counter; 
}

We can add any valid condition or expression with while keyword. Any expression which evaluates as non zero is said to be true and the expression which evaluates zero is said to be false.

2. We can use logical operators to describe condition. This is perfectly fine for all loops.

while(x<=60) 
while(x>=50&&y<=75)

3. If you want to execute only one statement in while loop then you can also drop the curly braces { }. Default scope of while loop is one statement below the while keyword.

while(x<=20)
i=i+1;

is same as

while(x<=10)
{  
i=i+1; 

}

However if you want to execute multiple statements in while loop then it is compulsory to use curly braces.

4. Remember to increment or decrement the loop counter. Otherwise it will result in an infinite loop. One common mistake is given below.

main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
}
}

The above program will result in an infinite loop because we are not changing the value of loop counter. Condition is always true as value of loop counter remains same and the loop will get executed infinitely.

Correction of above code is given below.

main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
i=i+1;
}
}

5. Never write a semicolon after while keyword. It will result in an infinite loop. A common error code is given below.

main()
{
int i=1;
while(i<=10);
{
printf(“%d”, i);
i=i+1;
}
}

Checkout I have given semicolon after while keyword. So do not make this mistake.

Few more operators

Post Increment/Decrement Operator

This operator is commonly used with loops. You must have noticed that most of the time we use expression i=i+1 to increment the loop counter. To make this a bit easy to write we can use post increment and decrement operator.

So i=i+1 is same as i++ (post increment operator).

and i=i-1 is same as i—(post decrement operator).

Pre Increment/Decrement Operator

This operator is similar to post. But with a small difference. This operator gives priority to incrimination in the function.

i=i+1 is same as ++i (pre increment operator).

i=i-1 is same as –i (pre decrement operator).

Sounds confusing? As both are same.

Well write one program with below function to understand difference between them.

i=1;
j=10;
prinf(“%d %d n”, ++i, i++);
prinf(“%d %d n”,–i, i–);
prinf(“%d %d n”, ++j, j++);
prinf(“%d %d n”,–j, j–);

Compound Assignment Operator

It is also similar to the above operators. The best way to understand them is by syntax.

i=i+5 is same as i+=5.

Or

i=i-5 is same as i-=5.

Or

i=i*6 is same as i*=6.

This operator can be used with arithmetic operators like +, -, *, / and %.

翻译自: https://www.thecrazyprogrammer.com/2015/01/while-loop-in-c-part-2.html

c语言中while循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值