You are on page 1of 24

Control Statements

• The C language programs until now follows a


sequential form of execution of statements.
• Many times it is required to alter the flow of the
sequence of instructions.
• C language provides statements that can alter the
flow of a sequence of instructions. These statements
are called control statements.
• These statements help to jump from one part of the
program to another. The control transfer may be
conditional or unconditional.
• The C conditional statements are the:

– if statement
– if-else statement
– switch statement
Logic of an if statement

condition
evaluated

true
false

statement
The if Statement
• The if statement has the following syntax:
The condition must be a
boolean expression. It must
if is a C evaluate to either true or false.
reserved word

if ( condition )
statement;

If the condition is true, the statement is executed.


If it is false, the statement is skipped.
• If student’s grade is greater than or equal to 60
Print “Passed”

• if ( grade >= 60 )
{
printf( "Passed\n" );
} /* end if */
Example
• Demonstration of IF Statement
1.Enter a number and print result if Number is
less than 0.

2.Calculation of Total Expenses by entering Qty


and Rate and if Qty>1000. Dis =10

5-7
IF-ELSE
• The if else is actually just on extension of the
general format of if statement.
• If the result of the condition is true, then program
statement 1 is executed, otherwise program
statement 2 will be executed.
• If any case either program statement 1 is executed
or program statement 2 is executed but not both
• When writing programs this else statement is so
frequently required that almost all programming
languages provide a special construct to handle this
situation.
If…Else Statement
if (a>b)
{

}
else
{

}
• the if part is executed if the test statement is true,
otherwise the else part is executed.
Logic of an if-else statement

condition
evaluated

true false

statement1 statement2
The if-else Statement
• An else clause can be added to an if
statement to make an if-else statement
if ( condition )
statement1;
else
statement2;

• If the condition is true, statement1 is executed; if the condition


is false, statement2 is executed

• One or the other will be executed, but not both


If…Else Statement
#include <stdio.h>  //include the stdio.h header file in your program 

  void main ()   // start of the main 

  { 

    int num;  // declare variable num as integer 

    printf ("Enter the number"); // message to the user 

    scanf ("%d", &num);   // read the input number from keyboard 

    if (num < 0)   // check whether number is less than zero


   printf ("The number is negative");   // if it is less than zero then it is negative


  }

    else  // else statement 


{
       printf ("The number is positive");  // if it is more than zero then the given number is positive 

}
getch();
}
Nested if else Statements
• The statement executed as a result of an if statement or else
clause could be another if statement

• These are called nested if statements

• An else clause is matched to the last unmatched if (no matter


what the indentation implies)

• Braces can be used to specify the if statement to which an


else clause belongs

• Example: Enter 1 or 2
Nested if else
The switch Statement
• The switch statement provides another way to
decide which statement to execute next
• The switch statement evaluates an expression,
then attempts to match the result to one of
several possible cases
• Each case contains a value and a list of
statements
• The flow of control transfers to statement
associated with the first case value that matches
The switch Statement
• Often a break statement is used as the last
statement in each case's statement list
• A break statement causes control to transfer to
the end of the switch statement
• If a break statement is not used, the flow of
control will continue into the next case
• Sometimes this may be appropriate, but often we
want to execute only the statements associated
with one case
The switch Statement
• An example of a switch statement:
switch (option)
{
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
The switch Statement
• A switch statement can have an optional
default case
• The default case has no associated value and
simply uses the reserved word default
• If the default case is present, control will transfer
to it if no other case value matches
• If there is no default case, and no other value
matches, control falls through to the statement
after the switch
The switch Statement
• The expression of a switch statement must
result in an integral type, meaning an integer
(byte, short, int,) or a char
• It cannot be a floating point value (float or
double)
• The implicit test condition in a switch
statement is equality
• You cannot perform relational checks with a
switch statement
The switch Statement
• The general syntax of a switch statement is:
switch switch ( expression )
and {
case case value1 :
are statement-list1
reserved case value2 :
words statement-list2
case value3 :
statement-list3 If expression
case ... matches value2,
control jumps
} to here
• The exact behavior of the switch statement
is controlled by the break and default
commands
• break continues execution after the
switch statement
• default is executed if no other match
is found
Switch Statement
true
variable
equals first case body
const 1

false

true
variable
equals second case body
const 2

false
default body

exit
Switch Statement
char c;
printf(”Enter your choice (a/b/c) : ”);
scanf(”%c”,&c);
switch (c)
{
case ’a’:
printf(”You picked a!\n”);
break;
case ’b’:
printf(”You picked b!\n”);
break;
case ’c’:
printf(”You picked c!\n”);
break;
default:
printf(”You picked neither a,b,c !\n”);
}

You might also like