Question

In: Computer Science

C# language. Answer in a discussion format. What is a loop? When do you use a...

  • C# language. Answer in a discussion format.
  • What is a loop? When do you use a loop versus a selection statement? What are some examples when an infinite loop is appropriate?
  • Describe one of the following three types of loops supported in C#:
    • while loop
    • for loop
    • do loop (or do-while loop)
  • Describe the steps necessary to make a while loop end correctly:
  • Explain the difference between incrementing and decrementing loop control variables.
  • Explain the benefits of using both pretest loops and posttest loops.
  • What are some examples when an infinite loop is appropriate?

Solutions

Expert Solution

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


Related Solutions

C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
C# Answer in discussion format Why is an array such a useful tool? What is a...
C# Answer in discussion format Why is an array such a useful tool? What is a jagged array? Ask your students to describe some circumstances in which jagged arrays might be useful. the System.Array class contains a variety of useful, built-in methods, describe one of them and how you would use it. Describe how to resize an array in C# using the new operator. How would you declare an array if you do not know in advance how many elements...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
in c++ >>When is beneficial to use entry condition loop (for loop) and when is it...
in c++ >>When is beneficial to use entry condition loop (for loop) and when is it beneficial to use exit condition loop (while loop). Give an example for each loops.
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs in the data base are one-way. Question: Write a program that can answer if there is a path between any to vertices. For the vertex pairs use this as your input example: AL...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs are two-way. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of vertices traversed from X to Y if there is a path....
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT