C#.NET Jump Statements

C# provides a jump statements that allow you to jump immediately to another line in a program.

They are:

1. goto

2. break

3. continue

goto

It allows you to jump directly to another specified line in the program indicated by a label which is an identifier followed by a semi colon;

break

It is used to exit from a case in switch statement.

It is used to exit from loops like for, for each, while and do while which will switch the control to the statement immediately after the end of the loop.

continue

This can be used only in loops which will jump the control to the iteration part without executing the statements present after it.
Thursday, 31 December 2015
Posted by Sudhir Chekuri
Tag :

C#.NET Do While

Do while loop executes a set of statements inside it atleast once and repeated till the condition is met.

Syntax

Do

{

<statements>

} while(condition);

C#.NET DoWhile Example program

Program

using System;
namespace DoWhileLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            do
            {
                Console.WriteLine(i);
                i++;
            } while (i < 10);
            Console.ReadKey();
        }
    }
}

Output

0
1
2
3
4
5
6
7
8
9
Posted by Sudhir Chekuri
Tag :

C#.NET While loop

While loop is used to execute a set of statements till the condition is met. It doesn’t have initialization and increment or decrement operators as part of its syntax.

Syntax

While(condition)

{

<Statements>

}

C#.NET While loop program

Program
using System;

namespace ConsoleTasks
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i < 10)
            {
                Console.WriteLine(i);
                i++;
            }
            Console.ReadKey();
        }
    }

}

Output

0
1
2
3
4
5
6
7
8
9

Armstrong Number program

If the sum of the cubes of digits in a number is equal to that number then it is known as Armstrong number.

Examples

1
Amstrong number because 1^3 = 1

121
Not amstrong number because 1^3+2^3+1^3 = 1+8+1= 10.
10 not equal to 121

153
Amstrong number because 1^3+5^3+3^3 = 1+ 125 + 27 =153
153 equal to 153.

C#.NET program to say Armstrong or not

using System;

namespace Armstrong
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, num1, temp, q, sum = 0;
            num = Convert.ToInt32(Console.ReadLine());
            num1 = num;
            while (num > 0)
            {
                q = num / 10;
                temp = num % 10;
                sum = sum + (temp * temp * temp);
                num = q;
            }
            if (sum == num1)
            {
                Console.WriteLine("{0} is Armstrong ");//153
            }
            else
            {

                Console.WriteLine("{0} is not Armstrong");//121
            }

            Console.ReadLine();
        }
    }
}


Explanation: In this program the number entered is stored in a variable num.
The value inside variable num is copied into variable num1 to match the result with this number at the last.
While loop is used to retreive the last number of the given number and to find the cube of that number which are added and stored in variable sum.
After coming out of while loop the value in sum and num1 is matched to know wheather the given number is amstrong or not.
If they are matched the value given is amstrong otherwise not.
Posted by Sudhir Chekuri
Tag :

C#.NET foreach loop

foreach loop is a special loop used for collections and arrays. It is repeated based on the number of elements in a collection. It automatically takes one by one element from a collection and it is repeated till the last element is printed.

Syntax for C# foreach loop

foreach(type variable in collect:ion/array)
{
statements
}

Example program using C# foreach loop

Program 


using System;

namespace loops
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] i = new int[3] { 7, 8, 9 };
            foreach (int a in i)
            {
                Console.WriteLine(a);
            }
            Console.ReadKey();
        }
    }

}

Output

7
8
9

C#.NET program to print how many times a character is present in a string

using System;

namespace Chars
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "arshadmohammod";
            char[] c = s.ToCharArray();
            int count = 0;
            foreach (char ch in c)
            {
                if (ch == 'a')
                {
                    count++;
                }
            }
            Console.WriteLine("no of times a repeated is :" + count);
            Console.ReadKey();
        }
    }

}

Output

3

Explanation

above program contains a string variable s which contain "arshadmohammod"
In the above string a is repeated 3 times arshadmohammod

That string is divided into an array of characters using method ToCharArray( ) . -

char[ ] c = s.ToCharArray( );

