section 2 - selection and repetition

29
Section 2 - Selection and Repetition

Upload: lynsey

Post on 15-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Section 2 - Selection and Repetition. Equality and Relational Operators. int aValue = 100, bValue = 1000; string sValue = “CS158”; decimal money = 50.22m; double dValue = 50.22; char cValue = ‘A’; . Relational Operators. Logical Operators. C# Operators - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Section 2 -  Selection and Repetition

Section 2 - Selection and Repetition

Page 2: Section 2 -  Selection and Repetition

Equality and Relational Operators

Page 3: Section 2 -  Selection and Repetition

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

Page 4: Section 2 -  Selection and Repetition

Logical Operators

Page 5: Section 2 -  Selection and Repetition

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.

Page 6: Section 2 -  Selection and Repetition

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

Page 7: Section 2 -  Selection and Repetition

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

Page 8: Section 2 -  Selection and Repetition

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

Page 9: Section 2 -  Selection and Repetition

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;

Page 10: Section 2 -  Selection and Repetition

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

Page 11: Section 2 -  Selection and Repetition

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.

Page 12: Section 2 -  Selection and Repetition

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

Page 13: Section 2 -  Selection and Repetition

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

Page 14: Section 2 -  Selection and Repetition

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

Page 15: Section 2 -  Selection and Repetition

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

Page 16: Section 2 -  Selection and Repetition

do {statements}

while (Boolean expression)

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

Page 17: Section 2 -  Selection and Repetition

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

Page 18: Section 2 -  Selection and Repetition

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

Page 19: Section 2 -  Selection and Repetition

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

type value array

Page 20: Section 2 -  Selection and Repetition

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

Page 21: Section 2 -  Selection and Repetition

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

Page 22: Section 2 -  Selection and Repetition

using System;using System.Text;using System.Windows.Forms;

public class HelloWorld { public static void Main() { MessageBox.Show("Hello World!"); } }

Page 23: Section 2 -  Selection and Repetition

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

Page 24: Section 2 -  Selection and Repetition

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;

Page 25: Section 2 -  Selection and Repetition

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

Page 26: Section 2 -  Selection and Repetition

MessageBox class

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

Page 27: Section 2 -  Selection and Repetition

MessageBox class (continued)

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

Page 28: Section 2 -  Selection and Repetition

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

Page 29: Section 2 -  Selection and Repetition

MessageBox.Show( ) Method

1st parameter

4th parameter

2nd parameter

3rd parameter

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