If..Else Statement is used whenever a particular condition is true, a certain set of tasks needs to be done and if the condition isn't true then another set of tasks are performed.
Syntax :
if (condition)
{
set of tasks or statements; (Set 1)
}
else
{
set of tasks or statements; (Set 2)
}
If the condition is satisfied, then the Set 1 statements are executed. If the condition fails, then the Set 2 statements are executed.
Example : Find if a value is even or odd. If even then print a statement, if not then print another statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int value = number % 2; //If
the remainder is zero its an even number.
if (value == 0)
{
Console.WriteLine("The number {0} is an even number.",
number);
}
else
{
Console.WriteLine("The number {0} is an odd number.",
number);
}
Console.ReadLine();
}
}
}
Output :
No comments:
Post a Comment