You are on page 1of 21

Chapter 4: Logic & File I/O

Logic & File I/O


What have we been doing

 Simple "sequential" programs


– The program starts at the beginning and goes to
the end.
– No choices, branches, decisions, just top to
bottom
Control Structures - Selection
 Statements that evaluate an expression and then perform
one action if the expression is true and another if the
expression is false
 The main way of doing this is with an "if" statement
 Some Examples (in pseudo code)
 If hours is not equal to 0 then
The employee’s Hourly_Rate is Gross_Pass / hours
 If hours less than or equal to 40 then
Employee_Pay = Hourly_Rate * Hours
Otherwise
Employee_Pay = Hourly_Rate * Hours
 If weather is raining then
Take your umbrella with you
Logical Boolean Expressions
Relational Operators

 A "logical Boolean expression" is a C# statement that has a value of


either true or false
 Boolean Expressions compare two or more variables/objects and
determine the relationship between the variable.
 Comparisons are based on relational operators:
– == equals != not equal
– < less than <= less than or equal
– > greater than >= greater than or equal
 Notice that "equal to" is TWO equal signs
– a = b is as assignment statement. The value of b is stored in address b.
– a == b is a conditional statement. If a is the same as b, the statement
evaluates to true otherwise false
Logical Boolean Expressions
Simple Data Types - Char

 Comparison of Char values is done based on the


Unicode value of the character.
– For the United States, this will really match the ASCII
character codes.
– Notice that lower case letters are "greater than" upper case
letters!!
– Also realize that the CHAR of a number is way different
than the number itself
 ‘8’ == 8 ?
– This will be allowed since (remember) we can do math with
chars, but what is the result?
ASCII Character Chart
Logical Boolean Expressions
Strings

 Strings are compared character by character using


the ASCII/Unicode codes we just looked at.
 The only direct operator available though is "==". As
soon as there is a difference between the two
strings, that character is going to determine the
logical relationship between the two strings.
– "Bee" == "Before" ??
 The determination is made on the third character. "e" Not = "f"
– What About "BEFORE" and "Before"??
Logical Boolean Expressions
Strings

 To determine if one string is "Greater Than" another,


they wrote a static method called String.Compare
– int nCompare = String.Compare("Be","Before")
– String.Compare returns 0 if the two strings are the same, 1
if string1 is "bigger than" string 2, and -1 if string1 is "less
than" string 2
– When one string is a substring of another starting with the
left character, the longer substring is "greater than" the
shorter one.
– This whole mess of "greater" or "less" probably doesn’t
make sense when playing with Strings, but who knows.
Logical Boolean Operators
 Logical Expressions can be connected together with Logical
Operators.
– ! is not – reverse the Boolean value of an expression (unary
operator)
– && is and – The entire expression is true if and only if both sides
of the && are true
 (a > b) && (a > c) implies than
– Both b and c are less than a
 What does
(a >b) && (a==b)
imply?
– || is or – The entire expression is true if the left side is true or the
right side is true
 (a > b) || (a > c) implies that
– a is greater than b or a is greater than c or a is greater than both of them
New Order of Precedence
with Logical Operators

1. Unary operators (- + ! ++ --)


2. Multiplication/Division (/ * %)
3. Addition/Subtraction (+ -)
4. Relational GT/LT(< <= > >=)
5. Relational Equals (== !=)
6. And (&&)
7. Or (||)
8. Assignment (=)
 The syntax for "if" statements in C# is:
– if (logical boolean expression)
– {
 C# commands
– }
– Else
– {
 More C# Commands
– {
 A "logical Boolean expression" is a C# statement
that has a value of either true or false.
Let’s Look At The Simple Case

int nScore = 85;


char sGrade = ‘F’;
if (nScore > 79)
sGrade = ‘B’;
Console.Writeline ("Your Grade Is: {0}",sGrade);
In this case, the boolean expression is
nScore > 79
once the comparison is done, either a value of true or false will be
returned. If the value is true, the next statement is executed. If
it is false, the next statement will be skipped.
if..else vs. if..else if..else
if (nBalance > 50000.00) if (nBalance > 50000.00)
{nInterestRate = 0.07; }
else {nInterestRate = 0.07; }
if (nBalance >= 25000.00) else if (nBalance >= 25000.00)
{nInterestRate = 0.05; } {nInterestRate = 0.05; }
else
if (nBalance >= 1000.00) else if (nBalance >= 1000.00)
{nInterestRate = 0.03;} {nInterestRate = 0.03;}
else
else
{nInterestRate = 0.00; }
{nInterestRate = 0.00; }

Both series of statements do exactly the same thing, but the one
on the right is easier to read and understand with less
indentation.
Switch Statement
(Another way to do if…else if)

 Allows one to assign an action to separate


values of variable
 Includes a default case used when none of
the other cases are appropriate.
Basic switch statement
switch (expression)
{
case value1;
statements
case value 2;
statements
case value n;
statements
default;
statements
}

 Although multiple statements can be placed after each case statement without
requiring curly braces, each case statement must end with the break command
OR the next command will also be executed until a break is hit.
Typical switch command
switch (nGradePoints/10) case 7:
{ Console.WriteLine ("gotta C");
case 0: break;
case 1: case 8:
case 2: Console.WriteLine("gotta B");
case 3: break;
case 4: case 9:
case 5: case 10:
Console.WriteLine ("You Failed"); Console.WriteLine("gotta A");
break; break;
case 6: default:
Console.WriteLine ("gotta D"); Console.WriteLine ("bad grade");
break; break;
}
The switch expression can be

 an integer
 a char
 a String
 a boolean
– but check for values of true or false in the case statements
– probably really doesn’t make sense to switch on this
 an expression that resolves to one of these
File I/O
StreamWriter and StreamReader

 C# makes use of two classes that are part of


the System.IO library
– StreamWriter has the ability to write information
out to a file on disk.
– StreamReader has the ability to read information
in from disk.
StreamWriter
 To write information to a file, we first need to associate it with a
variable in our program:
– String sFileName = "DemoFile.txt";
– StreamWriter ioOutFile = new StreamWriter(sFilename);
 A word about the new crap
 Once we have it created, we write to it just like we would to the
console, except instead of using the noun "Console" we use the name
we gave the stream:
– ioOutFile.WriteLine("{0} {1}","One","Two");
– ioOutFile.WriteLine("{0} {2}","Three","Four");
– ioOutFile.WriteLine("{0} {3}","Five","Six");
 When you are done writing you close the file:
– ioOutFile.Close();
 Let’s do this and see what happens.
StreamReader
 Once a file exists, one can open it and read it in. Unfortunately, C#
doesn’t have a nice input conversion ability like C and C++
– String sFileName = "DemoFile.txt";
– StreamReader ioInFile = new StreamReader(sFilename);
 Let’s just read the file in that we just made and display it on the
Console:
String sInLine;
while ((sInLine = ioInFile.ReadLine()) != null)
{
Console.WriteLine(sInLIne);
}
ioInFile.Close()

You might also like