CC213 Programming Applications Week #2
Control Structures • Control structures –control the flow of execution in a program or function. • Three basi...
Control Structures • Control structures –control the flow of execution in a program or function. • Three basic control structures: Sequential Flow - this is written as a group of statements bracketed by { and }where one statement follows another. Selection control structure - this chooses between multiple statements to execute based on some condition. Repetition – this structure executes a block of code multiple times.
C
Y
N
Y
C N
2
C control structures • Selection if if . . . else switch
• Repetition for loop while loop do . . . while loop 3
Conditions • A program chooses among alternative statements by testing the values of variables. 0 means false Any non-zero integer means true, Usually, we’ll use 1 as true. if (a>=b) printf(“a is larger”); else printf(“b is larger”); • Condition - an expression that establishes a criterion for either executing or skipping a group of statements a>=b is a condition that determines which printf statement we execute. 4
Relational and Equality Operators • Most conditions that we use to perform comparisons will have one of these forms:
variable relational-operator variable e.g. a < b variable relational-operator constant e.g. a > 3 variable equality-operator variable e.g. a == b variable equality-operator constant e.g. a != 10
5
Relational and Equality Operators Operator
Meaning
Type
greater than
relational
=
greater than or equal to
relational
==
equal to
equality
!=
not equal to
equality 6
Logical Operators • logical expressions - expressions that use conditional statements and logical operators. && (and) A && B is true if and only if both A and B are true || (or) A || B is true if either A or B are true ! (not) !(condition) is true if condition is false, and false if condition is true This is called the logical complement or negation • Example (salary < 10000) || (dependents > 5) (temperature > 90.0) && (humidity > 90) !(temperature > 90.0) 7
Truth Table && Operator A
B
A && B
False (zero)
False (zero)
False (zero)
False (zero)
True (non-zero)
False (zero)
True (non-zero)
False (zero)
False (zero)
True (non-zero)
True (non-zero)
True (non-zero) 8
Truth Table || Operator A
B
A || B
False (zero)
False (zero)
False (zero)
False (zero)
True (non-zero)
True (non-zero)
True (non-zero)
False (zero)
True (non-zero)
True (non-zero)
True (non-zero)
True (non-zero) 9
Operator Table ! Operator
A
!A
False (zero)
True (non-zero)
True (non-zero)
False (zero)
10
Remember! • && operator yields a true result only when both its operands are true. • || operator yields a false result only when both its operands are false.
11
If-Else Syntax if ( Expression ) StatementA else StatementB
NOTE: StatementA and StatementB each can be a single statement, a null statement, or a block. 12
Example: 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.
Switch statement Used to select one of several alternatives BASED on the value of a single variable. This variable may be an int or a char but NOT a float ( or double).
15
Example char grade ; printf(“Enter your letter grade: “); scanf(“%c”, &grade); switch ( grade ) { case „A‟ : printf(“ Excellent Job”); break; case „B‟ : printf ( “ Very Good “); break; case „C‟ : printf(“ Not bad “); break; case „F‟ : printf(“Failing”); break; default : printf(“ Wrong Input “); } 16
Example: Light bulbs Write a program to ask the user for the brightness of a light bulb (in Watts), and print out the expected lifetime: Brightness Lifetime in hours 25 2500 40, 60 1000 75, 100 750 otherwise 0 17
int bright ; printf(“Enter the bulb brightness: “); scanf(“%d”, &bright); switch ( bright ) { case 25 : printf(“ Expected Lifetime is 2500 hours”); break; case 40 : case 60 : printf ( “Expected Lifetime is 1000 hours “); break; case 75 : case 100 : printf(“Expected Lifetime is 750 hours “); break; default : printf(“Wrong Input “); } 18
What is a loop? • A loop is to execute a set of instructions repeatedly until a particular condition is being satisfied . • That is, you can execute particular statements more than once in a controlled fashion
• Statements are executed as long as some condition remains true 19
Two Types of Loops count controlled loops repeat a specified number of times
event-controlled loops some condition within the loop body changes and this causes the repetetion to stop
20
While Statement SYNTAX
while ( Expression ) { . . . }
/*loop body */
NOTE: Loop body can be a single null statement, or a block.
statement, a 21
Parts of a While Loop • Every while loop will always contain three main elements: – Priming: initialize your variables. – Testing: test against some known condition. – Updating: update the variable that is tested. 22
• If we want to compute , we need to go i 1+2+3+...+100 i 1 • We can use a while loop. /* computes the sum: 1 + 2 + 3 + ....+ 100 */ #include int main(void) { int sum =0, i = 1; while (i