Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 2 - Selection and Repetition. Equality and Relational Operators.

Similar presentations


Presentation on theme: "Section 2 - Selection and Repetition. Equality and Relational Operators."— Presentation transcript:

1 Section 2 - Selection and Repetition

2 Equality and Relational Operators

3 Relational Operators int aValue = 100, bValue = 1000; string sValue = “CS158”; decimal money = 50.22m; double dValue = 50.22; char cValue = ‘A’;

4 Logical Operators

5 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.

6 Use round brackets ( ) to impose your own precedence on expression evaluation

7 Decision Structures - Selection 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 Same syntax for C++, C#, and Java

8 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");

9 if (salesForYear > ) { Console.WriteLine( ); Console.WriteLine(“YES...you get a bonus!”); bonusAmount = ; } if (hoursWorked > 40) { payAmount = (hoursWorked – 40) * payRate * payRate * 40; Console.WriteLine(“You worked {0} hours overtime.”, hoursWorked – 40); } else payAmount = hoursWorked * payRate;

10 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

11 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.

12 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; }

13 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; }

14 Loops while (Boolean expression) {statements} int x – 10; while ( x > 1 ) { Console.Writeline("x is "); Console.Writeline( x ); x--; } Same syntax for C++, C#, and Java

15 int sum = 0; int number = 1; while (number < 11) { sum = sum + number; number++; } Console.WriteLine(“Sum of values ” + “1 through 10” + “ is ” + sum);

16 do {statements} while (Boolean expression) int x = 10; do { Console.Write("x is "); Console.Writeline( x ); x--; } while (x !=1 );

17 Loops for (initialization; condition; in(de)crement) {statement;} for (int n=10; n>0; n--) { Console.WriteLine( n ); } Same syntax for C++, C#, and JAVA

18 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++) {... }

19 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 (int x in data) { sum += x; } foreach typevaluearray

20 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);

21 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

22 using System; using System.Text; using System.Windows.Forms; public class HelloWorld { public static void Main() { MessageBox.Show("Hello World!"); }

23 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”); }

24 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;

25 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

26 MessageBox class MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

27 MessageBox class (continued) MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

28 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); }

29 MessageBox.Show( ) Method 1 st parameter 4 th parameter 2 nd parameter 3 rd parameter MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);


Download ppt "Section 2 - Selection and Repetition. Equality and Relational Operators."

Similar presentations


Ads by Google