The statement that can be executed based on the condition in
called conditional statement. The statement is a block of code.
They are following 2 types:
1. Conditional Branching
2. Conditional Looping
Conditional Branching
They are also 2 types:
1. If statement
2. Switch statement
If statement – If
statement is used to test whether or not a condition is met.
The example of the if statement is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Enter
the number");
a
= Int32.Parse(Console.ReadLine());
b
= Int32.Parse(Console.ReadLine());
if (a > b)
{
Console.WriteLine("a is greater");
}
else if (b > a)
{
Console.WriteLine("b is geater");
}
else
{
Console.WriteLine("Both are equal");
}
Console.ReadLine();
}
}
}
Switch Statement –
switch statement is used to compare two logical expressions.
The example of the switch statement is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Switch
{
class Program
{
static void Main(string[] args)
{
int value = 5;
switch (value)
{
case 1:
Console.WriteLine(1);
break;
case 5:
Console.WriteLine(5);
break;
default:
Console.WriteLine("you can choose correct value");
break;
}
Console.ReadKey();
}
}
}
Conditional loops
– Conditional loops is divided into 4 categories
For Loop
While Loop
Do while Loop
Foreach Loop
Each loop contains following 3 features
Initialization –
it sets the starting points of loops
Condition – it sets
the ending points of loops
Iteration – it is
used to set the forward or backward direction of loops
The example of the For Loop is given below:
Program to generate the
factorial of a number using for loop:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
int a, fact;
a
= Int32.Parse(textBox1.Text);
fact = 1;
for (int i = 1; i
<= a; i++)
{
fact = fact * i;
}
textBox2.Text = fact.ToString();
}
}
}
Output:
The example of the
while loop is given below:
Program to generate the
factorial of a number using while loop:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
int a, fact;
a
= Int32.Parse(textBox1.Text);
fact = 1;
int i = 1;
while (i <= a)
{
fact = fact * i;
i++;
}
textBox2.Text = fact.ToString();
}
}
}
Output:
The example of the do
while loop is given below:
Program to generate the
factorial of a number using do while loop:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
int a, fact;
a
= Int32.Parse(textBox1.Text);
fact = 1;
int i = 1;
do
{
fact = fact * i;
i++;
}
while (i <= a);
textBox2.Text = fact.ToString();
}
}
}
Output:
For each Loops –
For each loop in c# executes a block of code on each element in array .The syntax of the for each loops is given below:
string[] days = { "Sunday", "Monday",
"TuesDay"};
foreach (string day
in days)
{
MessageBox.Show("The day is : " +
day);
}
Author :
No comments:
Post a Comment