Download presentation
Presentation is loading. Please wait.
Published byGeorgia Higgins Modified over 9 years ago
1
Control Statements in C www.ustudy.in
2
1.Decision making statements 2.Looping statements 3.Branching statements www.ustudy.in
3
Decision making statements Used to making decisions based upon certain condition Conditions are decide whether or not a statement should be executed Keywords if and else for condition statement. www.ustudy.in
4
Decision making statements If If else Else if Nested if switch www.ustudy.in
5
If,else if,else Selection Structures Selection structures permit alternate actions based on the evaluation of logical expressions. The logical expressions evaluate as either true or false, and the action takes place if and only if the expression is true. When the expression is false, the program may take alternate action(s), or it may evaluate another expression to determine what to do next. www.ustudy.in
6
If,else if,else structures A simple if structure is called a single-selection structure because it either selects or ignores a single action. The if / else structure is called a double-selection structure because it selects between two different actions. Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures. www.ustudy.in
7
If,if else Syntax Syntax for the if selection structure is as follows: if ( this logical expression is true ) statement ; Syntax for the if / else selection structure is as follows: if ( this logical expression is true ) statement ; else statement ; www.ustudy.in
8
Example A very simple program: #include int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a; else c = b; } www.ustudy.in
9
If,if else structures The if selection structure is often written as: if ( this logical expression is true ) statement ; And, the if / else selection structure is often written as: if ( this logical expression is true ) statement ; else statement ; no semi-colon! www.ustudy.in
10
Example Often, the earlier example is written this way: #include int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a ; else c = b ; } www.ustudy.in
11
If,else if, else structures Syntax for the if / else if / else selection structure is as follows: if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else statement ; www.ustudy.in
12
else if syntax The actual syntax for the multiple if / else if / else selection structure is as follows: if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else statement ; www.ustudy.in
13
Simple Program Using if / else if / else #include int main ( ) { int a, b ; printf ("Enter values for a and b > ") ; scanf ("%d%d", &a, &b ) ; if ( a < b ) printf ("a is less than\n") ; else if ( a == b ) printf (" a is equal to b\n") ; else printf ("a is larger than b\n") ; } www.ustudy.in
14
Grading program using if,else if, else /* This program associates letter grades with numeric test scores */ #include int main ( ) { int score ; printf ("enter your test score >") ; scanf ("%d", &score) ; www.ustudy.in
15
if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score < 90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ; else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else printf ("Your score of %d is an E\n", score) ; } www.ustudy.in
16
Nested if statement The if statement may itself contain another if statement is known as nested if statement. Syntax: if (condition1) if (condition2) statement-1; else statement-2; else statement-3; www.ustudy.in
17
The switch Multiple-Selection Structure switch ◦ Useful when a variable or expression is tested for all the values it can assume and different actions are taken. Format ◦ Series of case labels and an optional default case switch ( value ){ case '1': actions case '2': actions default: actions } ◦ break; causes exit from structure www.ustudy.in
18
The switch Multiple-Selection Structure true false...... case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s) www.ustudy.in
19
1. Initialize variables 2. Input data 2.1 Use switch loop to update count 1/* Fig. 4.7: fig04_07.c 2 Counting letter grades */ 3#include 4 5int main() 6{6{ 7 int grade; 8 int aCount = 0, bCount = 0, cCount = 0, 9 dCount = 0, fCount = 0; 10 11 printf( "Enter the letter grades.\n" ); 12 printf( "Enter the EOF character to end input.\n" ); 13 14 while ( ( grade = getchar() ) != EOF ) { 15 16 switch ( grade ) { /* switch nested in while */ 17 18 case 'A': case 'a': /* grade was uppercase A */ 19 ++aCount; /* or lowercase a */ 20 break; 21 22 case 'B': case 'b': /* grade was uppercase B */ 23 ++bCount; /* or lowercase b */ 24 break; 25 26 case 'C': case 'c': /* grade was uppercase C */ 27 ++cCount; /* or lowercase c */ 28 break; 29 30 case 'D': case 'd': /* grade was uppercase D */ 31 ++dCount; /* or lowercase d */ 32 break;
20
2.1 Use switch loop to update count 3. Print results 33 34 case 'F': case 'f': /* grade was uppercase F */ 35 ++fCount; /* or lowercase f */ 36 break; 37 38 case '\n': case' ': /* ignore these in input */ 39 break; 40 41 default: /* catch all other characters */ 42 printf( "Incorrect letter grade entered." ); 43 printf( " Enter a new grade.\n" ); 44 break; 45 } 46 } 47 48 printf( "\nTotals for each letter grade are:\n" ); 49 printf( "A: %d\n", aCount ); 50 printf( "B: %d\n", bCount ); 51 printf( "C: %d\n", cCount ); 52 printf( "D: %d\n", dCount ); 53 printf( "F: %d\n", fCount ); 54 55 return 0; 56}
21
Program Output Enter the letter grades. Enter the EOF character to end input. A B C A D F C E Incorrect letter grade entered. Enter a new grade. D A B Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1
22
Iterative(looping) Statements It allow a set of instructions to be executed or performed several times until certain conditions are met. KINDS OF LOOPING STATEMENTS 1. for loop 2. while loop 3. do-while loop www.ustudy.in
23
The for loop statement SYNTAX: for (initialization; condition; increment/decrement) { statement_sequence; } for is a reserve word in turbo C initialization is an assignment statement that is used to set the loop’s counter. condition is a relational boolean expression that determines when the loop will exit. increment defines how the loop’s counter will change each time the loop is repeated. statement_sequence may either be a single Turbo C statement or block of Turbo C statements that make up the loop body. No semicol on after last expressi on www.ustudy.in
24
4.6 Examples Using the for Structure Program to sum the even numbers from 2 to 100 Program Output Sum is 2550 1/* Fig. 4.5: fig04_05.c 2 Summation with for */ 3#include 4 5int main() 6{6{ 7 int sum = 0, number; 8 9 for ( number = 2; number <= 100; number += 2 ) 10 sum += number; 11 12 printf( "Sum is %d\n", sum ); 13 14 return 0; 15}
25
While loop statement SYNTAX: initialization; while (condition) { statement_sequence; increment/decrement } while is a reserve word in turbo C initialization is an assignment statement that is used to set the loop’s counter. condition is a relational Boolean expression that determines when the loop will exit. increment defines how the loop’s counter will change each time the loop is repeated. Statement _ sequence may either be a single Turbo C statement or block of Turbo C statements that make up the loop body. www.ustudy.in
26
The do/while loop statement SYNTAX: initialization; do { statement_sequence; increment/decrement } while (condition) do-while is a reserve word in turbo C initialization is an assignment statement that is used to set the loop’s counter. condition is a relational Boolean expression that determines when the loop will exit. increment defines how the loop’s counter will change each time the loop is repeated. statement_sequence may either be a single Turbo C statement or block of Turbo C statements that make up the loop body. www.ustudy.in
27
The do/while Repetition Structure true false action(s) condition www.ustudy.in
28
The do/while Repetition Structure Example (letting counter = 1 ) do { printf( "%d ", counter ); } while (++counter <= 10); Prints the integers from 1 to 10 www.ustudy.in
29
1. Initialize variable 2. Loop 3. Print Program Output 1/* Fig. 4.9: fig04_09.c 2 Using the do/while repetition structure */ 3#include 4 5int main() 6{6{ 7 int counter = 1; 8 9 do { 10 printf( "%d ", counter ); 11 } while ( ++counter <= 10 ); 12 13 return 0; 14} 1 2 3 4 5 6 7 8 9 10
30
Branching statements 1.break statement 2.continue statement 3.goto statement www.ustudy.in
31
The break Statements break – Causes immediate exit from a while, for, do/while or switch structure – Program execution continues with the first statement after the structure – Common uses of the break statement Escape early from a loop Skip the remainder of a switch structure www.ustudy.in
32
The continue Statements continue – Skips the remaining statements in the body of a while, for or do/while structure Proceeds with the next iteration of the loop – while and do/while Loop-continuation test is evaluated immediately after the continue statement is executed – for structure Increment expression is executed, then the loop-continuation test is evaluated www.ustudy.in
33
The goto statement The goto statement transfers control to a label. The given label must reside in the same function and can appear before only one statement in the same function. Syntax statement: labeled-statement jump-statement jump-statement: goto identifier ; labeled-statement: identifier : statement www.ustudy.in
34
1. Initialize variable 2. Loop 3. Print Program Output 1/* Fig. 4.12: fig04_12.c 2 Using the continue statement in a for structure */ 3#include 4 5int main() 6{6{ 7 int x; 8 9 for ( x = 1; x <= 10; x++ ) { 10 11 if ( x == 5 ) 12 continue; /* skip remaining code in loop only 13 if x == 5 */ 14 15 printf( "%d ", x ); 16 } 17 18 printf( "\nUsed continue to skip printing the value 5\n" ); 19 return 0; 20} 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.