2. C# if-else
• In C# programming, the if statement is used to test the condition. There
are various types of if statements in C#.
• if statement
• if-else statement
• nested if statement
• if-else-if ladder
• C# IF Statement
• The C# if statement tests the condition. It is executed if condition is
true.
• Syntax:
• if(condition)
• { //code to be executed }
3. C# If Example
• using System;
• public class IfExample
• {
• public static void Main(string[] args)
• {
• int num = 10;
• if (num % 2 == 0)
• {
• Console.WriteLine("It is even number");
• }
• }
• }
4. C# IF-else Statement
• The C# if-else statement also tests the condition. It
executes the if block if condition is true otherwise else
block is executed.
• Syntax:
• if(condition){
• //code if condition is true
• }
• else{
• //code if condition is false
• }
5. C# IF-else-if ladder Statement
• The C# if-else-if ladder statement executes one condition from multiple
statements.
• Syntax:
• if(condition1){
• //code to be executed if condition1 is true
• }else if(condition2){
• //code to be executed if condition2 is true
• }
• else if(condition3){
• //code to be executed if condition3 is true
• }
• ...
• else{
• //code to be executed if all the conditions are false
• }
7. • The C# for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use
for loop than while or do-while loops.
• The C# for loop is same as C/C++. We can initialize variable, check
condition and increment/decrement value.
• A group of similar category objects is called as collections.
especially foreach loop is provided for collections
• Syntax:
• for(initialization; condition; incr/decr)
• {
• //code to be executed
• }
8. foreach
• A group of similar category objects is called as collections. especially
foreach loop is provided for collections
• syntax:
• foreach(control c in this controls)
• { statements; }
• foreach(var item in collections)
• { statements; }
• foreach(object obj in collections)
• { statements; }
9. Example on foreach loop:
• 1.take one form
• 2.add no of textboxes & take one button.
• 3.click on the button how many textboxes are there we can apply same color
• private void button1_Click(object sender, EventArgs e)
• {
• foreach (Control c in this.Controls)
• {
• // MessageBox.Show(c.GetType().Name);
• if (c.GetType().Name == "TextBox")
• {
• c.BackColor = Color.Red;
• }
• }
• }
10. While loop
• In C#, while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed, it is recommended to use while loop than for loop.
• syntax:
• while(condition)
• {statements; }
• Example:
• int i = 10;
• while (i<=100)
• {
• Console.WriteLine(i.ToString());
• i++;
• }
• Console.ReadLine();
• }
11. Do while
• The C# do-while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.
• The C# do-while loop is executed at least once because condition is checked after loop
body.
• syntax:
• do
• { statements; }
• while(condition);
• example:
• static void Main(string[] args)
• { int i = 10;
• do
• { Console.WriteLine("the value i is:" + i.ToString());
• i++;
• } while (i <= 100);
12. Switch(expression)
• The C# switch statement executes one statement from multiple conditions. It is
like if-else-if ladder statement in C#.
•
{
• case 0:statements;
• break;
• case 1:statements;
• break;
• case 2:statements;
• break;
• default:statements;
• break;
• }
14. C# Break Statement
• The C# break is used to break loop or switch
statement. It breaks the current flow of the
program at the given condition. In case of inner
loop, it breaks only inner loop.
• Syntax:
• jump-statement;
• break;
15. • using System;
• public class BreakExample
• {
• public static void Main(string[] args)
• {
• for (int i = 1; i <= 10; i++)
• {
• if (i == 5)
• {
• break;
• }
• Console.WriteLine(i);
• }
• }
• }
16. C# Continue Statement
• The C# continue statement is used to continue loop.
It continues the current flow of the program and
skips the remaining code at specified condition. In
case of inner loop, it continues only inner loop.
• Syntax:
• jump-statement;
• continue;
17. • using System;
• public class ContinueExample
• {
• public static void Main(string[] args)
• {
• for(int i=1;i<=10;i++){
• if(i==5){
• continue;
• }
• Console.WriteLine(i);
• }
• }
• }
18. Goto Statement
• The C# goto statement is also known jump statement. It is used to transfer control to the other part
of the program. It unconditionally jumps to the specified label.
• It can be used to transfer control from deeply nested loop or switch case label.
• Currently, it is avoided to use goto statement in C# because it makes the program complex.
• C# Goto Statement Example
• Let's see the simple example of goto statement in C#.
• using System;
• public class GotoExample
• { public static void Main(string[] args)
• { ineligible:
• Console.WriteLine("You are not eligible to vote!");
• Console.WriteLine("Enter your age:n");
• int age = Convert.ToInt32(Console.ReadLine());
• if (age < 18)
• { goto ineligible; }
• else
• { Console.WriteLine("You are eligible to vote!");
• } } }