Conditional Structures in C# Programming Language
Conditional Structures in C# Programming Language
AND LOOPS
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
if (20 > 18)
{
Console.WriteLine("20 is greater than 18");
}
}
}
}
IF STATEMENTS
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 20;
int y = 18;
if (x > y)
{
Console.WriteLine("x is greater than y");
}
}
}
}
ELSE
using System;
STATEMENTS
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
ELSE
using System;
STATEMENTS
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
THE ELSE IF
STATEMENTS
ELSE IF STATEMENT
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
C# SWITCH
STATEMENTS
C# SWITCH STATEMENTS
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int day = 4;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
C# WHILE
LOOP
WHILE LOOP
using System;
namespace SampleForAndrew
{
class Program
{
static void Main(string[] args)
{
int x = 0;
while (x < 5)
{
Console.WriteLine(x);
x++;
}
}
}
}
WHILE LOOP
using System;
namespace SampleForAndrew
{
class Program
{
static void Main(string[] args)
{
int x = 1;
while (x < 5)
{
Console.WriteLine("Value of x:" + x);
x++;
}
}
}
}
THE DO /
WHILE LOOP
DO WHILE
using System;
STATEMENTS
namespace SampleForAndrew
{
class Program
{
static void Main(string[] args)
{
int x = 0;
do
{
Console.WriteLine(x);
x++;
}
while (x < 5);
}
}
}
FOR LOOP
FOR LOOP SYNTAX
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
FOR LOOP EXAMPLE
using System;
namespace forDemonstrationAndrew
{
class Program
{
static void Main(string[] args)
{
for (int x = 0; x < 5; x++)
{
Console.WriteLine(x);
}
}
}
}
FOR LOOP EXAMPLE
using System;
namespace forDemonstrationAndrew
{
class Program
{
static void Main(string[] args)
{
for (int x = 0; x <= 10; x = x + 2)
{
Console.WriteLine(x);
}
}
}
}