Intro Week 3

esse quam videri
Jump to: navigation, search

Variables

Scope

scope defines the life of a variable. Local Variables

Variables declared with functions/methods are only around as long as the function/method is running. These variables are known as local varables

Global Variables

Global variables are variables that are around for the entire length of execution. They are declared a public within the programs base class.
In c# since everything must be contianed in a class global variables do not really exist.

Block Level Variables

Block level variables exitist with the code block.
A code block is contained by { }.

Scope.cs


Assignment

The Assigment operator in c# is the '=' sign. You can assign variables like this...

int myVar; //declare varible of type int called myVar
myVar = 15;   //assign myVar the value of 15 using the '=' sign

notice that the equals sign doesn't test for equality.

Branching

  • if
  • switch

If example

int MyInt = 7;

if( MyInt > 10)
{
  //Do this if MyInt is greater than 10
}

Multiple Ifs

If Age >= 18
  Eligibility = Yes
Else If Age = 15
  Eligibility =Maybe
Else
  Eligibility No
bool Eligible = false;  //set the bool to false

Comparison Operators

Operator Description
== Equality
< Less Than
<= Less than or equal to
> Greater than
>= Greater than or equal to
!= Inequality

Logical Operators

Operator Description
&& Logical AND
II Logical OR (note the II are really pipes)
! Logical NOT

Boolean Expressions

Logical operators usually work on bollean expressions. Boolean expressions are is a statement that evaluates to true of false

(1 < 2)
or
(y == x)

Boolean expressions are often grouped with logical operator to combine comparisons

(x > 0) && (x <100) //x is greater than 0 and less than 100
(x < 0) || (x >100) //x is less than zero or x is greater than 100

Logical operator Precedence

  1.  ! (NOT)
  2. && (AND)
  3. || (OR)

In class

Build a month namer. Ask the user to input a month number and our program will output the month name. How many ifs are we going to need?

Branching Statements

if

syntax

if (expression)
    // statement
    
if (expression) {
    // statements
    // statements
}
if (expression) {
    // statements
    // statements
}
else {
    // statements
}

switch

syntax

switch (expression)

{
   case constant-expression:
           statement
           jump-statement
   [default: statement]
}

example 1

// switch with integer type
switch (myInt)
{
    case 1:
            Console.WriteLine("Your number is {0}.", myInt);
            break;
    case 2:
            Console.WriteLine("Your number is {0}.", myInt);
            break;
    case 3:
            Console.WriteLine("Your number is {0}.", myInt);
            break;
    default:
            Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
            break;
}

In class

Redo month namer using a swtch rather than ifs

Home work

Write a program for converting student’s score into a letter grade. The program should ask to input a student score (between 0 and 100) then the program will display the grade earned by that score. Grade Points

A 90-100

B 80-89

C 70-79

D 60-69

F 59 or less