count is a variable of int type intialized with zero used to count no of a's repeated in the string.

using for each loop all elements in char array are stored into ch one by one and repeatedly checked using if condition.

if (ch == 'a')
{
count++;
}

if element matched with a then count is incremented by one.
after the foreach loop the number in count variable is printed as output.
Posted by Sudhir Chekuri
Tag :

C#.NET for loop

C#.NET for loop is used to execute a code repeatedly until the condition is true. For loops are appropriate when you know exactly how many times you want to perform the statements within the loop. Normally, for loop statements execute from the opening curly brace to the closing curly brace without interruption. Every for loop can contain three sections commonly.
Those sections are
Initialization: Which sets the starting point of the loop.
Condition: Once the initializer list has been evaluated, the for loop gives control to its second section, ie.,the boolean expression. The code is executed until the condition satisfied ie.,until it returns true value.
Iteration(Increment/Decrement): Once Condition has been evaluated, the for loop gives to its control to third section ie.,Iteration, Which takes to the next side either positive or negative direction. Sometimes it will be incremented, sometimes decremented depending on the situation.

Syntax for C#.NET for loop

for(initialize; condition; inc/dec)
{
statements
}

Example program to print even numbers between 10 and 45 using for loop and if condition

Program

using System;
namespace For
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 10; i < 45; i++)
            {
                if (i % 2 == 0)
                { Console.WriteLine(i); }

            }
            Console.ReadKey();
        }
    }

}

Output

10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44

Example program using C#.NET for loop

Program

using System;

namespace loops
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < i + 1; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }

}

Output

*
**
***
****

C#.NET program to print stars in triangle format

Output required: Print a message to ask user to enter the number of rows based on which stars are printed.

In the first row there should be one star, In the second row 2 stars, In the third row 3 stars,... as shown in the below image.

Program
using System;

namespace PrintingStars
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter no. of rows");
            //taking no. of rows from user
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j < i; j++)
                { Console.Write("*"); }
                //for moving to second row
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }

}

Output

Enter no. of rows

4

*
**
***
****
Posted by Sudhir Chekuri
Tag :

C#.NET Conditional Looping

C#.NET provides four different loops that allows you to execute a block of code repeatedly until a certain condition is met.
Those are as follows
1) for loop
2) while loop
3) dowhile loop
4) foreach loop
Posted by Sudhir Chekuri
Tag :

C#.NET Switch

The switch statement which allows you to compare an expression with a number of different values.

Syntax of C#.NET Switch statement


switch(variable)
{
case value1:
statements;
break;
case value2:
statements;
break;
-------------------
-------------------
-------------------
default:
statements;
break;

Example program using C#.NET switch statement
Program

using System;

namespace Switch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number");
            int no = Convert.ToInt32(Console.ReadLine());
            switch (no)
            {
                case 1:
                    Console.WriteLine("One");
                    break;
                case 2:
                    Console.WriteLine("Two");
                    break;
                default:
                    Console.WriteLine("Invalid");
                    break;
            }
            Console.ReadKey();
        }
    }
}

Output

Enter number
1
One

Output

Enter number
2
Two

Output

Enter number
12
Invalid

Explanation

In the above example we can give input is 1 it prints output as One, input is 2 print output as Two, if we can give input is any number except 1 and 2 the output prints Invalid because if we can give input is 1 compiler executes case1 and we give i/p is 2 executes case2 then print output as One and Two. We give any other number except 1 and 2 default case will execute and prints Invalid.

Example program for C#.NET switch statement
Program

using System;

namespace Switch2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 4;
            switch (i)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("One");
                    break;
                case 4:
                case 5:
                    Console.WriteLine("Two");
                    goto case 1;
                default:
                    Console.WriteLine("Invalid");
                    break;
            }
            Console.ReadKey();
        }
    }
}


Output

Two
One

Explanation

In the above example we can assign i value as 4. So directly case 4 is executed and print output is Two, next no break statement is available. instead of break we write goto statement as goto case1,then after the execution go and starts from case1 then the output one will be printed. Finally the output will be Two and One printed line by line.
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -