Archive for 04. Branching Statement in C

Branching Statement in C


The C language programs follows a sequential form of execution of statements. Many times it is required to alter the flow of sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called as control statements. 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:
1. If Statement
2. The If else Statement
3. Compound Relational tests
4. Nested if Statement
5. Switch Statement

If Statement
If statement is the simplest form of the control statement. It is very frequently used in allowing the flow of program execution and decision making.
The If structure has the following syntax


The command says that 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; // If it is negative then convert it into
Printf (“The absolute value is % d \n”, number); // print the value

The If else Statement
The if else is actually just an extension of the general format of if statement. If the result of the condition is true, then program statement 1 is executed else program statement 2 will be executed. The syntax of the If else statement is as follows:
If (condition)

Program statement 1;
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 called as nested if statement. The syntax of the Nested if Statement is as follows
if (condition1)
if (condition2)

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.
The syntax of switch statement is as follows :
switch( expression )
case constant-expression1: statements1;
[case constant-expression2: statements2;]
[case constant-expression3: statements3;]
[default : statements4;]
int n=7;

switch(n) {
case 0:
printf(“You typed zero.\n”);
case 3:
case 5:
case 7:
printf(“n is a prime number\n”);
case 2: printf(“n is a prime number\n”);
case 4:
case 6:
case 8:
printf(“n is an even number\n”);
case 1:
case 9:
printf(“n is a perfect square\n”);
printf(“Only single-digit numbers are allowed\n”);

Leave a Comment


Get every new post delivered to your Inbox.