Q:

C#.Net find output programs (Loops) | set 1

0

Find the output of C#.Net programs | Loops | Set 1: Enhance the knowledge of C#.Net Loops concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int i = 0;

            while (i < typeof(int).ToString().Length)
            {
                Console.WriteLine("Hello");
                i++;
            }

        }
    }
}

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int i = 0;

            Console.WriteLine(typeof(i).ToString());
            while (i < typeof(int).ToString().Length)
            {
                Console.WriteLine("Hello");
                i++;
            }
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            byte i = 250;

            while (i < 256)
            {
                Console.WriteLine("Hello");
                i++;
            }
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int LOOP = 0;
            
            while (false==false)
            {
                if (++LOOP == 5)
                    break;
                Console.WriteLine("Hello");
            }
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int LOOP = 0;
            
            while (1)
            {
                if (++LOOP == 5)
                    break;
                Console.WriteLine("Hello");
            }
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Press any key to continue . . .

Explanation:

In the above program, we created a variable i initialized with 0.

while (i < typeof(int).ToString().Length)

The typeof(int) operator will return "System.Int32", then we converted returned output into the string and get length using "Length" property that will be 12. Then the condition will be like this:

while(i<12)

That's why "Hello" will print 12 times on the console screen.

Answer  2:

Output:

main.cs(12,38): error CS0246: The type or namespace name `i' could not be found. 
Are you missing an assembly reference?

Explanation:

The above program will generate a syntax error. Because we cannot get system type using typeof() operator in C#.

Answer  3:

Output:

"Hello" will be printed infinite times.

Explanation:

The above program will print "Hello" infinite times because the byte variable can store 255 as a maximum value. But here we checked condition i<256, it means the loop will execute until variable i become 256, but after 255, the next number will not 256, it will be 0, that's why loop will never terminate and print "Hello" infinite times.

Answer  4:

Output:

Hello
Hello
Hello
Hello
Press any key to continue . . .

Explanation:

The above program will print "Hello" 4 times on the console screen. Here, we created a variable LOOP initialized with 0.

while (false==false)

In the above-given loop condition, we checked false==false then the condition will always true, but we used a break statement to terminate the loop. We increased the value of the variable LOOP using pre-increment operator when the value of the variable LOOP reaches to 5, then the loop will be terminated.

Answer  5:

Output:

main.cs(12,20): error CS0031: Constant value `1' cannot be converted to a `bool'

Explanation:

The above program will generate syntax error because we can use only Boolean values that are true or false in the loop condition in C#.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now