Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | blaise-watkins |
View: | 219 times |
Download: | 0 times |
1
Advanced Programming
IFIF
2
Control Structures
• Program of control
– Program performs one statement then goes to next line
• Sequential execution– Different statement other than the next one executes
• Selection structure
– The if and if/else statements
– The goto statement
• No longer used unless absolutely needed
• Causes many readability problems• Repetition structure
– The while and do/while loops
– The for and foreach loops
Control Statements
• If, Else
• While
• For
• Foreach
4
using System;
class Comparison
{
static void Main( string[] args )
{
int number1, number2;
number1 = Int32.Parse( Console.ReadLine() );
number2 = Int32.Parse( Console.ReadLine() );
if ( number1 == number2 )
Console.WriteLine( number1 + " == " + number2 );
if ( number1 > number2 )
Console.WriteLine( number1 + " > " + number2 );
OperatorsPrecedence
Category Operators
AdditiveAdd: +Subtract: -
ShiftShift bits left: <<Shift bits right: >>
Relational
Less than: <Greater than: >Less than or equal to: <=Greater than or equal to: >=Type equality/compatibility: isType conversion: as
6
Decision Making: Equality and Relational Operators
Operators Associativity Type () left to right parentheses * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality = right to left assignment
OperatorsPrecedence
Category Operators
EqualityEquals: ==Not equals: !=
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
8
Example
• Your city classifies a pollution index
– less than 35 as “Pleasant”,
– 35 through 60 as “Unpleasant”,
– and above 60 as “Health Hazard.”
– Display the correct description of the
– pollution index value.
9
Assignment Operators
• Assignment operators
– Can reduce code
• x += 2 is the same as x = x + 2
– Can be done with all the math operators
• ++, -=, *=, /=, and %=
10
Assignment Operators
Assignment operator Sample expression Explanation Assigns Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d - 4 1 to d
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g
11
Increment and Decrement Operators
• Increment operator– Used to add one to the variable– x++– Same as x = x + 1
• Decrement operator– Used to subtract 1 from the variable– y--
• Pre-increment vs. post-increment– x++ or x--
• Will perform an action and then add to or subtract one from the value
– ++x or --x• Will add to or subtract one from the value and then perform an
action
12
Conditional Operator (?:)
• Conditional Operator (?:)
– Similar to an if/else structure
– The syntax is:
• (boolean value ? if true : if false)
Console.WriteLine ( studentGrade >=60? “Passed” : “Failed”);
13
Example
Display one word to describe the integer value
of number as “Positive”, “Negative”, or
“Zero”
switch Statement
• Can branch on any predefined type (including string) or enum– User-defined types can provide implicit conversion
to these types
• Must explicitly state how to end case– With break, goto case, goto label, return, throw or continue
– Eliminates fall-through bugs
– Not needed if no code supplied after the label
Statementsswitch Statement
int Test(string label) { int result; switch(label) { case null: goto case “runner-up”; case “fastest”: case “winner”: result = 1; break; case “runner-up”: result = 2; break; default: result = 0; } return result;}
16
Example
• Write a program to ask a student for his grades in 3 exams ( each out of 50 ) , get their total and inform the student whether he passed or failed the course.
17
Example
• Compute the Grade of students as A,
B, C, D, and F in a class depending on
their scores
18
Write a program that calculates bills for customer of the Electricity company. There are 3 types of customers: residential (code R) , commercial (code C) , and Industrial (code I).
- For a code R customer, the bill is $10 plus $0.05 for each kilowatt used.
- For a code C customer, the bill is $1000 for the first 2000 kilowatt, and $0.005 for each additional kilowatt used.
- For a code I customer, the bill is $1000 if he used less than 4000 kilowatt, $2000 if he used between 4000 and 10000 kilowatt, or $3000 if he used more than 10000 kilowatt.
The inputs of the program should be the type of customer ( R, C or I) and the kilowatts used. The output should be the amount of money the customer has to pay.
Predefined Typeschar
• Escape sequence characters (partial list)
Char Meaning Value
\’ Single quote 0x0027
\” Double quote 0x0022
\\ Backslash 0x005C
\0 Null 0x0000
\n New line 0x000A
\r Carriage return 0x000D
\t Tab 0x0009
20
If--Else for a mail order
Write a program to calculate the total price of a certain purchase. There is a discount and shipping cost:
The discount rate is 25% and the shipping is 10.00 if purchase is over 100.00.Otherwise, The discount rate is 15% and the shipping is 5.00 pounds.