Skip to main content

Posts

Showing posts from May, 2015

C# Comparison Operators

The C# comparison operator is used to compare two operands. It returns true or false based on comparison. The complete list of comparison operators are listed in a table. Consider x is a variable and the value assigned the x=2 then, Operator Name Examples < Less than x<5 (returns true) > Greater than x>5 (returns false) <= Less than equal to x<=2 (returns true) >= Greater than equal to x>=2 (returns true) == Equal equal to x==2 (returns true) != Not equal to x!=2 (returns false) Examples: using System; namespace Comparison_Operator {    class Program    {      static void Main( string [] args)        {          int num1, num2;          //Accepting two inputs from the user          Console .Write( "Enter first number\t" );          num1 = Convert .T...

C# Logical Operator

The C# Logical Operator also evaluates the values and returns true or false as output. Based on true-false the program behaves dynamically at run time. This operator is widely used with C# programming. && Operator : It is pronounced as and operator . It returns true if both or all the condition is true and return false if any of the condition is false. Examples: using System; namespace and_operator {    class Program    {      static void Main( string [] args)        {          string name,password;          name= " Aav " ;          password= " A av 123" ;           // evaluating both expresson and returns true if all are true.          if (name == " A av " && password == " Aav 123" )          {          ...

C# Operator Example

The main objective of this chapter is to explain you in details about how to use C# operators in a program. 1. Write a program to check whether input character is vowel or not. using System; namespace Example1 {    class Program    {      static void Main( string [] args)        {          char ch;          Console .Write( "Enter a character: \t" );          ch = Convert .ToChar( Console .ReadLine());          if (ch == 'a' || ch == 'A' || ch == 'e' ||ch == 'E' || ch == 'i' || ch == 'I' ||                      ch == 'o' || ch == 'O' || ch == 'u' || ch== 'U' )          {            Console .WriteLine( "\n\n{0} is vowel" ,ch);          }       ...

Conditional Constructs (C#)

Conditional constructs is used to transfer execution control to the correct path based on comparison result. The conditional constructs determine runtime that which statements need to be executed. It uses comparison result for determining correct path of execution. If else, switch case are used for comparing value. In this chapter you will learn about conditional constructs in C#. List of Contents Conditional Constructs Chapter 1: if-else constructs Chapter 2: switch-case constructs Chapter 3: Examples

C# if else constructs

The if… else construct is used for determining the flow of program based on returning expression value. It evaluates the comparison operator and based on value executes the statements. For example, if you want to execute a piece of code, when the requirements meet then if… else construct determine which piece of code will be executed. Else is default condition and executes when no if condition matches. The following example will clear the concept of if… else construct using System; namespace if_else_construct {    class Program    {      static void Main( string [] args)        {          int opt, num1, num2;          float result;          label:          Console .WriteLine( "\n\tMenu" );          Console .WriteLine( "\nPress 1 for add" );          Cons...

C# switch case constructs

Switch case is also another condition constructs in C# programming that evaluates the condition as if else but only difference is that it makes program simpler and easier. It is used when there is multiple if condition in a program. It also includes a default value in Default statements. If no any case matches then Default statements executes and run the code. using System; namespace Switch_Case {    class Program    {      static void Main( string [] args)        {          int opt, num1, num2;          float result;          label:          Console .WriteLine( "\n\tMenu" );          Console .WriteLine( "\nPress 1 for add" );          Console .WriteLine( "Press 2 for subtraction" );          Console .WriteLine( "Press 3 for mu...