+ All Categories
Home > Documents > 1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement...

1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement...

Date post: 02-Jan-2016
Category:
Upload: blaise-watkins
View: 219 times
Download: 0 times
Share this document with a friend
20
1 Advanced Programming IF IF
Transcript
Page 1: 1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.

1

Advanced Programming

IFIF

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

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

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

Control Statements

• If, Else

• While

• For

• Foreach

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

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

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

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

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

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

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

OperatorsPrecedence

Category Operators

EqualityEquals: ==Not equals: !=

Bitwise AND &

Bitwise XOR ^

Bitwise OR |

Logical AND &&

Logical OR ||

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

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.

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

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 %=

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

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

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

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

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

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

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

13

Example

Display one word to describe the integer value

of number as “Positive”, “Negative”, or

“Zero”

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

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

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

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

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

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.

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

17

Example

• Compute the Grade of students as A,

B, C, D, and F in a class depending on

their scores

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

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.

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

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

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

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.


Recommended