C

您所在的位置:网站首页 loop灯具 C

C

2024-06-06 04:51| 来源: 网络整理| 查看: 265

Loops in programming are used to repeat a block of code until the specified condition is met. A loop statement allows programmers to execute a statement or group of statements multiple times without repetition of code.

C

// C program to illustrate need of loops #include    int main() {     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");     printf( "Hello World\n");            return 0; } Output Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

There are mainly two types of loops in C Programming:

Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the loop. For Loop and While Loop is Entry-controlled loops. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. do-while Loop is Exit Controlled loop. loops in c

 

Loop Type Description for loop first Initializes, then condition check, then executes the body and at last, the update is done. while loop  first Initializes, then condition checks, and then executes the body, and updating can be inside the body. do-while loop do-while first executes the body and then the condition check is done. for Loop 

for loop in C programming is a  repetition control structure that allows programmers to write a loop that will be executed a specific number of times. for loop enables programmers to perform n number of steps together in a single line.

Syntax:

for (initialize expression; test expression; update expression) { // // body of for loop // }

Example:

for(int i = 0; i < n; ++i) { printf("Body of for loop which will execute till n"); }

In for loop, a loop variable is used to control the loop. Firstly we initialize the loop variable with some value, then check its test condition. If the statement is true then control will move to the body and the body of for loop will be executed. Steps will be repeated till the exit condition becomes true. If the test condition will be false then it will stop.

Initialization Expression: In this expression, we assign a loop variable or loop counter to some value. for example: int i=1; Test Expression: In this expression, test conditions are performed. If the condition evaluates to true then the loop body will be executed and then an update of the loop variable is done. If the test expression becomes false then the control will exit from the loop. for example, i author kamleshjoshi18 Improve Previous Article Using Range in switch Case in C Next Article C for Loop Please Login to comment...


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3