C# Control Statements => If Else Statement

C# control statements with a simple if else statement to check negative and positive numbers.

 

Intro

  • Beginner C# tutorial right here.
  • We've used Visual Studio as our IDE.
  • The code is well commented for easier understanding.

Common Questions we answer

With this simple example we explore the following :

  • How to use If-Else statement in C#.
  • How to determine negative and positive numbers.
  • How to loop throgh an array.
  • How to use a ForEach loop.
  • How to write or read from console.

Tools Used

  • IDE : Visual Studio 2013
  • OS : Windows 8.1

Source Code

Here's the source code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace If_Stmt
{
    class Program
    {
        static readonly int[] numbers={2,-8,9,5,-7,3,-4};
        static void Main(string[] args)
        {
            foreach (var num in numbers)
            {
                if (num < 0)
                {
                    Console.WriteLine(num+" is negative");
                }
                else
                {
                    Console.WriteLine(num + " is Positive");

                }
            }
            Console.ReadKey();
        }
    }
}

Result

C# If Else statement C# If Else statement

How To Run

  • Just copy the project above.
  • Open the Visual Studio.
  • From the Menu bar click on File -> New -> Visual C# -> Console
  • Type the project name.
  • Paste the code in the program.cs file
  • Click Start to run the project

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *