The C language programs presented 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. These statements are called as control statements.
A D V E R T I S E M E N T
To jump from one part of the program to another,these statements help.
The control transfer may be unconditional or conditional.
Branching Statemnt are of following categories:
If Statement
The If else Statement
Compound Relational tests
Nested if Statement
Switch Statement
If Statement
The simplest form of the control statement is the If statement. It is very frequently used in decision making and allowing the flow of program execution.
The If structure has the following syntax
if(condition)
statement;
The statement is any valid C language statement and the condition is any valid C language expression, frequently logical operators are used in the condition statement. The condition part should not end with a semicolon, since the condition and statement should be put together as a single statement. The command says if the condition is true then perform the following statement or If the condition is fake the computer skips the statement and moves on to the next instruction in the program.
The following program calculate the absolute value of an integer using if statement:
Calculate the absolute value of an integer */
# include < stdio.h > //Include the stdio.h file
void main ( ) // start of the program
{
int numbers; // Declare the variables
printf ("Type a number:"); // message to the user
scanf ("%d", & number); // read the number from standard input
if (number < 0) // check whether the number is a negative
number
number = - number; // If it is negative then convert it into
positive.
Printf ("The absolute value is % d \n", number); // print the value
}
The If else Statement
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.
The syntax of the If else statement is as follows:
If (condition)
Program statement 1;
Else
Program statement 2;
The following program find whether a number is negative or positive using if 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.
}
Compound Relational tests
To perform compound relational tests,C language provides the necessary mechanisms.
A compound relational test is simple one or more simple relational tests joined together by either the the logical OR operators or logical AND. These operators are represented by character pairs && // respectively.
To form complex expressions in C,the compound operators can be used.
The syntax of the Compound Relational tests is as follows:
a> if (condition1 && condition2 && condition3)
b>if (condition1 // condition2 // condition3)
Nested if Statement
The if statement may itself contain another if statement is known as nested if statement.
The syntax of the Nested if Statement is as follows
if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
The following example print the given numbers along with the largest number using nested if statement.
#include < stdio.h > //includes the stdio.h file to your program
main ( ) //start of main function
{
int a,b,c,big; //declaration of variables
printf ("Enter three numbers"); //message to the user
scanf ("%d %d %d", &a, &b, &c); //Read variables a,b,c,
if (a>b) // check whether a is greater than b if true then
if(a>c) // check whether a is greater than c
big = a ; // assign a to big
else big = c ; // assign c to big
else if (b>c) // if the condition (a>b) fails check whether b is
greater than c
big = b ; // assign b to big
else big = c ; // assign C to big
printf ("Largest of %d,%d&%d = %d", a,b,c,big);
}
//print the given numbers along with the largest number.
Switch Statement
The switch-case statement is a multi-way decision making statement. Unlike
the multiple decision statement that can be created using if-else, the switch
statement evaluates the conditional expression and tests it against the numerous
constant values.During execution,the branch corresponding to the value that the expression
matches is taken.
The value of the expressions in a switch-case statement must have to be an ordinal
type i.e. integer, char, short, long, etc.Double and Float are not allowed.
switch(n) {
case 0:
printf("You typed zero.\n");
break;
case 3:
case 5:
case 7:
printf("n is a prime number\n");
break;
case 2: printf("n is a prime number\n");
case 4:
case 6:
case 8:
printf("n is an even number\n");
break;
case 1:
case 9:
printf("n is a perfect square\n");
break;
default:
printf("Only single-digit numbers are allowed\n");
break;
}
}
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
c branching statement,
if then statement,
while loop statement,
for loop statement,
c break statement,
java statement,
c tutorial,
c syntax,
value statement,
c array,
c string,
c examples,
example statement,