Intro Week 3

esse quam videri
Revision as of 18:28, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
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...

1 int myVar; //declare varible of type int called myVar
2 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

1 int MyInt = 7;
2 
3 if( MyInt > 10)
4 {
5   //Do this if MyInt is greater than 10
6 }

Multiple Ifs

1 If Age >= 18
2   Eligibility = Yes
3 Else If Age = 15
4   Eligibility =Maybe
5 Else
6   Eligibility No
1 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

 1 if (expression)
 2     // statement
 3     
 4 if (expression) {
 5     // statements
 6     // statements
 7 }
 8 if (expression) {
 9     // statements
10     // statements
11 }
12 else {
13     // statements
14 }

switch

syntax

switch (expression)

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

example 1

 1 // switch with integer type
 2 switch (myInt)
 3 {
 4     case 1:
 5             Console.WriteLine("Your number is {0}.", myInt);
 6             break;
 7     case 2:
 8             Console.WriteLine("Your number is {0}.", myInt);
 9             break;
10     case 3:
11             Console.WriteLine("Your number is {0}.", myInt);
12             break;
13     default:
14             Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
15             break;
16 }

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