A program of while loop to print sum of 1 to n consecutive numbers, where n is given?

A program of while loop to print sum of 1 to n consecutive numbers, where n is given?

A program of while loop to print sum of 1 to n consecutive numbers, where n is given?

The while loop would be more clear after going through the program.

The while loop would be more clear after going through the program.

The while loop would be more clear after going through the program.

. When the conditions become false (zero), the control transfers out of the loop and passes to the next executable statements that follow the loop. Make sure that expression must eventually become

The execution continues as long as the expression remains true . When the conditions become false (zero), the control transfers out of the loop and passes to the next executable statements that follow the loop. Make sure that expression must eventually become false so that the program can terminate.

The execution continues as long as the expression remains true . When the conditions become false (zero), the control transfers out of the loop and passes to the next executable statements that follow the loop. Make sure that expression must eventually become false so that the program can terminate.

When the compiler encounters a while loop in a program, first the expression is evaluated . If it evaluates to

When the compiler encounters a while loop in a program, first the expression is evaluated . If it evaluates to true (i.e. non-zero value ) then the statements contained in the loop are executed.

Where the expression enclosed in parentheses is the condition that controls the loop and may be relational or logical expression. Statement(s) can be a single statement or a block of statements.

Where the expression enclosed in parentheses is the condition that controls the loop and may be relational or logical expression. Statement(s) can be a single statement or a block of statements.

. It is mostly used in those cases where the programmer does not know in advance how many times the loop will be executed.

statements is the most basic looping statements . It lets you execute a set os statements repeatedly as long as condition evaluates to

The while statements is the most basic looping statements . It lets you execute a set os statements repeatedly as long as condition evaluates to true . It is mostly used in those cases where the programmer does not know in advance how many times the loop will be executed.

The while statements is the most basic looping statements . It lets you execute a set os statements repeatedly as long as condition evaluates to true . It is mostly used in those cases where the programmer does not know in advance how many times the loop will be executed.

statement is used where there is necessity to execute a loop once.

statement because in practise it is unusual to encounter a situation where you are sure you want to execute the loop atleast once. thus,

looping statement as the test condition represented using an expression is checked at the end of each iteration and therefore the body of the loop is executed unconditionally for the first time. For this reason,

do-while statement is an e xit controlled looping statement as the test condition represented using an expression is checked at the end of each iteration and therefore the body of the loop is executed unconditionally for the first time. For this reason, do -while is used less frequently than the while statement because in practise it is unusual to encounter a situation where you are sure you want to execute the loop atleast once. thus, do-while statement is used where there is necessity to execute a loop once.

The do-while statement is an e xit controlled looping statement as the test condition represented using an expression is checked at the end of each iteration and therefore the body of the loop is executed unconditionally for the first time. For this reason, do -while is used less frequently than the while statement because in practise it is unusual to encounter a situation where you are sure you want to execute the loop atleast once. thus, do-while statement is used where there is necessity to execute a loop once.

and then control transfers to the next executable statement following the

, the body of the loop is executed again and it keeps on executing until the test condition

, the body of the loop always executes atleast once even the test condition evaluates to

loop evaluates the condition after the execution of the statements in the body of the loop. So in case of

loop where the test condition is evaluated at the beginning of loop, the

The do-while looping statement is somewhat similar to the while loop statements as the loop continues as long as specified condition is true. Unlike the while loop where the test condition is evaluated at the beginning of loop, the do while loop evaluates the condition after the execution of the statements in the body of the loop. So in case of do while , the body of the loop always executes atleast once even the test condition evaluates to false . If the test condition evaluates to true , the body of the loop is executed again and it keeps on executing until the test condition is false and then control transfers to the next executable statement following the do while loop.

The do-while looping statement is somewhat similar to the while loop statements as the loop continues as long as specified condition is true. Unlike the while loop where the test condition is evaluated at the beginning of loop, the do while loop evaluates the condition after the execution of the statements in the body of the loop. So in case of do while , the body of the loop always executes atleast once even the test condition evaluates to false . If the test condition evaluates to true , the body of the loop is executed again and it keeps on executing until the test condition is false and then control transfers to the next executable statement following the do while loop.

The for loop

The for loop is another form of looping statement which is the most versatile, convenient and popular of three loop structures available in C. It is used in those situations when the programmer knows in advance the number of times a statement or block of statements will be executed. In this loop, the loop-control elements are gathered in one place, while in other loops they are scattered over the program and difficult to understand. The for loop allows us to specify three things about a loop in a single time.

The syntax of for loop is,

for(initial_exp ; test_expression; update_exp)

Here, initial_exp is an expression which is used to initialize the loop control variable(s) that is used to control the loop. It is executed once before executing the body of the loop. The test_expression is any relational or logical expression that specifies condition that need to be checked to control the loop control variable. If it evaluates to true , the body of the loop will be executed . If it evaluates to false , the loop terminates and control of execution is transferred to the statement following the for loop.

The update_exp is an expression that is executed at the end of each loop iteration. It is usually causes an increment or decrement of loop control variable(s). These three expression in the for loop are separated by semicolon and inclosed in parentheses.





A program of for loop to calculate the sum of the first 10 natural numbers.





#include<stdio.h>

#include<conio.h>

main()

{

int sum=0,i;

clrscr();

for(i=1;i<=10;i++)

{

sum+=i;

}

printf("

sum of first 10 numbers= %d", sum);

getch();

}









OUTPUT:

sum of first 10 numbers= 55

READ MORE







