[go: up one dir, main page]

0% found this document useful (0 votes)
14 views12 pages

Control Statement

The document explains various control statements in C language that alter the flow of program execution based on conditions, including if statements, nested if-else, else-if ladder, switch, goto, and break statements. Each statement is detailed with syntax, examples, and sample programs demonstrating their usage. These control statements are essential for implementing decision-making capabilities in programming.

Uploaded by

Sathya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Control Statement

The document explains various control statements in C language that alter the flow of program execution based on conditions, including if statements, nested if-else, else-if ladder, switch, goto, and break statements. Each statement is detailed with syntax, examples, and sample programs demonstrating their usage. These control statements are essential for implementing decision-making capabilities in programming.

Uploaded by

Sathya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CONTROL STATEMENT

A program is nothing but the execution of sequence of one or more instructions. There are a
number of situations where one has to change the order of the execution of statements based on
the conditions or repeat a group of statements until certain specified conditions are met.

C language possesses such decision-making capabilities by supporting the following statements:

 if statement
 if - else statement
 nested if - else statement
 else - if ladder statement
 switch statement
 goto statement
 break statement

These statements are known as decision-making statements. Since these statement ‘control’ the
flow of execution, they are also known as control statement.

If statement:

 If statement is executed only when the condition is true.


 In case the condition is false, the compiler skips the lines within the if block.
 Keyword if to execute a set of command lines or a command line when logical condition
is true.
 It has only one option. The set of command lines or command line are executed only
when the logical condition is true.
Syntax:

/* one statement */
if ( test condition)
statement;
(or)
/* group of statements */
if ( test condition )
{
statement block;
}

Eg
..........
if ( a %2 == 0)
printf (“ a is an even number ” );
..........
Program:
#include <stdio.h>
#include <conio.h>
void main ( )
{
int a;
clrscr ( );
printf (“ Enter the number: ”);
scanf (“%d”, &a);
if ( a < 10 )
printf (“ a is less than 10 ” );
getch ( );
}
Output:
Enter the number: 5
a is less than 10
Enter the number: 11
If - else statement:

If - else statement takes care of true as well as false conditions.

It has two blocks.

 If block, it is executed when the condition is true.


 Else block, it is executed when the condition is false. else statement cannot be used
without if.

Eg .. ........
if (leap%4 == 0 )
printf (“ Given year is a leap year ”);
else
printf (“Given year is not a leap year”);
..........
Syntax:

/* one statement */
if ( test condition )
statement;
else
statement;
(or)
/* group of statements */
if ( test condition )
{
statement block;
}
else
{
statement block;
}

Program: /* To find smallest number */


#include <stdio.h>
#include <conio.h>
void main ( )
{
int a, b;
clrscr ( );
printf (“ Enter two numbers: ”);
scanf (“%d”, &a, &b);
if ( a < b )
printf (“ a is small number ” );
else
printf (“ b is small number”);
getch ( );
}
Output: Enter two numbers: 24 79
a is small number
Nested if - else statement:

In this kind of statement, number of logical conditions are checked for executing various
statements Here, if any logical condition is true the compiler executes the block followed by if
condition, otherwise it skips and executes else block.

Syntax: (or)

if ( test condition-1 ) if ( test condition-1 )


{ {
if (test condition-2) if (test condition-2)
{ {
statement block; statement block;
} }
else else
{ {
statement block; statement block;
} }
} }
else else
{ {
if (test condition-3) statement block;
{ }
statement block;
}
else
{
statement block;
}
}

Eg:
.. ........
if ( exp > 10 )
{
if (salary > 10000 )
bonus = 0.5 x salary;
else
bonus = 0.3 x salary;
}
else
{
bonus = 0.2 x salary;
}
..........
Program:/* To find biggest number */
void main ( )
{ int a, b, c;
printf (“ Enter three numbers: ”);
scanf (“%d”, &a, &b, &c);
if (a > b )
{
if ( a > c )
printf (“ a is a biggest number “);
else
printf (“c is a biggest number”);
}
else
{
if (b > c )
printf (“ c is a biggest number ”);
else
printf (“ b is a biggest number ”);
}
}
Output:
Enter three numbers: 24 79 10
b is a biggest number
Else - if ladder statement:

