Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Control Statements in C

www.ustudy.in

1.Decision making statements


2.Looping statements
3.Branching statements

www.ustudy.in

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

Decision making statements

If
If else
Else if
Nested if
switch

www.ustudy.in

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

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

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

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

If,if else structures


The if selection structure is often written as:
if ( this logical expression is true )
statement ;

no semi-colon!

And, the if / else selection structure is often written as:


if ( this logical expression is true )
statement ;
else
statement ;

www.ustudy.in

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

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

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

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

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

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

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

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

The switch Multiple-Selection Structure

case a

true

case a action(s)

break

case b action(s)

break

case z action(s)

break

false

case b

true

false
.
.
.

case z

true

false
default action(s)

www.ustudy.in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

/* Fig. 4.7: fig04_07.c


Counting letter grades */
#include <stdio.h>
int main()
{
int grade;
int aCount = 0, bCount = 0, cCount = 0,
dCount = 0, fCount = 0;
printf(
printf(

"Enter the letter grades.\n" );


"Enter the EOF character to end input.\n"

1. Initialize variables
2. Input data

);

while ( ( grade = getchar() ) != EOF ) {


switch ( grade ) {

/* switch nested in while */

case 'A': case 'a':


++aCount;
break;

/* grade was uppercase A */


/* or lowercase a */

case 'B': case 'b':


++bCount;
break;

/* grade was uppercase B */


/* or lowercase b */

case 'C': case 'c':


++cCount;
break;

/* grade was uppercase C */


/* or lowercase c */

case 'D': case 'd':


++dCount;
break;

/* grade was uppercase D */


/* or lowercase d */

2.1 Use switch loop to


update count

33
34

case 'F': case 'f':

35

++fCount;

36

break;

/* grade was uppercase F */


/* or lowercase f */

37
38

case '\n': case' ':

39

/* ignore these in input */

break;

3. Print results

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
56 }

return 0;

2.1 Use switch loop to


update count

Enter the letter grades.


Enter the EOF character to end input.
A
B
C
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

Program Output

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

The for loop statement


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

No
semicol
on after
last
expressi
on

for is a reserve word in turbo C


initialization is an assignment statement that is used to set the
loops counter.
condition is a relational boolean expression that determines when
the loop will exit.
increment defines how the loops 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

/* Fig. 4.5: fig04_05.c

2
3

Summation with for */


#include <stdio.h>

4
5

int main()

int sum = 0, number;

8
9
10

for ( number = 2; number <= 100; number += 2 )

4.6 Examples Using the


for Structure
Program to sum the
even numbers from 2
to 100

sum += number;

11
12

printf( "Sum is %d\n", sum );

13
14

return 0;

15 }
Sum is 2550

Program Output

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
loops counter.
condition is a relational Boolean expression that determines when
the loop will exit.
increment defines how the loops 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

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 loops
counter.
condition is a relational Boolean expression that determines when the
loop will exit.
increment defines how the loops 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

The do/while Repetition Structure

action(s)

true
condition
false

www.ustudy.in

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

/* Fig. 4.9: fig04_09.c

2
3

Using the do/while repetition structure */


#include <stdio.h>

1. Initialize variable

4
5

int main()

2. Loop
int counter = 1;

3. Print

8
9

do {

10

printf( "%d

11

", counter );

} while ( ++counter <= 10 );

12
13

return 0;

14 }

10

Program Output

Branching statements
1.break statement
2.continue statement
3.goto statement

www.ustudy.in

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

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

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

/* Fig. 4.12: fig04_12.c

2
3

Using the continue statement in a for structure */


#include <stdio.h>

4
5

int main()

1. Initialize variable

int x;

2. Loop

8
9

for ( x = 1; x <= 10; x++ ) {

10
11

3. Print

if ( x == 5 )

12

continue;

13

/* skip remaining code in loop only


if x == 5 */

14
15
16

printf( "%d ", x );


}

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

Program Output

You might also like