c语言for循环中暂停_C中的for循环–第2部分

c语言for循环中暂停

Read:  for loop in C – Part 1

Before proceeding to this tutorial I am assuming that you are familiar with for loop. If you didn’t read my last tutorial then I strongly recommend you to read that. Anyways till now we have learnt about the basic use of for loop and its execution.

Today I will tell you about the basic variations while using for loop. All three things initialization, condition and loop counter increment are resides within the syntax of for loop. This is one of the key advantages of using it. Giving below are some variations while using it.

1. It is not compulsory to initialize the for loop with the syntax. I can also initialise it before.


Consider the above example carefully. You will notice I have not initialized variable i within for loop. But I have left the space for it. I have also given semi-colon for it. It is compulsory even if you do not want to initialize a variable within for loop.

2. The same condition is also true if I do not mention the third part in it. Consider the below example.

int i;
for(i=1;i<10;)
{
 printf("This is working");
 i++;
}


Notice that I have not increment the loop counter within for loop. Instead of it I am incrementing it inside the body of the loop. Again it is compulsory to give semicolon.

3. What if I drop both the parts? Consider the below example carefully.


Will it work or not? Well its perfectly fine. So from the above three points we conclude that the initialization and increment part is completely optional in for loop.

4. Use of post increment/decrement operator within for loop.

int i=1;
for(;i++<10;)
{
 printf("This is working");
}


Checkout the execution of the above program stepwise.

  • i will initialized with value 1
  • Condition is checked within for loop. It turns true.
  • After that i will increment to 2.
  • Now printf() function will display the message “This is working”.
  • Now again condition is checked with value i=2. It will turn true.
  • After that i will increment to 3.

Note: Post increment operator is used with i. So first the condition will be checked. After that i will increment to 2. The reverse is also true for pre-increment/decrement operator.

5. Use of logical operators in for loop.


You can check the loop for multiple conditions by using logical operators in it.

6. In the last tutorial I have told you that we can replace any valid expressions in these three parts. One good example of that is given below.

Shortest C program to print 10 numbers

#include<stdio.h>
 
void main()
{
 int i;
 for(i=1;i<11;printf("%dn",i++));
}

Consider the above example carefully. I have inserted the printf() function in it. And I have also given a post increment operator with i. After the for loop I have given a semicolon. Because I don’t want to execute a single statement in loop after that.

Loops are important for this C programming tutorial. So I recommend you to try your hands on for loop. Try to write programs with some variations to obtain on good conclusions.

You can try the below programs for practice.
Write the shortest program to print reverse counting from 10 to 1.

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

c语言for循环中暂停

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值