Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

34
Control Statements in C www.ustudy.in

Transcript of Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Page 1: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Control Statements in C

www.ustudy.in

Page 2: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

1.Decision making statements

2.Looping statements

3.Branching statements

www.ustudy.in

Page 3: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 4: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Decision making statements

• If

• If else

• Else if

• Nested if

• switch

www.ustudy.in

Page 5: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 6: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 7: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 8: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Example

• A very simple program:

#include <stdio.h>

int main ( )

{

int a = 1, b = 2, c ;

if (a > b) c = a;

else c = b;

}

www.ustudy.in

Page 9: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 10: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Example

• Often, the earlier example is written this way:

#include <stdio.h>int main ( ){ int a = 1, b = 2, c ; if (a > b)

c = a ; else

c = b ;}

www.ustudy.in

Page 11: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 12: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 13: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Simple Program Using if / else if / else

#include <stdio.h>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

Page 14: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Grading program using if,else if , else

/* This program associates letter grades with numeric test scores */

#include <stdio.h>

int main ( ){

int score ;printf ("enter your test score >") ;scanf ("%d", &score) ;

www.ustudy.in

Page 15: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 16: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 17: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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 caseswitch ( value ){case '1':

actions

case '2':actions

default:actions

}

◦ break; causes exit from structure

 

www.ustudy.in

Page 18: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

The switch Multiple-Selection Structure

true

false

.

.

.

case a case a action(s) break

case b case b action(s) break

false

false

case z case z action(s) break

true

true

default action(s)

www.ustudy.in

Page 19: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

1. Initialize variables

2. Input data

2.1 Use switch loop to update count

1 /* Fig. 4.7: fig04_07.c2 Counting letter grades */3 #include <stdio.h>45 int main()6 {7 int grade;8 int aCount = 0, bCount = 0, cCount = 0, 9 dCount = 0, fCount = 0;1011 printf( "Enter the letter grades.\n" );12 printf( "Enter the EOF character to end input.\n" );1314 while ( ( grade = getchar() ) != EOF ) {1516 switch ( grade ) { /* switch nested in while */1718 case 'A': case 'a': /* grade was uppercase A */19 ++aCount; /* or lowercase a */20 break;2122 case 'B': case 'b': /* grade was uppercase B */23 ++bCount; /* or lowercase b */24 break;2526 case 'C': case 'c': /* grade was uppercase C */27 ++cCount; /* or lowercase c */28 break;2930 case 'D': case 'd': /* grade was uppercase D */31 ++dCount; /* or lowercase d */32 break;

Page 20: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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 }

Page 21: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Program Output

Enter the letter grades.Enter the EOF character to end input.ABCCADFCEIncorrect letter grade entered. Enter a new grade.DAB Totals for each letter grade are: A: 3B: 2C: 3D: 2F: 1

Page 22: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 23: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

The for loop statement

SYNTAX:for (initialization; condition; increment/decrement){

statement_sequence;}

for is a reserve word in turbo Cinitialization 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 semicolon after last expression

www.ustudy.in

Page 24: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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 <stdio.h>

4

5 int main()

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 }

Page 25: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

While loop statement

SYNTAX:initialization;while (condition){

statement_sequence;increment/decrement

}

while is a reserve word in turbo Cinitialization 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

Page 26: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

The do/while loop statement

SYNTAX:initialization;do{

statement_sequence;increment/decrement

} while (condition)

do-while is a reserve word in turbo Cinitialization 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

Page 27: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

The do/while Repetition Structure

true

false

action(s)

condition

www.ustudy.in

Page 28: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 29: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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 <stdio.h>

4

5 int main()

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

Page 30: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

Branching statements

1.break statement

2.continue statement

3.goto statement

www.ustudy.in

Page 31: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 32: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 33: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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

Page 34: Control Statements in C . 1.Decision making statements 2.Looping statements 3.Branching statements .

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 <stdio.h>

4

5 int main()

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 10Used continue to skip printing the value 5