The conditions are evaluated from the ladder, downwards. As soon as a true condition is found,
the statement associated with it is executed and the control is transferred to statement-x
(skipping the rest of the ladder). When all the n conditions become false, then the final else
containing the default statement will be executed.

Syntax:

if ( test condition-1 )
statement-1;
{
else if (test condition-2)
statement-2;
else if (test condition-3)
statement-3;
else if (test condition-n)
statement-n;
else
default statement;
}

Eg:
.. ........
.. ........
if ( code == 1 )
colour == “ Red” ;
else if ( code == 1 )
colour == “ Green” ;
else if ( code == 1 )
colour == “ White” ;
else
colour == “ block” ;
.. ........
. ........
Program:
#include <stdio.h>
#define FIRST 75
#define SECOND 60
#define THIRD 50
void main ( )
{
int m1, m2, m3, tot, avg;
printf ( “Enter each subject mark: ” );
scanf (“ %d %d %d %d %f ”,&m1, &m2, &m3, &tot, &avg);
tot = m1+m2+m3; avg = tot/3;
printf (“ Average mark = %d”, avg);
if (avg >= FIRST)
printf (“ First class”);
{
else if (avg >= SECOND)
printf (“ Second class”);
else if (avg >= THIRD)
printf (“ Third class”);
else
printf (“Fail”);
}
}
Output:

Enter each subject mark: 98 99 100

Average mark = 99

First class
Switch statement:

 The switch statement is a multi-way branch statement.


 The switch statement requires only one argument of any data type, which is checked with
number of case options.
 The switch statement evaluates expression and then looks for its value among the case
constants. If the value matches with case constant, this particular case statement is
executed. If not, default is executed.
 The break statement is used to exit from the current case structure.
Syntax:

switch ( expression )
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
.......
.......
case value-n:
block-n;
break;
default:
default-block;

Eg:
.. ........
switch (ch)
{
case 1:
printf (“\n Enter your name”);
fopen ( );
break;
case 2:
printf (“\n Enter your reg.no”);
fopen ( );
break;
default:
printf (“\n Invalid Choice”);
.. ........
Program:
#include <stdio.h>
void main ( )
{
int a, b, c, ch ;
printf (“\n \t [1] Addition”);
printf (“\n \t [2] Subtraction”);
printf (“\n \t [3] Multiplication”);
printf (“\n \t [4] Division”);
printf (“\n\n\t Enter your choice:”);
scanf(“%d”, &ch);
if ( ch <= 4 && ch > 1 )
{
printf (“ Enter two numbers :”);
scanf (“%d %d”, &a, &b);
}
switch (ch)
{
case 1:
c = a + b;
printf (“\n Addition: %d”, c);
break;
case 2:
c = a - b;
printf (“\n Subtraction: %d”, c);
break;
case 3:
c = a * b;
printf (“\n Multiplication: %d”, c);
break;
case 4:
c = a / b;
printf (“\n Division: %d”, c);
break;
default:
printf (“\n Invalid Choice”);
}
}
Output:
[1] Addition
[2] Subtraction
[3] Multiplication
[4] Division
Enter your choice: 3
Enter two numbers: 2 7
Multiplication: 14
Break statement:

 It exits from current block or loop.


 Control passes to next statement.
 It terminates the program.

Refer switch statement for syntax, eg, program.


Goto statement:

 Goto statement does not require any condition.


 This statement passes control anywhere in the program, that is, control is transferred to
another part of the program without testing any condition.
 The goto requires a label in order to identify the place where the branch is to be made.
 A label is any valid variable name, and must be followed by a colon.
 Two ways present in goto statement:
o Forward jump
o Backward jump

Syntax:

goto label; label :


....... statement;
....... ........
....... ........
label : ........
statement; goto label;

Forward jump Backward jump

Eg:

.......

.......

goto add;

else

goto sub;

add:

c = a + b;

sub:

c = a – b;

.......

.......
Program:

#include < stdio.h>

void main ( )

int x;

printf (“Enter a number”);

scanf (“%d”, &x);

if ( x%2 == 0)

goto even;

else

goto odd;

even:

printf (“\n %d is even number”);

return;

odd:

printf (“\n %d is odd number”);

Output:

Enter a number: 5

5 is odd number

You might also like