Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

C# Arrays

C# Character

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

C# ArrayList

C# Stack

C# Queue

C# HashTable

C# SortedList

Page Stats

Visitor: 428

C#.Net Flow Control

Flow control can be categorized into 3 categories:

  1. Selection Statements : Selection statements or Decision making statement select one of a number of possible statements for execution based on the value of a controlling expression. Selection statements are of 2 types:
    1. if statement : if statement selects a statement for execution based on the condition specified.
    2. if-else statement :
    3. Nested if-else statement :
    4. Switch statement : Switch statement executes the statements that are associated with the value of the controlling expression.
  2. Iterations or Loops : Iterations are used to repeate execution of a statement or a set of statements. There are 4 types of Iterations:
    1. For loop : Use to perform iteration i.e. repeat a block of instructions a given number of times.
    2. While loop : Use to conditionally execute a set of statements zero or more time.
    3. Do Loop : Use to conditionally execute a set of statements one or more time.
    4. Foreach loop : Foreach loop is used to iterate through the collection to get the desired information.
  3. Jumping Statements : Jumping statements are used to unconditionally transfer the control or it may transfer control out of a block. Jumping statements are of 3 types:
    1. Break statement : Break statement exits the nearest enclosing switch, while, do-while, for or foreach loop.
    2. Continue statement : Skips all the statements which are mentioned below continue keyword. Continue keyword is used inside a loop only.
    3. Goto statement : The goto statement transfers the program control directly to a labeled statement.

Ternary operator

In C#, Ternary operator '?:' is also a decision making operator like if, if-else, switch statement, but the limitation of ternary operator is that it is one line decision making operator. On the basis of condition it execute the statment but only one line statement. Syntax:

return_data_type var_name = condition ? true : false ;

Example 1: Find greater number

string s = a > b ? "1st number is greater" : "2nd number is greater;
Console.WriteLine(s);

Example 2: Find greater number

int a=10, b=20;
int i = a > b ? a : b;
Console.WriteLine("Greater number is "+i);
Nested Ternary operator

Example 3: Find greater number from 3 numbers

int a=10, b=20, c=30;
int i = (a > b && a>c) ? a : (b>a && b>c)? b : c;
Console.WriteLine("Greater number is "+i);

Goto in switch statement

The switch case can use goto statement to jump on any switch case

Example 4: Goto in switch statement

string s = "c-sharp";

switch (statementType)
{
    case "c++":
        Console.Write("C++ Language");
    break;
    case "java":
        Console.Write("Java Language");
    break;
    case "python":
        Console.Write("Ternary operator");
    break;
    case "c-sharp":
        Console.Write("C-Sharp combines the good features of ");
        gotocase "c++";
        Console.Write(" and ");
        gotocase "java";
}
C-Sharp combines the good features of C++ Language and Java Language
Advertisement
Updated: 23-Aug-21