Question

What is the output of the following program? [ Explain the working of the while loop...

What is the output of the following program? [
Explain the working of the while loop in this program as per your
understanding.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 100;
while (true) {
Console.WriteLine(number);
number+=100;
if (number <= 1000)
continue;
else
break;
}
Console.ReadLine();
}
}
}

Homework Answers

Answer #1

In the given program, the variable number is declared and initialized with value 100. After that, a while loop structure is started which is a forever loop(only stops with break or any other exit condition). Inside the loop, the following operations takes place in order:

  • Display the value of number using Console.WriteLine() method.
  • Increments the value of number with 100.
  • Checks if the value of number is less than or equal to 1000. If yes, it breaks and exits from the loop. If not, the whole procedure is repeated and the next value of number is printed.

Console.ReadLine() method is used to pause the execution after displaying all the values of the variable number, that is from 100 to 1000. The executed code and the corresponding output are as follows:

using System.IO;
using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 100;
while (true) {
    Console.WriteLine(number);
    number+=100;
    if (number <= 1000)
    continue;
    else
    break;
    }
  Console.ReadLine();
  }
 }
}

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Identify the errors in the following C# program. Write the correct program. Explain the errors. class...
Identify the errors in the following C# program. Write the correct program. Explain the errors. class Program { static Main(string[] args) { int x = 10 Console.WriteLine("{0}" + x); Console.ReadLine(); }
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment06 {    class Program    {...
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment06 {    class Program    {        static void Main(string[] args)        {            int age = 10;            if (age >= 16)            {                Console.WriteLine("Error;Since you are " + age + " years old, you are legally unable to obtain a license.");            }            Console.Write("Hit any key to close"); Console.ReadKey(false);       ...
int a =12; a += 3; System.out.println(a); // What is the output of the above program?-----...
int a =12; a += 3; System.out.println(a); // What is the output of the above program?----- --------------- the Java keyword "new" accomplishes what? starts a new program gets input creates an object in memory refresh variable values ------------------------ Every Java Class MUST contain the main method: 'public static void main(String[]args)'   true/false
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT