Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.

Similar presentations


Presentation on theme: "1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different."— Presentation transcript:

1 1 Advanced Programming IF

2 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

3 Control Statements If, Else While For Foreach

4 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 );

5 Operators Precedence CategoryOperators Additive Add: + Subtract: - Shift Shift bits left: << Shift bits right: >> Relational Less than: < Greater than: > Less than or equal to: <= Greater than or equal to: >= Type equality/compatibility: is Type conversion: as

6 6 Decision Making: Equality and Relational Operators

7 Operators Precedence CategoryOperators Equality Equals: == Not equals: != Bitwise AND& Bitwise XOR^ Bitwise OR| Logical AND&& Logical OR||

8 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 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 10 Assignment Operators

11 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 12 Conditional Operator (?:) –Similar to an if/else structure –The syntax is: (boolean value ? if true : if false) Console.WriteLine ( studentGrade >=60? “Passed” : “Failed”);

13 13 Example Display one word to describe the integer value of number as “Positive”, “Negative”, or “Zero”

14 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

15 Statements switch 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 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 17 Example Compute the Grade of students as A, B, C, D, and F in a class depending on their scores

18 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.

19 Predefined Types char Escape sequence characters (partial list) CharMeaningValue \’ Single quote0x0027 \” Double quote0x0022 \\ Backslash0x005C \0 Null0x0000 \n New line0x000A \r Carriage return0x000D \t Tab0x0009

20 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.


Download ppt "1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different."

Similar presentations


Ads by Google