C Programming Control Structures PDF

Summary

This document is a presentation or lecture on control flow structures in C programming, covering concepts such as sequential, selection, and iteration logic. It introduces control structures like if/else statements, loops (while, do/while, for), and switch statements with examples and flowcharts.

Full Transcript

Module # 2 Control Structure, Branching and Looping Structures Dr. Tatwadarshi P. N. Control Structures Dr. Tatwadarshi P. N. Control Structures ► Control Structures are just a way to spec...

Module # 2 Control Structure, Branching and Looping Structures Dr. Tatwadarshi P. N. Control Structures Dr. Tatwadarshi P. N. Control Structures ► Control Structures are just a way to specify flow of control in programs. ► Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. ► It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. ► There are three basic types of logic, or flow of control, known as: ► Sequence logic, or sequential flow ► Selection logic, or conditional flow ► Iteration logic, or repetitive flow Dr. Tatwadarshi P. N. Sequential Flow ► Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer. ► Unless new instructions are given, the modules are executed in the obvious sequence. ► The sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order in which modules are written. ► Most of the processing, even some complex problems, will generally follow this elementary flow pattern. Dr. Tatwadarshi P. N. Branching or Conditional Flow or Decision Making Dr. Tatwadarshi P. N. Conditional Flow ► Selection Logic simply involves a number of conditions or parameters which decides one out of several written modules. ► The structures which use these type of logic are known as Conditional Structures. Dr. Tatwadarshi P. N. Decision Making with ‘if’ ► The if statement is a powerful decision making statement and used to control the flow of the execution of statements. ► It is basically a two-way decision statement and is used in conjunction with an expression. ► It takes the following form ► if (test expression) ► 1.if (bank balance is zero):borrow money ► 2. if (room is dark): put on lights ► 3. if (code is 1): person is male ► 4. if (age is more than 55): person is retired Dr. Tatwadarshi P. N. Simple ‘if’ ► The general form of a simple if statement is ► if (test expression) ► { ► statement-block; ► } ► statement-x; ► The ‘statement-block’ may be a single statement or a group of statements. If the test ► expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. ► Remember, when the condition is true both the statement-block and the statement-x are executed in sequence. Dr. Tatwadarshi P. N. Simple ‘if’ (Contd…) Dr. Tatwadarshi P. N. Simple ‘if’ (Contd…) Dr. Tatwadarshi P. N. ‘if’ ……… ‘else’ ► The if...else statement is an extension of the simple if statement. The general form is ► If (test expression) ► { ► True-block statement(s) ► } ► else ► { ► False-block statement(s) ► } ► statement-x Dr. Tatwadarshi P. N. ‘if’ ……… ‘else’ (Contd…) Dr. Tatwadarshi P. N. ‘if’ ……… ‘else’ (Contd…) #include int main() { int i = 20; // Check if i is 10 if (i == 10) printf("i is 10"); // Since is not 10 // Then execute the else statement else printf("i is 20"); printf("Outside if-else block"); return 0; Dr. Tatwadarshi P. N. } Nesting of ‘if’ ……… ‘else’ ► When a series of decisions are involved, we may have to use more than one if...else statement in nested form as shown below: Dr. Tatwadarshi P. N. Nesting of ‘if’ ……… ‘else’ (Contd…) Dr. Tatwadarshi P. N. Nesting of ‘if’ ……… ‘else’ (Contd…) ► if (gender is female) ► { ► if (balance > 5000) ► bonus = 0.05 * balance; ► else ► bonus = 0.02 * balance; ► } ► else ► { ► bonus = 0.02 * balance; ► } ► Dr. Tatwadarshi P. N. balance = balance + bonus Else if Ladder ► There is another way of putting ifs together when multipath decisions are involved. ► A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form: Dr. Tatwadarshi P. N. Else if Ladder (Contd…) ► Let us consider an example of grading the students in an academic institution. ► The grading is done according to the following rules: ► 80 to 100 Honours ► 60 to 79 First Division ► 50 to 59 Second Division ► 40 to 49 Third Division ► 0 to 39 Fail Dr. Tatwadarshi P. N. Else if Ladder (Contd…) Dr. Tatwadarshi P. N. Else if Ladder (Contd…) ► if (marks > 79) ► grade = “Honours”; ► else if (marks > 59) ► grade = “First Division”; ► else if (marks > 49) ► grade = “Second Division”; ► else if (marks > 39) ► grade = “Third Division”; ► else ► grade = “Fail”; ► printf (“%s\n”, grade); Dr. Tatwadarshi P. N. Switch Statement ► We have seen that when one of the many alternatives is to be selected, we can use an if statement to control the selection. ► However, the complexity of such a program increases dramatically when the number of alternatives increase. ► The program becomes difficult to read and follow. At times, it may confuse even the person who designed it. ► Fortunately, C has a built-in multiway decision statement known as a ‘switch’. ► The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed. Dr. Tatwadarshi P. N. Switch Statement (Contd…) ► switch (expression) ► { ► case value-1: ► block-1 ► break; ► case value-2: ► block-2 ► break; ►...... ►...... ► default: ► default-block ► break; ► } ► Dr. statement-x; Tatwadarshi P. N. Switch Statement (Contd…) Dr. Tatwadarshi P. N. Switch Statement (Contd…) Dr. Tatwadarshi P. N. Switch Statement (Contd…) Dr. Tatwadarshi P. N. Repetitive Flow or Looping Structure Dr. Tatwadarshi P. N. Looping Structure ► In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. ► A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statement. ► The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop. ► Depending upon the position of the control statement in the loop a control structure may be classified either as the entry-controlled loop or as the exit-controlled loop. Dr. Tatwadarshi P. N. Looping Structure (Contd…) Dr. Tatwadarshi P. N. Looping Structure (Contd…) ► The test conditions should be carefully stated in order to perform the desired number of loop executions. ► It is assumed that the test condition will eventually transfer the control out of the loop. ► In case, due to some reason it does not do so, the control sets up an and the body is executed over and over again. ► A looping process generally involves: ► 1. Setting and initialization of a condition variable. ► 2. Execution of the statements in the loop. ► 3. Test for the specified value of the condition variable for execution of the loop. ► 4. Incrementing or updating the condition variable. Dr. Tatwadarshi P. N. Looping Structure (Contd…) ► Based on the nature of control variable and the kind of value assigned to it for testing the control ► When we know in advance exactly how many times the loop will be executed, we use a counter controlled loop. We use a control variable known as counter. The counter must be initialized, tested and updated properly for the desired loop operations. The number of times we want to execute the loop may be a constant or a variable that is assigned a value. ► A counter controlled loop is sometime called the Definite Repetition Loop. ► In a sentinel-controlled loop, a special value called a sentinel value is used to change the loop control expression from true to false. ► For example, when reading data we may indicate the “end of data” by a special value, like –1 and 999. The control variable is called sentinel controlled variable. A sentinel controlled loop is often called Indefinite Repetition Loop because the number of repetitions is not known before the loop begins Dr. Tatwadarshi P. N. executing. The ‘While’ Statement ► The simplest of all the looping structures in C is the while statement. The basic format of the while statement is ► while (test condition) ► { ► body of the loop ► } Dr. Tatwadarshi P. N. The ‘While’ Statement (Contd…) ► while (test condition) ► { ► body of the loop ► } ► The while is an entry-controlled loop statement. ► The test-condition is evaluated and if the condition is true, then the body of the loop is executed. ► After execution of the body the test condition is once again evaluated and if it is true, the body is executed once again. ► This process of repeated execution of the body continues until the test condition becomes false and the control is transferred out of the loop. ► On exit, the program continues with the statement immediately after the body of the loop. Dr. Tatwadarshi P. N. The ‘While’ Statement (Contd…) Dr. Tatwadarshi P. N. The ‘Do’….‘While’ Statement ► The while loop construct that we have discussed in the previous section, makes a test of condition before the loop is executed. ► Therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. ► On some occasions it might be necessary to execute the body of the loop before the test is performed. Such situations can be handled with the help of the do statement. ► This takes the form: ► do ► { ► body of the loop ► } ► while (test-condition); Dr. Tatwadarshi P. N. The ‘Do’….‘While’ Statement (Contd…) ► On reaching the do statement, the program proceeds to evaluate the body of the loop first. ► At the end of the loop, the test-condition in the while statement is evaluated. ► If the condition is true, the program continues to evaluate the body of the loop once again. ► This process continues as long as the condition is true. ► When the condition becomes false, the loop will be terminated and the control goes to the statement that appears immediately after the while statement. ► Since the test-condition is evaluated at the bottom of the loop, the do...while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once. Dr. Tatwadarshi P. N. The ‘Do’….‘While’ Statement (Contd…) Dr. Tatwadarshi P. N. The ‘For’ Statement ► The for loop is another entry-controlled loop that provides a more concise loop control structure. ► The general form of the for loop is ► for ( initialization ; test-condition ; increment) ► { ► body of the loop ► } Dr. Tatwadarshi P. N. The ‘For’ Statement (Contd…) ► The execution of the for statement is as follows: ► 1. Initialization of the control variables is done first, using assignment statements like i=1, and count = 0. The variables i and count are known as loop-control variables. ► 2. The value of the control variable is tested using the test-condition. The test-condition is a relational expression, such as i < 10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop. ► 3. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. ► Now, the control variable is incremented using an assignment statement such as i = i+1 and the new value of the control variable is again tested again executed. ► This process continues till the value of the control variable fails to satisfy the test-condition. Dr. Tatwadarshi P. N. The ‘For’ Statement (Contd…) ► Consider the following segment of a program: ► This for loop is executed 10 times and prints the digits 0 to 9 in one line. ► The three sections enclosed within parentheses must be separated by semicolons. ► Note that there is no semicolon at the end of the increment section, x = x+1. Dr. Tatwadarshi P. N. The ‘For’ Statement (Contd…) ► The for statement allows for negative increments. For example, the loop discussed above can be written as follows: ► for ( x = 9 ; x >= 0 ; x = x–1 ) ► { ► printf(“%d”, x); ► } ► printf(“\n”); ► This loop is also executed 10 times, but the output would be from 9 to 0 instead of 0 to 9. ► Note that braces are optional when the body of the loop contains only one statement. Dr. Tatwadarshi P. N. The ‘For’ Statement (Contd…) Dr. Tatwadarshi P. N. Additional features of ‘For’ loop ► The for loop in C has several capabilities that are not found in other loop constructs. ► For example, more than one variable can be initialized at a time in the for statement. The statements ► p = 1; ► for (n=0; n

Use Quizgecko on...
Browser
Browser