Midterm review Flashcards
Using Directive
- Using System
- tells the compiler where to look for a class that’s used in this app
- Each using directive identifies a namespace containing predefined classes that a C# app should be able to use
White space
- space bar, tab, blank lines
- Ignored by the compiler
- make code easier to read
Class Name Convention
- begin with a capital letter and capitalize the first letter of each word they include
- a series of characters consisting of letters, digits and underscores (_) that does not begin with a digit and does not contain spaces
Write
- cursor stays on the same line of text in the console window
WriteLine
- cursor moves to the next line of text in the console window
escape sequence
- \n, \t, \r, ", \
\n
- New line. Positions the screen cursor at the beginning of the next line
\t
- Horizontal tab. Moves the screen cursor to the next tab stop
\r
- Carriage return. Positions the screen cursor at the beginning of the current line—does not advance the cursor to the next line. Any characters output after the carriage return overwrite the characters previously output on that line.
"
- Double quote. Used to place a double-quote character (“) in a string—e,g., Console.Write( “"in quotes"” ); displays “in quotes”.
\
Backslash. Used to place a backslash character in a string.
declare a class named “Addition”
public class Addition
declare the main method
public static void Main(string [ ] args)
{
}
declare “number1” as an integer data type
int number1;
initialize number1 as an integer data type equal to 10
int number1 = 10;
convert “number1” to an Integer data type
number1 = Convert.ToInt32( Console.ReadLine ( ) );
Integer data types
- byte
- short
- int
- long
floating point data types
- float
- double
- decimal
char data type
- one character - “A”
Bool data type
- true / false
- yes / no
String data type
“my name is Krista”
Simple data types
- int
- float
- char
- bool
complex data type
string
=
- assignment oppertator
- reads right side first
Arithmetic Operators
- Addition ( + )
- Subtraction ( - )
- Multiplication ( * )
- Division ( / )
- Remainder ( % ) -> Modulas
Relational Operators
> , =, <=
higher Precedence
Equality Operators
== , !=
lower Precedence
get accessors
- reading the values of a variable
set accessors
- string values in a variable
create a Gradebook object of a Gradebook class in another class
- Gradebook mygb = new Gradebook( );
Call Gradebook’s DisplayMessage method
myGradebook.DisplayMessage( );
new class all together
public class GradeBookTest {
public static void Main( string[] args ) { GradeBook myGradeBook = new GradeBook();
myGradeBook.DisplayMessage(); } }
local variables
- Variables declared in the body of a method
- can be used only in that method
instance variables
- When each object of a class maintains its own copy of an attribute
- each object (instance) of the class has a separate instance of the variable
public property
public class gradebook
constructor
- if you dont declare one it will automatically be assigned
- public GradeBook( string cname )
{
CourseName = cname;
}
constructor characteristics
- same name as class name
- no return type
- main method
auto implemented property
public string CourseName { get; set; }
selection
- if (one option)
- if / else (two options)
- switch (more than two options)
looping
- while (counter and sentinel controlled)
- for
counter
int counter = 1
While (Counter <= 22)
{
functions
counter = counter + 1
}
if selection statement
if ( grade >= 60 )
Console.WriteLine( “Passed” );
if / else double selection statement
if ( grade >= 60 )
Console.WriteLine( “Passed” );
else
Console.WriteLine( “Failed” );
Conditional Operator
- an be used in place of an if / else statement
- Console.WriteLine( grade>=60 ? “Passed”:”Failed” );
Nested if / else Statements
if ( grade >= 90 ) Console.WriteLine( "A" ); else if ( grade >= 80 ) Console.WriteLine( "B" ); else if ( grade >= 70 ) Console.WriteLine( "C" ); else if ( grade >= 60 ) Console.WriteLine( "D" ); else Console.WriteLine( "F" );
while loop
int product = 3;
while ( product <= 100 )
product = 3 * product;
Arithmetic compound operators
counter = counter + 1
counter += 1
++ counter
counter ++
increment operators
- prefix increment ( ++a )
- postfix increment ( a++ )
decrement operators
- prefix decrement ( –a )
- postfix decrement ( a– )
casting
double avg = (double) total / numofgrades
for
for ( int counter = 1; counter <= 10; ++counter )
Console.Write( “{0} “, counter );
for ( initialization; loopContinuationCondition; increment ) statement
while
initialization;
while ( loopContinuationCondition ) {
statement
increment; }
switch
switch ( grade / 10 ) { case 9: case 10: \++aCount; break; case 8: \++bCount; break; ... default: \++fCount; break; }
continue statement
skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
Logical Operators
- enable you to form more complex conditions by combining simple conditions
- The logical operators are && (conditional AND), || (conditional OR), & (boolean logical AND), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR) and ! (logical negation)
syntax error
- user typed something in wrong
logical error
- compiler will not catch it, but the code will not run correctly
run time error
- the user typed something in wrong
system
- namespace in the .Net library
class
a definition of things (blueprint)
format specifier
{ 0:F } - round to two decimal places
{ 0:C } - formats as currency
static
the code can run even with no object