Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 4 Simple Selections and Repetitions. 2 4.1 INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make.

Similar presentations


Presentation on theme: "1 Chapter 4 Simple Selections and Repetitions. 2 4.1 INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make."— Presentation transcript:

1 1 Chapter 4 Simple Selections and Repetitions

2 2 4.1 INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make decisions as to what code to execute and when (that is, selection) and the ability to execute certain portions.

3 3 4.2 PROGRAMMING FOR SIMPLE SELECTIONS if gross income is greater than $10,000 compute city_tax = 0.0175 * gross_income else set city_tax to 0 end_if

4 4 Two-Way Selection Using the if Statement selection expression must be enclosed in parentheses. in C a nonzero value is assigned to a predicate if it is true ; otherwise, its value will be zero. C considers any nonzero value compute for the selection expression in if statements to correspond to the Boolean true and the value zero to correspond to the Boolean false,

5 5 Using Relational Operators in Conditional Expressions 1.Equal to, = = 2.Not equal to, != 3.Less than, < 4.Less than or equal to, <= 5.Greater than, > 6.Greater than or equal to, >=

6 6 if (employee_status == ‘f’) printf(“%d” is full-time.”, employee_number); else printf(“%d” id part-time.”, employee_number); /* end if */

7 7 Precedence Level Operator Associativity 1 (highest)( )left 2 Unary + unary - right 3* /left 4+ -left 5 >= left 6= = !=left 7 (lowest)=right

8 8 Using Strings in Conditional Expressions string2=string1; - will not work - The reason is that the names of string variables declared as character arrays are the addresses of the memory locations where they are stored. - program must contain #include - function strcpy to copy one string into another

9 9 Using Strings in Conditional Expressions (Cont.) Example 4.11 char string1[21], string2[21]; strcpy(string2,string1); - strcmp can be used to compare two strings. - It returns an integer that is less than zero, equal to zero, or greater than zero if the first string parameter is less than, equal to, or greater than the second string parameter.

10 10 Using Strings in Conditional Expressions (Cont.) Example 4.12 char string1[21], string2[21]; if ( strcmp ( string1, string2 ) > 0 ) printf( “ %s is greater than %s ”, string1, string2 ); else printf( “ %s is not greater than %s ”, string1, string2 ); /* end if */

11 11 Nested if Statements and Multiway Selection if ( filing_status == 1 ) printf ( “Single” ); /* end if */ if ( filing_status == 2 ) printf ( “Married filing jointly” ); /* end if */ Figure 4.6 A multiway selection structure based on one variable: Use of unnested if statement

12 12 Nested if Statements and Multiway Selection (Cont.) if selection structure is more efficient from a program execution point of view. if ( filing_status == 1 ) printf ( “Single” ); else if ( filing_status = = 2 ) printf ( “Married filing jointly” ); /* end if */ Figure 4.6 A multiway selection structure based on one variable: Use of nested if statement

13 13 4.3 PROGRAMMING FOR CONTROLLED REPETITIONS Repetition Statements - to define controlled repetitions ( or loops ) on a program block - There are two types of repetition statements: 1. Pretest repetition statements 2. Posttest repetition statements - In C we have three statements for repetition: 1.The while statements, which is a pretest repetition statement 2. The for statement, which is also a pretest repetition statement 3. The do-while statement, which is a posttest repetition statement

14 14 Controlled Repetitions Using the while Statement while statement works as follows: 1. The loop control expression in parentheses is evaluated. 2. If the result of evaluation is a nonzero value, the loop body is executed. 3. If the loop control expression produces a zero value, the loop body is bypassed. while ( Expression ) Statement /* end while */

15 15 Controlled Repetitions Using the while Statement (Cont.) fact = 1; count = 2; while ( count <= n ) { fact = fact * count; count=count + 1; } /* end while */

16 16 4.4 STYLE CONSIDERATIONS FOR if AND while STATEMENTS The loop body for a while statement should be indented to visually emphasize the syntactical elements. While ( count < n ) { fact = fact * count; count = count + 1; } /* end while */

17 17 4.5 STRUCTURED PROGRAMMING conditional branch specified by the if and while statements. unconditional branches take the program control to another point in the program without providing a return to the point of branching. goto Label ; Label : Statement ;

18 18 4.5 STRUCTURED PROGRAMMING (Cont.) A new discipline called structured programming. Any program can be written using only three basic control structure: sequence, selection, and repetition. Unconditional branching and the goto statement are not essential control structure in programming.


Download ppt "1 Chapter 4 Simple Selections and Repetitions. 2 4.1 INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make."

Similar presentations


Ads by Google