In: Computer Science
1 ans: loops are used to iterate over the sequence of instructiosn
2 ans:loops are used iterate instructions more times but selection statements works only once
exaple
for(int i=1;i<=10;i++){
Console.WriteLine(i);
}
Here we get numbers 1 to 10 numbers
coming to selection statements
int num=4;
if(num%2==0){
Console.WriteLine("Even");
}else{
Console.WriteLine("Odd");
}
here output will be Even because 4%2==0 then to conditions checking we can use selection statements which runs only once if it present in the loop then it will run n number of times based on the outer loop
3 ans:
infinite loop
while(true){
}
it can run infinite times
another one
int check=0;
do{
}while(check==0);
it can also run infinite time
4 ans:
for loop
for(int i=0;i<10;i++){
}
here for loop can take three values first one where the value start from and second one is where the value is end and 3rd one is step if i++ which mean each time it increment by 1.for loop is used if we know terminal value
while loop
here if we don't know where to stop but it can be stop but some condition otherwise it will be infinite times
int i=0;
while(i<10){
i++;
}
here intially i=0 then in while loop it checks 0<10 true then it wil go to inner the loop then i++ which increment by 1 then i is now 1 then again while checks 1<10 true then again goes until it fails the condition if i =10 then 10<10 which is not true then loop will break
do while loop
it was used for if we run the loop atleast once we can use do while loop
int i=0
do{
}while(i!=10);
5 ans:
increment or decrement will be based on the user
int i=10
while(i>0)
i--;
}
it was the loop works based on the decrement until it reaches to zero
6 ans:
pretest loops are nothing but when enter into loop it must always check the condition which are for and while loop are the examples and posttest loop is nothing but do while loop without any conditon intially it runs then after it chakecs the condition
for loop is used if we know terminal or end position
and while loop is used if we want run n number of times until user enter valid data
do while loop is used for if we run some statement once then only loop will works
7 ans:
infinite loops are for it can run infinite times
examples
while(true){
}
or
while(10==10){
}
or while(1==1){
}
like we can do n number of ways to represent infinite loops