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

15 15 Example Compute the Grade of students as A, B, C, D, and F in a class depending on their scores

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

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

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

19 19 Example The Air Force has asked you to write a program to label aircrafts as military or civilian. Your program input is the plane’s speed and its estimated length. For planes traveling faster than 1100 km/hr, you will label those shorter than 52 m “military”, and longer as “Civilian”. For planes traveling less than 1100, you will issue an “aircraft unknown” statement.

20 Conditional Operator (?:) Conditional operator ( ?: ) takes three arguments –Ternary operator Syntax for using the conditional operator: expression1 ? expression2 : expression3 If expression1 is true, the result of the conditional expression is expression2 –Otherwise, the result is expression3

21 switch Structures switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector

22 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

23 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; }

24 Light bulbs Write a program to ask the user for the brightness of a light bulb (in Watts), and print out the expected lifetime: BrightnessLifetime in hours 252500 40, 601000 75, 100750 otherwise0

25 Program Write a C# program to calculate the average of three test grades and print out a report with the student’s ID number, average, and how well is the student progress. “Very Good” is a 70- point average or better, “Good” is an average between 60 and 70, and “Failing” is 50 point average or less.

26 Write a program that reports the content of a compressed-air cylinder based upon the first letter of the cylinder’s color. The program input is a character representing the observed color of the cylinder: ‘Y’ or ‘y’ for yellow, ‘G’ or ‘g’ for green and so on. Given: ColorContent OrangeAmmonia BrownCarbon Monoxide YellowHydrogen GreenOxygen


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