Thursday, April 14, 2016

Conditional statement in c#



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 :


Wednesday, April 13, 2016

Difference between Break and continue statement in c# with example



Break Statement – we use break statement for “jump out of loop” the example of the break statement is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i <= 4; i++)
            {
                if (i == 3)
                {
                    break;
                }
            Console.ReadLine( "The number is"+i );
           
            }
        }
    }
}

Output:
The number is 0
The number is 1
The number is 2

Continue Statement – we use continue statement for “jump over one iteration” the example of the continue statement is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i <= 4; i++)
            {
                if (i == 3)
                {
                    continue;
                }
            Console.ReadLine( "The number is"+i );
           
            }
        }
    }
}

Output :
The number is 0
The number is 1
The number is 2
The number is 4




Sunday, April 10, 2016

Date Function in SQL

Date Function in SQL

The various functions are as follows:

Getdate ()

It returns the date and time of the system where the server is running.

Example

select Getdate () Ans: 2016-04-10 15:30:57.073

Day (d)

It returns the day part of the specified date 'd'

Example

(1) select Day (Getdate ()) Ans: 10
(2) 
select Day ('24/14/89') Ans: Error (format must be MM/DD/YY).

Month (d)

It returns the month part of the given date 'd'.

Example

select Month ('04/10/80')  Ans 4

Example

Queston Write a query to get the list of employees joined in the month of April?
Ans select * from Emp where Month (Hiredate) = 04

Year (d)

It returns the year part of the given date 'd'.

Example


select Year ('12/24/1989')   Ans: 1989

Datename (Datepart, Date)
It is a generalized function which can pick any specific date part value like day, month or year, etc. 
Example
select Datename (DD, Getdate ());
select Datename (mm, Getdate ());
select Datename (yy, Getdate ());

The ‘Datepart’ values can be any of the following prescribed values.

yearyy, yyyy
quarterqq, q
monthmm, m
day of yeardy, y
daydd, d
weeksk, ww
weekdaydw
hourHH, hh
minutemi, n
secondsss, s
millisecondsms

Example

Question Write a program to get the details of employee who has joined on 'Monday'?
Answer select * from Emp where Datename (dw, Hiredate) = 'Monday'

DateAdd (datepart, n, d)

 It returns a new date value by adding the specified no. of datepart values to the given date.

Example

select DateAdd (DD, 75, Getdate ()) Ans 2016-06-24 15:52:54.417

DateDiff (Datepart, D1, D2)

It returns the difference between D1 and D2 in the specified 'Datepart' format.

Example

Question Write a query to find out the no. of years each employee is working in the organization?

Answer  

select * from Emp where DateDiff (yy, Hiredate, Getdate ())  (wrong)
select DateDiff (yy, Hiredate, Getdate ()) from Emp  (right)

Question Write a query to find how many years now completed from Independence day?
Answer select DateDiff (yy, '08-15-1947', Getdate ())

Question Write a query to get a list of employees who are working for more than 28 years?
Answer select * from Emp where DateDiff (yy, Hiredate, Getdate ())