Skip to main content

Posts

Conditional constructs example

Qu 1: Write a program in which accept a number from the user and identify whether the number is even or odd. Example: using System; namespace example1 {    class Program    {      static void Main( string [] args)        {          int num;          Console .Write( "Enter your number:\t" );          num = Convert .ToInt32( Console .ReadLine());          if (num % 2 == 0)          {             Console .WriteLine( "{0} is Even Number" , num);          }          else          {            Console .WriteLine( "{0} is Odd number" , num);           }          Console .ReadLine();  ...

C# Statements

There are various basic things in C# that you need to know. These are very small but too effective. These are called statements in C#. Without covering statements in C#, you can’t be a good programmer. In C# programming, there is various statements as block, empty, goto-label, break, continue, return, throw, checked, unchecked, lock, using etc. These are small but give powerful control you to write your program. List of Contents C# Statements Chapter 1: Block Chapter 2: Empty Chapter 3: Goto-label Chapter 4: Break Chapter 5: Continue Chapter 6: Return Chapter 7: Throw Chapter 8: Checked Chapter 9: Unchecked Chapter 10: Lock Chapter 11: Using Chapter 12: Enumeration Chapter 13: Structure Chapter 14: Examples

C# Block Statement({})

The block statement is used to write multiple segments of program in logical group. Example: using System;   namespace Block_statement { //Begin Statement    class Program    { // Begin Statement      static void Main( string [] args)        { //Begin Statement          int num1,power;          num1 = 5;          power = num1 * num1;          Console .WriteLine(power);          Console .ReadLine();        } // End Statement    } // End Statement } // End Statement

C# Empty Statement(;)

Empty statement is used when you no need to perform an operation where a statement is required. It simple transfers control to the end point of the statement. It is also very useful with while loop with blank body and label statements. Example: using System; namespace empty_statement {    class Program    {      public bool print()        {          Console .WriteLine( " Aav Kumar " );          return true ;        }       static void Main( string [] args)        {          int i = 0;          Program p = new Program ();          while (p.print())          {             ; //Empty Statement          }       ...

C# Goto Statement

The goto statement is a jump statement that controls the execution of the program to another segment of the same program. You create label at anywhere in program then can pass the execution control via the goto statements . Example: using System; namespace goto_statement {    class Program    {      static void Main( string [] args)        {          string name;          label: //creating label with colon(:)          Console .WriteLine( "Enter your name:" );          name = Console .ReadLine();          Console .WriteLine( "Welcome {0}" , name);          goto label; //jump to label statement                    }    } } Note: To terminate the program press Ctrl+C Output En...

C# Break Statement

The break statement is used to terminating the current flow of program and transfer controls to the next execution. Example: using System; namespace break_statement {    class Program    {      static void Main( string [] args)        {          int i = 0;          while (i < 100)          {             Console .WriteLine(i);            if (i == 20)              {                Console .WriteLine( "breaking the current segment..." );                break ;              }            i++;          }             Console .Rea...

C# Continue Statement

The continue statements enables you to skip the loop and jump the loop to next iteration.   Example: using System; namespace Continue_Statement {    class Program    {       static void Main( string [] args)        {          int i = 0;            while (i<10)          {             i++;            if (i < 6)              {                continue ;              }                  Console .WriteLine(i);          }                      Console .ReadLine();        }    } } ...