Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Statements

Similar presentations


Presentation on theme: "Conditional Statements"— Presentation transcript:

1 Conditional Statements
No arithmetic expressions are allowed where boolean expressions are expected Thus, if you write: if ( x = y ) // compilation error

2 Operator Evaluation Order
x = a a; left to right x = 10 right to left x = 8 X=?

3 Loops C# provides a number of the common loop statements while
do-while for foreach

4 ‘foreach’ Loop Syntax foreach (variable1 in variable2) statement[s]
The 'foreach' loop is used to iterate through the values contained by any object which implements the IEnumerable interface When a 'foreach' loop runs, the given variable1 is set in turn to each value exposed by the object named by variable2 int[] a = new int[]{1,2,3}; foreach (int b in a)     System.Console.WriteLine(b);

5 Switch switch (expression) { case constant-expression: statements
Expression – an integral or string expression switch (expression) { case constant-expression: statements jump statement [default: ] } Jump statement is required for each block – even in default block Fall through is allowed to stack case labels Control does not fall through

6 switch(a) {      case 2:          Console.WriteLine("a>1 and ");          goto case 1;      case 1:       Console.WriteLine("a>0");          break;      default:          Console.WriteLine("a is not set");          break; }

7 void func(string option)
{ switch (option) case "label": goto case "jump": case "quit": return; case "spin": for(;;){ } case "jump": case "unwind": throw new Exception(); default: break; }

8 Class Class Attributes
Attributes are used to give extra information to the .NET compiler E.g. it’s possible to tell the compiler that a class is compliant with the .NET Common Language Specification [CLSCompliant(true)] public class myClass { //class code7 }

9 Class Class attributes Class modifiers ‘public’ ‘private’ ‘protected’
‘internal’ Accessible only to types within the same assembly Similar to a package (in Java) ‘internal protected’ ‘new’

10 new The 'new' keyword can be used for 'nested' classes
A nested class is one that is defined in the body of another class; it is in most ways identical to a class defined in the normal way, but its access level cannot be more liberal than that of the class in which it is defined A nested class should be declared using the 'new' keyword just in case it has the same name as (and thus overrides) an inherited type

11 ‘abstract’ A class declared as 'abstract' cannot itself be instantiated - it is designed only to be a base class for inheritance ‘sealed’ A class declared as 'sealed' cannot be inherited from structs vs. sealed classes


Download ppt "Conditional Statements"

Similar presentations


Ads by Google