Branching and Looping

By Bintu Chaudhary   Posted at  10:45:00 PM  

Home  »  C#  »  Branching and Looping

     We know that Instruction those are written in C# Language are Executed in Sequence wise or Step Wise as they are Written in Program. or these are Executed in Sequence Order. Decision Making statements are used when we wants to execute the statements as according to the user needs. A User can Change the Sequence of the Statements for Execution. Many Times we Wants to Execute the Set of Instructions to be Executed in one Situation and other Statements in other Situations For Executing the Statements in Specific Situation there are Some Decision Making Statements or Decision or Control Statements those are provided by the java Language. In The Decision First a Condition is checked if it is true then it Executes the next Instruction otherwise it Executes other Statements.

There are three decision making statements :-
1. if
2. Switch Statement
3. The Conditional or ternary Operator


Branching Statement

if statement

     The Keyword if tells the compiler what follows The Condition Following the Keyword if is always enclosed within a pair or Parentheses if The Condition id True Then the Statements is Executed if the Condition is not true then the Statement is not Executed For Checking a condition Relational Operators are Used Like > ,< ,==, >=,<= etc. The if statement is widely used for checking a particular condition Suppose you want to check the condition and then execute the condition if your condition is met.
for ex:
then print a is greater then here we first check the condition
that either a is greater than b if yes then execute the condition


     The if Statement is used for Executing a Single Statement or a Group of Statements When the Condition Following is true . It does nothing when the Condition is False , For Executing the group of Statements either a Condition is False we uses else The if-else is similar to if but the difference is that is also provides us the alternative to execute the other statement if a condition is not true for ex:

print a
print b
Here if first checks the condition either the a is greater than b if yes then it will print a suppose if a is not greater than b then it will be print b because we specify the b in the else statement.

if-else if

     The if-else if is similar to the if-else but here the first if is used for checking a condition and the other else if is used for checking a one more condition suppose if we wants to check the two or more conditions then we can use the if-else if.

Nested if ....else statement

     When an if statement occurs within another if statement, then such type of is called nested if statement.

The ? : Operator

     This operator is an assignment operator that selects one of the two values depending on the truth value of given condition. consider the following code snippet.
A = condition ? value1 : value2;

The value1 is assigned to the variable a in case the condition is true otherwise value2 is assigned to the variable a.

Switch Statement

     This Statement is Similar to if –else but it uses matching operations rather than checking a condition if we Wants to Check Many Conditions then the Program will very Complex So in that Situation We Will use Switch Statement Switch Statement Uses Many Cases rather and it matches the value with Case where it Matches it will Execute the Statement But There is Very Necessary to Stop the Execution of Each Case So that it Doesn't Execute Next Case So that for this Purpose we have to put the break Statement after Ending of an Each Case We Know if uses Else Statement if Condition is False or All Conditions are False The Code of Else Statement is Executed if Code of all if statements is false So that in Switch Default is used when all Cases are not Matched In other Languages like c and C++ we can Match Only the Integer or a Character value but in the C# We can also a String in the Switch Statement.

Looping Statement

     A Computer is used for performing many Repetitive types of tasks The Process of Repeatedly performing tasks is known as looping The Statements in the block may be Executed any number of times from Zero to Up to the Condition is True The Loop is that in which a task is repeated until the condition is true or we can say in the loop will Executes all the statements are until the given condition is not to be false. These are generally used for repeating the statements. In this There is Either Entry Controlled loop or as Exit Controlled Loop We know that before Execution of Statements all Conditions are Checked these are Performed by Entry Controlled Loops Which First Checks Condition And in Exit Controlled Loop it Checks Condition for Ending Loop Whether given Condition is False or not if a Loop First Checks Condition For Execution then it is called as Entry Controlled Loop and if a Loop Checks Condition after the Execution of Statement then they are Called as Exit Controlled Loops.

In The loop generally there are three basic operations are performed
1) Initialization
2) Condition check
3) Increment

There are the three types of loops in the C#
1. while
2. do-while
3. for
4. foreach
All these are used for performing the repetitive tasks until the given condition is not true.


     While Loop is Known as Entry Controlled Loop because in The while loop first we initialize the value of variable or Starting point of Execution and then we check the condition and if the condition is true then it will execute the statements and then after it increments or decrements the value of a variable. But in the Will Condition is false then it will never Executes the Statement.

Do while

     This is Also Called as Exit Controlled Loop we know that in The while loop the condition is check before the execution of the program but if the condition is not true then it will not execute the statements so for this purpose we use the do while loop in this first it executes the statements and then it increments the value of a variable and then last it checks the condition So in this either the condition is true or not it Execute the statement at least one time.


     In This loop all the basic operations like initialization ,condition checking and incrementing or decrementing all these are performed in only one line. this is similar to the while loop for performing its execution but only different in its syntax.


     This is another type of Loops which is provided by C# Language this Loops works Collection Variables Like Arrays, List and Strings or Any Command Line Arguments. For each Loop First initialize a first Value which is Stored into the Collection Element and then as loop do the Next Element will be Assigned to the foreach variable But Remember we do not have to end foreach loop Explicitly by using Any Statement.

//The same way be achieved using the foreach

Jumping Statement

     These Statements are also Called as Jumping Statements those are used for transferring the Control of Execution of program using break, continue etc.


     This statement is used for stopping the execution of the program the break statement is used where we does t want to execute the next statements and Wherever it finds a Break Statement then from that Compiler will not Execute Any Statement. And the Execution of program will be halt or Stopped it doesn't Execute Any Statement after the break Statement.


     The Continue Statements is used for executing the statements by skipping a statement for ex if u don t want to execute any statement then we can use the continue means to skip a particular statement and then execute the next statements. Wherever it Finds a Continue Statement the Control Will be Transferred to another Statement But Always Remember once the Control has been Transferred this will never come back to its Previous Statement.


     Goto Statement is used for transferring the Execution to another Statements to a Specific Location and Location is Explicitly defined by user and that Location must have a Specific Name so that when a use wants to send to that Location he must have to Specify the Name along with goto . Name with Goto have any valid Name and Must be defined by using Colon (: )

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me