© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
35 replies
Cheetah

How to make If statement in loop only run once

i have three if statements and when i write a number it runs infinitetly even tho its if
using System;
using System.Runtime.InteropServices;

namespace Guessing_game
{
    internal class Program
    {
        static void Main(string[] args)
        {
         Random rnd = new Random();
        int num = rnd.Next(1, 100);
        Console.WriteLine(num);
        Console.WriteLine("Guess the number");
        string Guess = Console.ReadLine();

        int IntGuess = int.Parse(Guess);

            Console.WriteLine(IntGuess);

            int on = 0;

            while (on < 1)
            {
                if (num > IntGuess)
                {
                    Console.WriteLine("Higher");
                }

                if (num < IntGuess)
                {
                    Console.WriteLine("Lower");
                }

                if (num == IntGuess)
                {
                    Console.WriteLine("Good job");

                }
            }
        }
    }
}
using System;
using System.Runtime.InteropServices;

namespace Guessing_game
{
    internal class Program
    {
        static void Main(string[] args)
        {
         Random rnd = new Random();
        int num = rnd.Next(1, 100);
        Console.WriteLine(num);
        Console.WriteLine("Guess the number");
        string Guess = Console.ReadLine();

        int IntGuess = int.Parse(Guess);

            Console.WriteLine(IntGuess);

            int on = 0;

            while (on < 1)
            {
                if (num > IntGuess)
                {
                    Console.WriteLine("Higher");
                }

                if (num < IntGuess)
                {
                    Console.WriteLine("Lower");
                }

                if (num == IntGuess)
                {
                    Console.WriteLine("Good job");

                }
            }
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ how to write javvscript only if c# statement is true ??
C#CC# / help
3y ago
✅ How to make this loop
C#CC# / help
3y ago
✅ If statement.
C#CC# / help
4y ago