{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture 2-flow of control

# lecture 2-flow of control - Flow of Control True and False...

This preview shows pages 1–15. Sign up to view the full content.

1 Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 True and False in C False is represented by any zero value . The int expression having the value 0. The floating expression having the value 0.0. The null character ‘\0’. The NULL pointer (for pointer see chap. 8). True is represented by any nonzero value . A logical expression, such as a<b, is either true or false. This expression yields the int value 1 if it is true or the int value 0 if it is false .
3 Examples int i=1, j=2, k=3 ; double x=5.5, y=7.7, z=0.0 ; i < j-k i<(j-k) 0 -i+5*j >= k+1 ((-i)+(5*j))>=(k+1) 1 x-y <= j-k-1 (x-y)<=((j-k)-1) 1 x+k+7 < y/k ((x+k)+7)<(y/k) 0 i != j 1 !! 5 !(!5) 1 ! i-j+4 ((!i)-j)+4 2 x || i && j-2 x||(i&&(j-2)) 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Selection if switch
5 if statement Conditional Execution : if (Boolean expression) statement; else Where a statement may consist of a single statement, a code block, or empty statement. The else clause is optional.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 if – logic : if (Boolean Expression) statement_1; If-else-logic : if (Boolean Expression){ compound_1 } else{ compound_2 } Conditional execution allows you write code that reacts to tested conditions. No Semi-colon Yes- For a Semi-colon
7 Example #include <stdio.h> WHY? int main ( ) { double salary, payrate; int hours, union; printf (“Please enter hours worked, payrate”, “ and union status”);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 printf (“Enter 1 if a union member, 0 if not”); scanf (“%d%lf%d”, &hours, &payrate, &union); if (hours > 40 && union = = 1) salary = (40 * payrate) + ((1.5 * payrate) * (hours - 40)); else salary = payrate * hours; printf (“Salary is: \$ % 6.2 f”, salary);
9 Nested ifs Nested: One statement coded within another statement. Nested ifs: An nested if is an if that is the target of another if or else. Why Nested ifs? Needed to handle tasks where we have 3 or More options that are mutually exclusive. ANSI C specifies that at least 15 levels of nesting must be supported by the compiler.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 The if-else-if ladder Linear nested If General form: OLD STYLE if ( expression ) statement ; else if ( expression ) statement ; else if ( expression ) statement ; . . . else statement ;
11 The conditions are evaluated from the top downward . As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the conditions are true, the final else is executed . That is, if all other conditional tests fail, the last else statement is performed. If the final else is not present, no action takes place if all other conditions are false.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 E.g. You are a salesperson for the Widget Manufacturing Co. You will earn a salary bonus according to the following rules : Sales > = \$50,000 earn \$5,000 Sales > = \$100,000 earn \$15,000 Sales > = \$150,000 earn \$30,000 Example
13 double sales, bonus; printf (“please enter total sales”); scanf (“%lf”, &sales); if (sales < 50000) bonus = 0; else if (sales < 100000) bonus = 5000; else if (sales < 150000) bonus = 15000; else bonus = 30000;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
14 In a nested if, an else always refers to the nearest if that is within the same block as the else and that is not already associated with an else .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 56

lecture 2-flow of control - Flow of Control True and False...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online