Section 2 - Selection and Repetition Equality and Relational Operators Relational Operators int aValue = 100, bValue = 1000; string sValue = “CS158”; decimal money = 50.22m; double dValue = 50.22; char cValue = ‘A’; Logical Operators C# Operators The table on the following slide describes the allowable operators, their precedence, and associativity. Left associativity means that operations are evaluated from left to right. Right associativity mean all operations occur from right to left - Assignment operator, for example - everything to the right is evaluated before the result is placed into the variable on the left. Use round brackets ( ) to impose your own precedence on expression evaluation Decision Structures - Selection Same syntax for C++, C#, and Java if (Boolean expression) statement; • The basic selection statement is the if statement. • Used to execute or skip a statement or block of statements { … } depending on a Boolean expression, • Also have an if … else … construct Examples if (x == value100) { Console.Write("x is "); Console.Writeline( x ); } if (x == value100) Console.Writeline("x is 100"); else Console.Writeline("x is not 100"); if (salesForYear > 500000.00) { Console.WriteLine( ); Console.WriteLine(“YES...you get a bonus!”); bonusAmount = 1000.00; } if (hoursWorked > 40) { payAmount = (hoursWorked – 40) * payRate * 1.5 + payRate * 40; Console.WriteLine(“You worked {0} hours overtime.”, hoursWorked – 40); } else payAmount = hoursWorked * payRate; Switch construct Same syntax for C++, C#, and Java switch can only be used to compare an expression with different constants. The types of the values a switch statement operates on can be booleans, integer types, and strings (null is acceptable as a case label). Every statement sequence in a case must be terminated with break If no case label matches default switch (expression) { case constant1: block of instructions 1 break; case constant2: block of instructions 2 break; . . . default: default block of instructions break; } You may also include a default choice following all other choices. If none of the other choices match, then the default choice is taken and its statements are executed. Use of the default choice will help catch unforeseen circumstances and make your programs more reliable. switch (x) { case 5: Console.Writeline("x is 5"); break; case 99: Console.Writeline("x is 99"); break; default: Console.Writeline("value of x unknown"); break; } switch(country) { case"Germany": case"Austria": case"Switzerland": language = "German"; break; case"England": case"USA": language = "English"; break; case null: Console.WriteLine("no country specified"); break; default: Console.WriteLine("don't know language of", country); break; } Loops Same syntax for C++, C#, and Java while (Boolean expression) {statements} int x – 10; while ( x > 1 ) { Console.Writeline("x is "); Console.Writeline( x ); x--; } int sum = 0; int number = 1; while (number < 11) { sum = sum + number; number++; } Console.WriteLine(“Sum of values ” + “1 through 10” + “ is ” + sum); do {statements} while (Boolean expression) int x = 10; do { Console.Write("x is "); Console.Writeline( x ); x--; } while (x !=1 ); Loops Same syntax for C++, C#, and JAVA for (initialization; condition; in(de)crement) {statement;} for (int n=10; n>0; n--) { Console.WriteLine( n ); } Examples x = 5; if (x > 0 && x < 10) count++; else if (x == -1) ... else { ... } while (x > 0) { ... x--; } for (int k = 0; k < 10; k++) { ... } foreach Construct • Specialized foreach loop provided for collections like arrays – reduces risk of indexing error – provides read only access int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach foreach (int x in data) { sum += x; } type value array foreach statement For looping over arrays int[] a = {3, 17, 4, 8, 2, 29}; foreach (int x in a) sum += x; //sum numbers in array string s = "Hello"; foreach (char ch in s) Console.WriteLine(ch); Windows Applications Using Loops • Event-driven model – Manages the interaction between user and GUI by handling repetition for you • Designed with graphical user interface (GUI) • Predefined class called MessageBox – Used to display information to users through its Show( ) method using System; using System.Text; using System.Windows.Forms; public class HelloWorld { public static void Main() { MessageBox.Show("Hello World!"); } } Windows Applications Example using System; using System.Windows.Forms; class SquaredValues { static void Main( ) { int counter = 0; string result =""; while (counter < 10) { counter++; result += " \t“+ counter + " \t" + Math.Pow(counter, 2) + "\n"; } MessageBox.Show(result, “1 through 10 and their squares”); } } Windows Applications • To use MessageBox class in console application – Add a reference to System.Windows.Forms • View > Solutions Explorer • Right-click on the Reference folder – Select Add Reference – Look for and select System.Windows.Forms – Click OK – Add using directive to System.Windows.Forms namespace in program using System.Windows.Forms; MessageBox.Show( ) method is overloaded – may be called with different number of parameters First parameter – String displayed in window Second parameter– Caption for Window title bar Third parameter– Type of dialog button Fourth parameter– Button type MessageBox class MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question); MessageBox class (continued) MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question); static void Main() { bool moreData = true; Random number = new Random(); int s = number.next(100); while (moreData) { Console.WriteLIne(s); if (MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) // Test to see if No clicked { moreData = false; } else { s = number.Next(100); } } MessageBox.Show( ) Method 2nd parameter 1st parameter 4th parameter 3rd parameter MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);