Section 2 - Selection and Repetition
description
Transcript of Section 2 - Selection and Repetition
![Page 1: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/1.jpg)
Section 2 - Selection and Repetition
![Page 2: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/2.jpg)
Equality and Relational Operators
![Page 3: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/4.jpg)
Logical Operators
![Page 5: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/6.jpg)
Use round brackets ( ) to impose your own precedence on expression evaluation
![Page 7: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/26.jpg)
MessageBox class
MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
![Page 27: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/27.jpg)
MessageBox class (continued)
MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
![Page 28: Section 2 - Selection and Repetition](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022050910/56815f0c550346895dcdcc2e/html5/thumbnails/29.jpg)
MessageBox.Show( ) Method
1st parameter
4th parameter
2nd parameter
3rd parameter
MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);