Q: what is loop? Ans: A statement or set of statements that is executed repeatedly is known as loop, or iteration. There are three types of loops available in C language.
1. For Loop
i) Counter loop
2. Conditional Loop
ii) While loop
iii) do-while loop
Q: What is “while” loop? Ans: while loop is the simplest loop of C language. This loop executes one or more statements while the given condition remains true. Syntax: While(condition) statement;
The syntax of compound statement as :
While (condition)
{
Statement 1;
Statement 2;
:
Statement N;
}
ü write a program that displays “Pakistan” for five times using while loop.(int n; n=1; while(n<=5); n++; and \n).
ü write a program that displays counting from 1 to 10.
(int n; while(condition) n++ ü write a program that displays first five numbers and their sum using while loop.(int c,sum; sum=0 while(condition) sum=sum+c;)
ü write a program that displays first 5 numbers with their squares using while loop
.(n=1;, while(n<=5), printf(“%d %d\n”,n,n*n);, n++ ü write a program that inputs a number from the user and displays the factorial of that number using while loop.
ü What is do-while loop? The do-while is an iterative control in c language. This loop execute one or more statements while the given condition is true. In this loop, the condition comes after the body of loop. The loop is important in a situation where a statement must be executed at least once.
Syntax: do
{
Statement 1;
Statement 2;
:
:
Statement N;
}
While (condition);
ü What is difference b/w while and do-while loops?
While loop:Y in while loop, conditions comes before the body of the loop. Y if condition is false in the beginning, while loop is never executed. Y the semicolon is not used after the condition.
do-while loop: Y in do-while loop, condition comes after the body of the loop. Y do-while is executed at least once even if conditions is false in the beginning. Y semicolon is used after the condition.
Program: write a program that displays first ten odd numbers using
do-while loop.