This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Module 4- Flow Control as PDF for free.
- while, do..while, for, foreach - break, continue, goto, return
Statements
Statements are the smallest unit of execution in C# programs. They can be grouped into sequences using curly braces. These are called Compound Statements or Blocks. A compound statement can be used in place of simple statement.
C# provides variety of statements. Most of the statements are similar to C,C++,Java.
The statements in C# can be categorized as:
Selection Statements
Iteration Statements
Jump Statements
Selection Statements
Selection Statements selects one of a number of possible statements for execution based on the value of a controlling expression.
A selection statement can be:
If Statement
Switch Statement
If Statement
The If statement selects a statement for execution based on the value of a Boolean expression.
Syntax:If(condition 1) statement_1 Else statement_2 statement_3 The condition is tested first. If the condition evaluates to TRUE then statement_1 is executed otherwise statement_2 is executed. After that statement_3 is executed.
Example 1 – If Statements using System; class If_construct { public static void Main() { int var1 =10; if (var1 > 0) Console.WriteLine(“Your number is greater than 0"); else Console.WriteLine(“Your number is less than or equal to 0”); } }
Example 1 – If Statements.. Output of the code is: Your number is greater than 0
Nested If
The If statement may be nested to many levels. This means If statement may contain another If statement.
Example – Using Logical Operators in If..else Check the age of an applicant and display an appropriate message as follows: Print the message “ Valid age” if the age of the candidate is between 18 and 25 and print “Invalid age” if the age criterion is not satisfied. using System; class IF { public static void Main() { int age=26; if (age >=18 && age <=25)
Example – Using Logical Operators in If..else .. Console.WriteLine("Valid age"); else Console.WriteLine("Invalid age"); } } Output of the code is: Invalid age
Example 1 – Nested IF using System; class NestedIF { public static void Main() { int A=30;int B=50;int c=25; if (A > B) { if (A > C) { than” +C
Console.WriteLine(A+ “is greater + “and” +B);
}
Example 1 – Nested IF.. else if (A > C) { Console.WriteLine(B+ “is greater than” +C + “and” +A); } } }
Switch Statement A switch…case statement executes the statements that are associated with the value of controlling expression as follows: The switch expression is evaluated first and acts as the control of execution. If the value of the case label matches the switch expression, then the statements following it are executed. A label in a case is a fixed value. If no label matches the value of the switch expression, then the statements following the default label are executed.