· Define new terms
· Describe truth tables and how they are used
· Explain how a program selects actions to follow
· Describe the differences between simple and complex decision structures
Introduction
The primary difference between a calculator and a computer is that a computer can compare pieces of information and take different actions depending on those values. In this chapter you will learn how the computer evaluates and compares different values. Decision-making statements are key tools for a programmer. In this chapter you will learn the C# commands required to specify what values to compare and what actions the computer will need to do based on those values.
Boolean Algebra
An English mathematician, George Boole invented Boolean algebra in the 1800’s. This form of algebra and the term Boolean is named after Boole. Computers use Boolean operators to compare values. The computer uses Boolean operators to see if the values are equal, not equal or if one value is greater then the other. Using Boolean algebra the computer looks at a formula and asks the question “is this true?” All Boolean formulas, like all Boolean variables, have the value of either True or False. Boolean formulas are more commonly called expressions. A Boolean expression is a group of values and comparisons that can be evaluated and determined to be either True or False.
Truth Tables
Boolean expressions are evaluated using truth tables. Truth tables are tables that show the possible results of an expression using all possible combinations. If two values are used then there are four possible combinations. Both values may be true, both values may be false or one value may be true and the other may be false. A truth table shows the possible values for each item in the expression in the first columns with the resulting evaluation for that combination in the last column. Figure 5-x AND Operation Truth Table shows the table for the AND operation.
|
Truth Table for AND operations |
||
|
T |
T |
T |
|
T |
F |
F |
|
F |
T |
F |
|
F |
F |
F |
Figure 5-x AND Operation Truth Table
The AND operation is True if, and only if, both values are true. For example, Americans who are older then 21 and who are male must register for the draft. Both cases must be true for a person to have to register. If either item is not true, if one is not male or is under 21 years old, a person does not have to register.
You can look at a truth table such as the one above and easily determine the value of an expression based on the particular values you are using in an expression.
The OR truth table, shown below in figure 5-x OR Table, looks very different. For an OR expression to be true only one of the values must be true. If both values are true the expression is true as well. Only if both values are false does the expression evaluate to false.
Truth Table for OR operations |
||
|
T |
T |
T |
|
T |
F |
T |
|
F |
T |
T |
|
F |
F |
F |
Figure 5-x OR Table
A typical OR expression may be opposite of an AND expression. For example, in English you might say “you do not have to register for the draft if you are female or are under 21 years of age.” If either case is true, you are female or you are under 21 even if male, you do not have to register. Only if both values are false do you have to register.
The NOT operation has the simplest of all tables. NOT uses the opposite of the value in a variable or expression. If an expression evaluates to True then the NOT of that value is False. The truth table for NOT is shown in figure 5-x NOT Truth Table.
Truth Table for NOT operations |
|
|
T |
F |
|
T |
F |
|
F |
T |
|
F |
T |
Figure 5-x NOT Truth Table
These tables can be combined and used to determine the correctness of Boolean expressions. For example, let us take the following sentence and develop a truth table for it. “People who have taken a drivers education course may get a drivers license at age 16 otherwise one must be over 18 to get a license”
There are three comparisons in this sentence: Taken a driver’s education course, over 16, over 18. Either both of the first two cases must be correct or the second case must be correct for a person to get a driver’s license. We will need several columns for this table; one column for each case and one for the results. We will need eight rows because there are three cases there are eight possible combinations (two to the third power).
First the table must be filled with the combinations. Then these combinations must be evaluated. The AND evaluation will be handled first. It will be helpful to add a column to our table to show the result of these evaluations. Once that is done then the OR evaluation can take place more easily.
Figure 5-x License Table shows all the possible combinations with their result.
|
Taken Course |
16 or over |
AND Result |
18 or older |
Result |
|
T |
T |
T |
T |
T |
|
T |
T |
T |
F |
T |
|
T |
F |
F |
T |
F |
|
T |
F |
F |
F |
F |
|
F |
T |
F |
T |
T |
|
F |
T |
F |
F |
F |
|
F |
F |
F |
T |
T |
|
F |
F |
F |
F |
F |
Figure 5-x License Table
Tables like this one provides a set of expected results that can be compared to the results a program produces to verify the program. Truth tables are very useful for helping to select data for testing purposes. You will want to determine how to test your program before you write it and a truth table can help to make sure all possible cases are tested.
Exercise 5-1
Create a truth table for the following set of statements.
“The Theater Arts program is looking for a boy over six feet tall with blond hair and for a girl with red hair to take part in our next play. If you fit one of these criteria please see Mr. Murphy in the Theater after school.”
Your table should show all possible combinations and their evaluation.
Boolean Expressions
Boolean expressions use a number of operators. If you have taken a class in algebra or pre-algebra you probably already know some of these operators. Some of these operators are different because of limitations of the computer keyboard. Boolean operators are written to compare items.
Here are some examples of questions used by Boolean operators: Which of two values is greater then the other? Are two values the same? Are they just different? Figure 5-1 summarizes the symbols or operators that C# uses to ask these questions.
|
== |
Equal to |
The double equal sign is used in logical expressions to ask if two values are the same. This is called the equality operator. If the values are the same, then the expression is true. The following expression is True because four is equal to two times two. 4 == (2 * 2) Remember that there is no space between the two equal signs. A single equal sign is the assignment operator and is completely different. If you use the assignment operator where the equality operator is expected the compiler will give you an error. |
|
> |
Greater than |
The Greater Than operator asks if the first value is greater (larger) than the second value. If the first value is greater than the second value, the expression is true. The following expression is true because 21 is a larger number than 18. 21 > 18 |
|
< |
Less than |
The Less Than operator asks if the first value is less (smaller) than the second value. If the first value is smaller than the second value, the expression is true. The following expression is true because 18 is less than 65. 18 < 65 |
|
>= |
Greater than or equal to |
The Greater than or equal to operator asks if the first value is greater than or the same as the second value. If the two values are the same or if the first value is bigger than the second value, the expression is true. Both following expressions are true because 65 is equal to 60 plus five and 70 is greater than 65. 65 >= (60 + 5) 70 >= 65 |
|
<= |
Less than or equal to |
This operator asks if the first value is less than or the same as the second value. If the two values are the same or if the first value is smaller than the second value, the expression is true. The first expression is true because 65 is less then 70. The second expression is true because 35 plus 30 equals 65. 65 <= 70 65 <= (35 + 30) |
|
!= |
Not equal |
The Not equal operator is a member of the equality operator class and asks if two values are the same. In this case, the expression is true only if the two values are different. The following expression is false because five is equal to two plus three. 5 <> (2+3)
There is no space between the exclamation mark and the equal sign. |
|
! |
Not |
The Not operator switches the value of a Boolean. The Boolean expression below is True if the value of bFlag is False because False and Not True are equivalent. bFlag = !True
In a comparison the Not operator has a similar purpose. The Boolean expression is True if the value of bFlag is False. Without the Not operator the True clause would be executed when bFlag was True. ! bFlag = True |
|
&& |
And |
The And operator is a way to join simple expressions and make more complicated decisions. Both sides of an And operation have to be true for the complete expression to be true. The expression below is true only if the value of myGender is female and myAge is 18. (MyGender == “female”) && (myAge == 18)
There is no space between the two ampersand characters. |
|
|| |
Or |
The Or operator is a way to join simple expressions and make more complicated decisions. If either side of an Or expression is true then the complete expression is true. The expression below is True if either the value of myGender is female or myAge is 18. If of myGender is female and myAge is 18, the expression is also True. (MyGender ==“female”) || (myAge == 18)
The two vertical lines are called pipes and there is no space between them. |
Figure 5-1 Relational and Logical Operators
The following table shows the order of precedence for the operators used in Boolean expressions. All operators in the same row have the same precedence and will be evaluated from left to right.
|
Category |
Operators |
|
Primary |
() |
|
Relational |
< > <= >= |
|
Equality |
== != |
|
Conditional AND |
&& |
|
Conditional OR |
|| |
Figure 5-2 Precedence Table
As you can see, parentheses are used to specify precedence in Boolean operators. All of the mathematical operators have a higher precedence then the Boolean operators but it is still a good idea to use parentheses to make the precedence clear and unambiguous.
Exercise 5-2:
In this exercise you are to play the role of the computer and evaluate some logical expressions. Assume that you have a number of integer variables. The variables have been set with the following initial values:
int A = 5;
int B = 6;
int C = 25;
int D = -1;
int E = 7;
Are the following Boolean expressions True or False?
A > B && B > E || C < A * B
A > B && (B > E || C < A * B)
A < B && (B > E || C < A * B)
A > B || (B > E || C < A * B)
B + D == A && B > E
B + D == A || B > E
C / E > A || D < 1
B > C && (C > E && D < E)
! (B > C) && (C > E && D < E)
! (B + D == A || B > E)
Simple C# Boolean Expressions
Some expressions can be very simple. Any Boolean variable all by itself is an expression. Very often programmers use Boolean variables as flags. A flag is a variable, often but not always a Boolean value, which is used to keep track of an event. Flags are often used to avoid making the same comparison repeatedly. The Not operator is often used with flag variables so that the same flag can be used to ask if a case has been met or if the case has not been met.
Here is an example of how using the Not operator can be useful with flags. Consider the problem of keeping track of which game player is allowed to move. The game will have two players. One player is player X. The second player is not player X. A Boolean variable, PlayerX, can be used to keep track of player turns. If the value of PlayerX is True then it is player X’s turn. If the value of PlayerX is false then it is the other player’s turn. You will need a statement that changes the value of PlayerX after each player takes their turn.
The following statement changes the value of PlayerX. If PlayerX was true this expression makes it false. If it was false, it will become true.
PlayerX = Not PlayerX;
The programmer does not have to know what the value of PlayerX is before the expression. Whatever the value was it will now be reversed.
Simple if Statement
The most common decision statement in C# is the If statement. This is the C# way of saying “if something is true then do this.” The reserved word If starts the statement. This is followed by a logical expression that is evaluated. If the expression is true, then the statement that follows the expression is executed. That is the simplest form of an If statement. It looks like this:
if (Age >= 18) Console.WriteLine( “You are old enough to vote!”);
This statement is a simple If statement. If the expression is true, in this case if the value in age is eighteen or higher, the statement at the end of the line, writing a message to the console in this case, is executed. If the expression is not true, if it is false, the program will just move on to the next line and ignore the statement at the end of the line. Several statements can be included between curly braces and they would all be executed if the statement evaluates to true.
if (Age >= 18)
{
Console.WriteLine( "You are old enough to vote!");
Console.WriteLine( "You are no longer a child.");
}
By using the braces to create a block you avoid having to use a group of if statements all with the same expression. Why evaluate an expression more then once if you can avoid it?
if/else Statements
Very often you want your program to do one thing if an expression is true and quite another thing if the expression is false. The reserved word else allows you to write a second clause to your if statement that will execute if your expression evaluates to false. For example, if you wish to check for age 18 and print a different message for people under age 18 you could write code like the following.
if (Age >= 18)
{
Console.WriteLine( "You are old enough to vote!");
Console.WriteLine( "You are no longer a child.");
}
else
Console.WriteLine("Sorry but you may not vote.");
As with the simple if statement, the else clause can use a block specified by a set of curly braces such as the example below.
if (Age >= 18)
{
Console.WriteLine( "You are old enough to vote!");
Console.WriteLine( "You are no longer a child.");
}
else
{
Console.WriteLine("Sorry but you may not vote.");
Console.WriteLine("Please come back in {0} year(s)",18 - Age);
}
Many programmers use the curly braces for both the if and else clauses even if there is only one statement. The braces make it more clear what is part of the if statement and what is not. This avoids confusion later on. It also makes it easier to add statements to a clause without worrying that it will not be executed properly. This helps avoid some common errors. Look at the following lines of code.
if (Age >= 18)
Console.WriteLine( "You are old enough to vote!");
Console.WriteLine( "You are no longer a child.");
From the way this code is indented you might think that the second Console.WriteLine will only print if Age is greater then 18. That would be incorrect. The first Console.WriteLine will only be executed if the expression is true, Age is greater then 18, but the second Console.WriteLine will always be executed. That is because without the curly braces to include multiple statements only the first statement after the expression is included in the if clause.
If you write more then one statement after an if clause and do not use curly braces then an else reserved word after the second or later statement will result in a compiler error. The Visual Studio IDE will automatically create a set of curly braces whenever you start an if statement.
Nested if Statements
There are times when one level of decision making requires a second level to make a final determination of what action to take. For example, suppose a school wants to have separate meetings for the boys and girls track teams. The girls are to meet in the cafeteria while the boys are to meet in the auditorium. This could be expressed in several ways. One way is to say, “all boys on the track team go to the auditorium for a meeting” and “all girls on the track team go to the cafeteria for a meeting.” In computer terms there are two if statements each with two compares. In C# code it might look something like the following:
If (MyGender = “Male” && TrackTeam == True) SendTo (“Auditorium”);
If (MyGender = “Female” && TrackTeam == True) SendTo (“Cafeteria”);
If we are using this criteria with 800 students that means there are always 3200 compares. Each student has their membership on the track team twice then their gender is checked once for male and once for female. A more efficient way would be to first check the track status and then check the gender only if the student is a member of the track team. This code might look like the following:
If (TrackTeam == True) {
If (myGender == “Male”)
SendTo (“Auditorium”);
else
SendTo (“Cafeteria”);
}
If we check this way we can take advantage of this nesting of if statements the total number of compares is greatly reduced. Each student is compared to track membership only once. Then each student is checked for gender. If there are 100 members of the track team then the gender will only be checked 100 times. As each student must still be checked for membership on the track team there will be 800 compares for that condition. This results in a total of 900 compares as opposed to 3200 compares with the less efficient program code.
C# does do some optimization of Boolean expressions. If the program can determine from the first compare in an expression that the expression will evaluate to a specific value the evaluation will stop immediately. For example, the following expression will evaluate to false no matter what the second set of values is if the value of MyGender is “Male.” In that case there is no need to do the second evaluation.
MyGender == “Female” && myAge > 18
If the value of MyGender is “Female” then the comparison with myAge must be tested to determine the final value of the expression.
This optimization is called short circuit evaluation because the evaluation is stopped or short circuited as soon as an evaluation can be concluded.
Short circuit evaluation can be used to make the track team assessment that was used earlier. The following code is more efficient then the example used earlier because the evaluation is short circuited, and the gender check is not made, if the track membership is false.
If (TrackTeam == True && MyGender = “Male”) SendTo (“Auditorium”);
If (TrackTeam == True && MyGender == “Female”) SendTo (“Cafeteria”);
Good programmers plan their expressions to take advantage of short circuit evaluation.
Program code can be nested very deeply. In other words, the level of if blocks inside other if blocks can get to be a large number. The compiler can handle this easily but this can get very confusing for a person reading the program. It can even make it more difficult to find problems. One solution to this problem is to take some of the inside blocks and make them into a separate function. Coding standards, at some companies, suggest that if nesting goes beyond three levels deep, the inner levels should become a new method. This means that the inside of one block will consist only of a call to this new method. This will make the code easier to read and simpler to debug.
The simple if statement when used with the else clause easily handles cases where an expression is only true or false. There are cases that are more complicated then a single if/else set can handle. Fortunately the else clause can also have an if statement. This if statement may also have an else clause. This allows a series of tests to take place.
You can see how this works when a program needs to convert numeric grades into letter grades. Lets take a simple case and use 90 and above to be an A, 80 to 89 to be a B, 70 to 79 is a C, 65 to 69 is a D and anything less then 65 is an F. These numbers could be used in a set of statements that check ranges like the lines below.
int grade = 89;
if (grade >= 90) Console.WriteLine("A");
if (grade >=80 && grade <= 89) Console.WriteLine("B");
if (grade >=70 && grade <= 79) Console.WriteLine("C");
if (grade >=65 && grade <= 69) Console.WriteLine("D");
if (grade < 65) Console.WriteLine("F");
These are a lot of statements and a lot of compares. The same result can be acquired with fewer statements and compares by using a serious of if statements in else clauses. The following lines of code demonstrate how this will work.
if (grade >= 90 )
Console.WriteLine("A");
else
if (grade >=80)
Console.WriteLine("B");
else
if (grade >= 70)
Console.WriteLine("C");
else
if (grade >= 65)
Console.WriteLine("D");
else
Console.WriteLine("F");
This code involves fewer compares because each compare is only executed if the statement before it is false. In other words, the check for greater then or equal to 80 only takes place if the value of grade is less then 90. In this case there is no need to check for 89 or less because the program would not get this far unless the value was that low.
This code does not have to do an explicit check for a value below 65 because any value above 65 will result in a message being displayed and the if block being exited. In this example, any grade below a 65 is an “F” so if the grade does not match any of the other values it falls to the default value. A default value is a value that is used if no specific value is indicated.
Having a default value, by ending a series of if/else statements with a final an else clause, prevents problems later on. An If block that the programmer expects to do something but does not handle an unexpected case can cause other code to work in unexpected ways.
1. Start Visual Studio and open a new Windows C# project.
2. Add two text boxes and a command button to the form. Clear the text parameter in the text boxes.
3. Change the text property of the form to identify the project. Change the text property of the command button to read “Grade.”
4. Enter the following code in the command button function. If you made name changes to objects you must make the same changes to the code below.
int grade = Convert.ToInt32(textBox1.Text);
if (grade >= 90 )
textBox2.Text = "A";
else
if (grade >=80)
textBox2.Text ="B";
else
if (grade >= 70)
textBox2.Text ="C";
else
if (grade >= 65)
textBox2.Text ="D";
else
textBox2.Text ="F";
}
5. Run the program and enter a number in the text box. Push the command button and notice the display in the picture box.
Things to Think About:
1. How does writing this code as a series of If/Else/If clauses make the program more efficient than a series of independent If statements?
2. How would the program be different if the grades were looked for in random order?
3. If you only use the greater than symbol and omit the equal in the If statements, how would the results be different?
4. What changes would you have to make for the program to use your school’s grading scale? Add more lines, change more lines, or both?
5. To test this program using every value from 0 to 100 would be too time consuming. It is easier to choose specific values to test the program. What specific values would you need to try?
6. What grade would be shown if a number greater then 100 was entered?
7. What number would be shown if a number less then zero was entered?
8. How might this program be changed to show a special message if a number above 100 or below zero was entered?
9. What grade is shown if nothing is entered in the text box? Why?
10. How can the program avoid data entry problems?
Switch Blocks
The Switch block is an alternative to complex nested if structures that involves a number of possible blocks of code to execute. It can be easier to read and understand then some complicated if/else/if structures.
Switch blocks use a single value, unlike an If structure which may use several values. The value is checked once, at the top of the structure. This value is then compared to values for individual cases that have been defined in the structure. If a match between the checked value and the value for a case is found, the group of statements associated with that case is executed. The cases to be checked must be specific values.
The switch block begins with the reserved word switch followed by a single variable or expression that evaluates to either a number or a string. The block begins and ends with a set of curly braces. In between the braces are a number of case statements. Each case has a value or expression. These values are compared to the value of the expression in the switch. If the value for a case is a match to the value in the switch then the statement or statements listed for that case are executed. The end of statements for case is ended with one of two possible statements. The break statement exits the switch block. The goto statement is used to check other cases in the switch block.
Once one case is matched and its statements executed the program moves to the code after the switch block or to the case named in a goto statement. If no cases, match then no code in the block is executed.
Switch also has a way to have things happen if no cases are matched. The default statement starts a block of code that will be executed if none of the other cases in the block are matched. The default statement belongs after all the other cases in the block. Below is a simple switch block with two cases and the default case.
switch (iMonth)
{
case 1:
sMonth = “January”;
break;
case 0:
sMonth = “TBD”;
goto case 1;
default:
sMonth = “not January”;
break;
}
This example shows the goto as well as the break statement as ways to end a block. In this case the “TBD” placed in the variable sMonth will be replaced by the “January” used in case 1.
Once a break statement is executed the program leaves the switch block. The goto statement will cause the program to execute the code for another case before exiting the switch block. The case the goto sends the program will always be executed. That case does not have to match the value in the switch statement. If this second block uses another goto statement then that target case will also be executed.
The goto statement must be used very carefully or the program can become confusing. In many cases, if you find yourself using the goto statement you may want to think about using a set of if blocks and not the switch block.
Switch blocks are particularly useful for dealing with menus. The following sample code shows a switch block being used the select a function to call based on a user selected option.
switch (userOption)
{
case 1:
case 2:
LogInUser(userOption);
break;
case 3:
LogOutUser();
break;
case 4:
PrintStatus();
break;
case 5:
GeneralLedger();
break;
case 6:
Payables();
break;
default:
Console.WriteLine("{0} is not a valid option", userOption);
break;
}
Notice that there is no statement between case 1 and case 2. This means that the same code will be executed if userOption is either 1 or 2. This is the only case where a break or got can be left out of a case block.
Testing
Computers do what a programmer tells them to do. If a programmer is not careful, what the programmer tells the computer may result in the computer misunderstanding the instructions. Testing is how the programmer makes sure their work is accurate. Programmers should always think about how to test their program as they write it.
Testing a program involves trying the program in a way the programmer knows what results to expect. If the program is doing a mathematical function, a programmer needs to know what answer should be given before those numbers are given to the program. It is not enough to get an answer; the answer must be the correct one.
When a careful programmer designs a logical expression, it should be tested on paper before it is put in the computer. Testing requires selecting a value for each variable in a formula and going through the expression carefully to determine if the expression evaluates to the expected result. Very often there are far too many possible values to test them all. This does not mean that an expression cannot be well tested. It just means that the right group of values must be tried. Planning is important.
When comparing two values, a good programmer always tests the following cases:
· The first value is greater
· The second value is greater
· Both values are the same
When an expression involves a range of values, between 18 and 21 for example, some extra values must be tested.
· A value in the middle of the range
· A value above the range
· A value below the range
· A value at the exact bottom of the range
· A value at the exact top of the range
Test values and how the computer should treat them should be planned before the expression is even written into a code window. When an expression involves more then one variable and more than one comparison the planning for testing becomes both more involved and more important. A plan for testing is part of the design of the program not an after thought. The question that good programmers ask themselves is “how will I know it works correctly?”
Logical expressions are mathematical expressions or formulas that are either true or false. They compare different values to see how they are related to each other. If the values are related in the particular way the formula asks, then the expression is true. If they are not related the way the program is checking them the value of the expression is false. Several simple expressions may be combined to form more complicated expressions by using the words And, OR and Not between each simple expression.
For an expression that compares simple expressions with the word And, both simple expressions must evaluate to be true for the larger expression to also evaluate as true. The word Or creates an expression that is true if either or both of the simple expressions are true. The word Not is used to reverse the value of an expression. Not will make a true expression false or a false expression true.
If statements are basic decision making statements. A simple If statement evaluates one expression. If the expression is true, one statement is executed. If the expression is false then the program moves on to the next statement.
A block If statement uses a set of braces to identify several statements. If the expression is true, the program will execute the statements that are included inside the braces. There may be one or many statements in the block. Any time a programmer wants to execute more then one statement in a block, the statements must be included inside a set of braces. Statements outside the braces, even if indented as other statements, are not part of the block.
A programmer who needs to have specific statements executed when an expression is false as well as when it is true must use the else statement. The else statement follows either a single statement or the block of statements that will be executed if the if test is true. The else clause may be a single statement or it may be a block of statements inside a set of braces.
Any type of program statement may appear inside an If block including other If blocks. Programmers must be careful to organize nested If blocks because they can not over lap. A block that starts inside one block must end in the same block.
The switch block is an alternative to sets of nests If statements. The switch statement evaluates only one value. This value is compared to any number of cases set up for the block. A case may include only a single value but several cases may execute the same code. A set of statements inside a case block must be terminated using either the break statement or the goto statement. Switch block may be nested inside if blocks and if blocks may be nested inside switch blocks.
Programmers should prepare test values for all expressions. The programmer must know what values they will use and what result to expect. If the programmer does not know if the result they get is the right one then they have not properly planned and they have no way of knowing if their program is correct.
1. What is the value of Not True?
2. How many possible values can a Boolean expression have?
3. How many statements can there be between an if statement and an else statement without using braces? How many if braces are used?
4. How is the beginning and ending of an if block indicated?
5. Does the expression in a nested if statement have to use the same variables as the outside if statement?
6. How many values are evaluated in the switch statement?
7. If the value in a switch statement matches more then one case value which code block or blocks are executed?
8. Which clause in an if/else block is executed if the expression is found to be false?
9. What is the primary difference between a calculator and a computer?
10. If no code is written for a case statement, what happens when that case is matched?
Angle Brackets The symbols ( < > ) are used to indicate greater than and less than comparisons.
Block A group of program statements that are grouped together. Any group of statements enclosed in a set of curly braces is a block.
Boolean Algebra A logical system that uses the values true and false and operators that compare values to determine a decision.
Boole, George An English mathematician in the 1800’s who invented a logic system now known as Boolean Algebra.
Braces These symbols, ( { } ) also called curly braces, are used to enclose the statements in a program block.
Default The value or operation that the computer uses if no other value or operation is given is called the default.
Expression A Boolean expression is a group of values and comparisons that can be evaluated and determined to be either true or false.
Flag A flag is a variable that is used to keep track of an event or condition. It is often set as a result of a complicated set of expressions so that those expressions do not have to be repeated.
Nest Nesting code means that one block of code is placed inside another block of code. C# allows a programmer to nest code several layers deep.
Projects
|
Cars |
Buses |
Trucks |
|
$2 the first hour |
$2.50 the first hour |
$3.00 the first hour |
|
$1 for each additional hour |
$1.25 for each additional hour |
$2 for each additional hour |
|
No more then $14.00 for 24 hours |
No more then $17.50 for 24 hours |
No more then $27.00 for 24 hours |
o Paper covers rock (a person showing paper beats a person showing rock)
o Scissors cuts paper (a person showing a scissors beats a person showing paper)
o Rock breaks scissors (a person showing a rock beats a person showing scissors)
o If both players show the same sign it is a tie.
Write a program that lets users pick which item they want to show and then announces a winner. This may be done using a Windows program that hides each player's selection until the winner is announced.
5. Write a guessing game. The program should randomly select a number between one and one hundred. Allow the player to guess the number. Tell the player if their guess is higher or lower than the number they are trying to guess. Count the number of guesses the player takes to find the number.