Download presentation
Presentation is loading. Please wait.
1
C# and the .NET Framework
More Control Structures and Types
2
Conditional Operators
3
Evaluating an example of a conditional AND expression
4
Evaluating an example of a conditional OR expression
5
Evaluating a logical complement expression
6
Evaluating a conditional AND expression
7
Evaluating a condition OR expression
8
relational < > <= >= equality == != conditional AND &&
Highest NOT! ! multiplicative * / % additive relational < > <= >= equality == != conditional AND && conditional OR || assignment = += -= *= /= %= Lowest Operator precedence*
9
If-else statement to choose between two alternatives
if (score >= 60 && score < 80) Console.WriteLine ("Score " + score + " receives a C"); else ("Score " + score + " receives a B or an A"); If-else statement to choose between two alternatives
10
Nested if-else statement to choose among three alternatives
if (score >= 60 && score < 80) Console.WriteLine("Score " + score + " receives a C"); else if (score >=80 && score < 90) Console.WriteLine("Score " + score + " receives a B"); else Console.WriteLine("Score " + score + " receives an A"); Nested if-else statement to choose among three alternatives
11
if (score >= 60 && score < 80)
Console.WriteLine("Score " + score + " receives a C"); else if (score >= 80 && score < 90) Console.WriteLine("Score " + score + " receives a B"); else if (score >= 90 && score <= 100) Console.WriteLine("Score " + score + " receives an A"); Improved version
12
Nested if format if ( Is it the first alternative? ){
First alternative code } else if ( Is it the second alternative? ) { Second alternative code ... else if ( Is it the last alternative? ) { Last alternative code else { Code when none of the above alternatives is true
13
Flow chart for nested if-else statements
True Test1? Test1 true code False True Test2? Test2 true code False ... False True Last? Last true code False Last false code Flow chart for nested if-else statements
14
Incorrect attempt to pair an else with an if
if (score >= 60) if (score >= 80) Console.WriteLine("You got a B or an A"); else Console.WriteLine("You got a D or an F"); // Wrong pairing // else does not pair with first if Incorrect attempt to pair an else with an if
15
Corrected pairing of else and if
if (score >= 60) if (score >= 80) Console.WriteLine("You got a B or an A"); else Console.WriteLine("You got a C"); // Correct pairing // Pair else with nearest if Corrected pairing of else and if
16
rewritten as an if-else with nested if
if (score >= 60) { if (score >= 80) Console.WriteLine("You got a B or an A"); } else // Paired to first 'if' Console.WriteLine("You got a D or an F"); rewritten as an if-else with nested if
17
switch format switch (test_expression) { case expression1: statement1;
break; case expression2: statement2; ..... default: default_statement; }
18
An example of a switch statement
switch(mark) { case 0: case 1: case 2: case 3: case 4: System.out.println("F"); break; case 5: System.out.println("D"); case 6: case 7: System.out.println("C"); case 8: System.out.println("B"); case 9: case10: System.out.println("A"); default:System.out.println("Incorrect score"); } An example of a switch statement
19
for (initialize; test; update) for_statement int sum = 0;
for (int i = 1; i <= 4; i++) sum += i; A for statement pattern and example for the sum
20
Trace of execution of the for loop
initialize i = 1 test 1 <= 4 is true execute body sum += 1 (result: sum = = 1) update i++ (result: i = 2) 2 <= 4 is true sum += 2 (result: sum = = 3) (result: i = 3) 3 <= 4 is true sum += 3 (result: sum = = 6) (result: i + 4) 4 <= 4 is true sum += 4 (result: sum = = 10) (result: i = 5) 5 <= 4 is false Trace of execution of the for loop
21
A for statement for the sum 1+3+5+7+9
int sum = 0; for (int i = 1; i < 10; i += 2) sum += i; A for statement for the sum
22
A for statement for the sum 4+3+2+1
int sum = 0; for (int i = 4; i >= l; i--) sum += i; A for statement for the sum
23
Declaring an index variable before the for loop
int i; // declare loop index int sum = 0; for (i = 4; i >= 1; i--) // initialize loop index sum += i; ... i += 17; // use variable i Declaring an index variable before the for loop
24
Syntax for the do statement
while (condition) ; Syntax for the do statement
25
Pseudocode for example 4.5 enhancement
Compute balance as in Example 4.5 Ask the user -- Repeat or Quit? } while (User chooses to repeat); Pseudocode for example 4.5 enhancement
26
char type Represents characters Use single quote ‘a’, ‘B’
Internally 16 bits ASCII table in appendix Unicode represents thousands of characters We use ASCII for English \ is the escape character, gives special meaning to next character
27
Escape sequences for special characters
Meaning \n newline, move to the start of the next line \t tab \b backspace \r return, move to the start of the current line \" double quote \\backlash Escape sequences for special characters
28
Additional primitive types
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 long bigNumber = L; sbyte, byte, short, ushort -- integer types, special uses float -- decimal type, 7 place accuracy float size = 4.56F; uint, ulong, decimal
29
Enumerations Provides convenient names for values
enum Color {Red, Green, Blue} Declare variables Color c = Color.Red; Test if ( c == Color.Green) // do something
30
Powers and Roots Math.Pow(2.0,3.0) returns 8.0 // 2 cubed
Math.Pow(3.0,2.0) returns 9.0 // 3 squared Math.Sqrt(2.0) returns Math.Sqrt(16.0) returns 4.0
31
Maximum and Minimum Math.Max(3, 4) returns 4
Math.Min(3, 4) returns 3 Math.Min(17.32, ) returns Math.Min(-9, -11) returns -11
32
Absolute value Math.Abs(x) = x, if x >= 0 = -x, if x < 0
Math.Abs(-10) returns 10 Math.Abs(12.34) returns
33
Floor and Ceiling Math.Floor(25.194) returns 25.0
Math.Ceiling(25.194) returns Math.Ceiling( ) returns
34
Trigonometric and Exponential
Math.Sin( Math.PI ) returns Math.Cos(Math.PI) returns Math.Tan(Math.PI/4) returns 1 Math.Exp(1.0) returns Math.Exp(2.0) returns Math.Log(Math.E) returns 1 Math.Log(10.0) returns
35
Pattern for a menu-driven application
do { choice = IO.GetInt("Choose: \n" + "1. Convert from meters to yds,ft,in \n" + "2. Convert from yds,ft,in to meters \n" + "3. Quit: "); int choice = Io.readInt("Enter your choice, 1, 2 or 3"); switch (choice) { case 1: MetricToEnglish(); break; case 2: EnglishToMetric(); case 3: Console.WriteLine("Bye, Have a nice day"); } } while (choice != 3); Pattern for a menu-driven application
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.