Iteration is the repetition of a statement | My Assignment Tutor

60Chapter 4IterationIteration is the repetition of a statement or block of statements in a program. C++ has threeiteration statements: the while statement, the do..while statement, and the for statement.Iteration statements are also called loops because of their cyclic nature.4.1 THE while STATEMENTThe syntax for the while statement iswhile (condition) statement;where condition is an integral expression and statement is any executable statement. If thevalue of the expression is zero (meaning “false”) then the statement is ignored and programexecution immediately jumps to the next statement that follows the while statement. If the valueof the expression is nonzero (meaning “true”) then the statement is executed repeatedly untilthe expression evaluates to zero. Note that the condition must be enclosed by parentheses.EXAMPLE 4.1 Using a while Loop to Compute a Sum of Consecutive IntegersThis program computes the sum 1 + 2 + 3 + ⋅⋅⋅ + n, for an input integer n:int main(){ int n,i=1;cout > n;long sum=0;while (i


Leave a Reply

Your email address will not be published. Required fields are marked *