C#.NET Nested If

It is one type of If statement. Nested If means if in if ie.,The if statement contains another if within that.

Syntax for Nested If

if(condition)
{
if(condition)
{
if(condition)
{
statements
}
}
}

Example program for Nested If Statements
Program 27


using System;

namespace ifelse
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("Enter number");
            i = Convert.ToInt32(Console.ReadLine());
            if (i > 0)
            {
                if (i > 5 && i < 10)
                {
                    Console.WriteLine("Between 5 and 10");
                }
            }
            Console.ReadKey();
        }
    }
}

Output

Enter number
4
_

Output

Enter number
8
Between 5 and 10

Explanation

In the above example we can give input is 4,nothing will be printed in output because 4 is greater than zero but it is not in between 5 and 10.so,the output will be _.In second output we can give input is 8,it is in between 5 and 10 so,it will be printed as Between 5 and 10.
Thursday, 31 December 2015
Posted by Sudhir Chekuri
Tag :

C#.NET Else if

This is one type of conditional branching.In this type the compiler 1st goes to if condition,if it is false it goes to else if condition,it also false finally goes to else statement and print output.

Syntax for Else if statement

if(condition)
{
statements
}
else if(condition)
{
statements
}
else
{
statements
}

Example program using Else if statement
Program

using System;

namespace elseif
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("Enter number");
            i = Convert.ToInt32(Console.ReadLine());
            if (i > 0)
            {
                Console.WriteLine("Positive");
            }
            else if (i < 0)
            {
                Console.WriteLine("Negative");
            }
            else
            {
                Console.WriteLine("zero");
            }
            Console.ReadKey();
        }
    }
}



Output

Enter number
223
Positive

Output

Enter number
-741
Negative

Output

Enter number
0
zero

Explanation

In the above example we give input is 223,it is greater than zero,then the output will be printed as positive and we give input is -741 it is less than zero,then the compiler goes to else if part of the program and print output as negative and finally gives 0 it is equal to zero,then the compiler goes to else pert of the program and print output as zero.
Posted by Sudhir Chekuri
Tag :

C#.NET If else

It is one of the statements in conditional branching. It is used to check whether the condition is true or false. if it is true, it goes to if condition and print output. if it is false, it goes to else part of the program and print the output.

Syntax for if else statement

if(condition)
{
statement
}
else
{
statement
}

Example program using if else statement

Program

using System;

namespace ifelse
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("Enter number");
            i = Convert.ToInt32(Console.ReadLine());
            if (i > 0)
            {
                Console.WriteLine("Positive");
            }
            else
            {
                Console.WriteLine("Negative");
            }
            Console.ReadKey();
        }
    }
}

Output
Enter number
12
Positive

Output

Enter number
-6
Negative

Explanation

In the above example we can give input is 12 then the output will be printed as positive because 12 is greater than zero. We can give input is -6 then the output will be printed as negative because -6 is less than zero.
Posted by Sudhir Chekuri
Tag :

C#.NET If

If statement allows you to test whether a specific condition is met.

Syntax

if(condition)
{
//statements
}

Example program using if condition

Program

using System;

namespace branching
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("Enter number");
            i = Convert.ToInt32(Console.ReadLine());
            if (i > 0)
            {
                Console.WriteLine("Positive");
            }
            Console.ReadKey();
        }
    }
}

Output

Enter number
12
Positive

Output

Enter number
-5
_

Explanation

In the above output we can give input is -5.here -5 is less than zero.so nothing will be printed in output. Blank will be printed in output.
Posted by Sudhir Chekuri
Tag :

C#.NET Conditional Branching

These statements allow you to branch code depending on whether the certain conditions met or not.
C# has two constructs for branching code.
1) If Statement
  • C# If
  • C#.NET If else
  • C#.NET Else if
  • C#.NET Nested If
2) Switch Statement
Posted by Sudhir Chekuri
Tag :

C#.NET Conditional Statements

A block of code that executes basing upon a condition is Conditional Statement . Conditional statements are used to execute a block of code only when the condition is satisfied.
Conditional statements are classified into two types.
1) Conditional Branching – If and Switch
2) Conditional Looping – For, for each, while and do while

C#.NET Conditional Branching

These statements allow you to branch code depending on whether the certain conditions met or not.
C# has two constructs for branching code.
1) If Statement
2) Switch Statement

C#.NET Conditional Looping

C# 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 String Functions

ToUpper and ToLower

ToUpper function is used to convert a whole string into uppercase and
ToLower function is used to convert a whole string into lowercase.

Example program to convert a string into lowercase and lowercase

Program

using System;

namespace UpperLower
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "suDHir";
            string s4 = str1.ToLower();  //converting into lowercase
            Console.WriteLine(s4);
            s4 = str1.ToUpper();         //converting into uppercase
            Console.WriteLine(s4);

            Console.ReadKey();
        }
    }
}

Output
sudhir
SUDHIR

Remove

Remove fuction is used to remove a part of a string by giving the starting index of the string and number of characters to be removed.

Example program to remove a part of string
Program

using System;

namespace Remove
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "sudhir";
            string s3 = str.Remove(1, 3); //removing characters from index 1, and removing 3 characters ie., udh
            Console.WriteLine(s3);

            Console.ReadKey();
        }
    }
}

Output

sir

Trim

Trim function is used to remove the white spaces from both the ends of the string.
Along with Trim function we also have TrimStart() and TrimEnd().
TrimStart is used to trim the white spaces at the starting of the string
TrimEnd is used to trim the white spaces at the end of the string.

Example program for Trim, TrimStart and TrimEnd
Program

using System;

namespace Trim
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "   hello    ";
            string tr = s.Trim();
            Console.WriteLine(tr);
            string tst = s.TrimStart();
            Console.WriteLine(tst);
            string tend = s.TrimEnd();
            Console.WriteLine(tend);

            Console.ReadKey();
        }
    }
}

Output

_hello_
_hello _
_ hello_

Replace

Replace function is used to replace an old string with a new string.

Syntax: string s2=s1.Replace(old string,new string);

Example program for Replace
Program

using System;

namespace Replace
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "hello";
            string s2 = s.Replace("hello", "hai");
            Console.WriteLine(s2);

            Console.ReadKey();
        }
    }
}

Output

hai
IndexOf

Used to find substring within a string and return its starting index value.

Example program for IndexOf

Program

using System;

namespace IndexOf
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "hellohai";
            string s1 = "hai";
            int i = s.IndexOf(s1);
            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}

Output

5
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

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