Home | | Data Structures | | Programming and Data Structures I | Conditional Statements

Chapter: Programming and Data Structures

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Conditional Statements

We will look into following conditional statements. 1. if 2. if else 3. else if 4. switch 5. goto

CONDITIONAL STATEMENTS

 

Conditional statements are used to execute a statement or a group of statement based on certain conditions. The ability to control the flow of our program, letting it make decisions on what code to execute, is valuable to the programmer. One of the important functions of the conditional statement is that it allows the program to select an action based upon the user's input.

 

C Statements and Blocks

 

 

C has three types of

statement. Assignment

=

Conditional (branching)  if (expression)

else switch

Control (looping)  while (expression)

for (expression;expression;expression)  do {block}

 

Blocks

 

These statements are grouped into blocks, a block is identified by curly brackets...There are two types of block.

Statement blocks if ( i == j)

 

{

printf("martin \n");

 

}

 

The statement block containing the printf is only executed if the i == j expression evaluates to TRUE.

Function blocks

int add( int a, int b) /* Function definition */

{

int c;

c = a + b;

return c;

}

The statements in this block will only be executed if the add function is called.

 

We will look into following conditional statements.

 

1.     if

2.     if else

 3.     else if

4.     switch

5.     goto

 

1 IF STATEMENT

If statement syntax

if (test expression){

statement/s to be executed if test expression is true;

 

}

If the test expression is true then, statements for the body if, i.e, statements inside parenthesis are executed. But, if the test expression is false, the execution of the statements for the body of if statements are skipped.

Flowchart of if statement


Example of if statement

Write a C program to print the number entered by user only if the number entered is negative.

 

#include <stdio.h> int main(){

int num;

 

printf("Enter a number to check.\n"); scanf("%d",&num);

 

if(num<0) /* checking whether number is less than 0 or not. */ printf("Number=%d\n",num);

 

/*If test condition is true, statement above will be executed, otherwise it will not be executed */ printf("The if statement in C programming is easy.");

 

return 0;

}

 

Output 1

 

Enter a number to check.-2

Number=-2

 

The if statement in C programming is easy.

When user enters -2 then, the test expression (num<0) becomes true. Hence, Number=-2 is displayed in the screen.

 

 

 

 

Output 2

 

Enter a number to check.5

The if statement in C programming is easy.

 

When the user enters 5 then, the test expression (num<0) becomes false. So, the statement for body of if is skipped and only the statement below it is executed.

 

2 IF ..ELSE

If...else statement

The if...else statement is used, if the programmer wants to execute some code, if the test expression is true and execute some other code if the test expression is false.

Syntax of if...else

if (test expression)

 

statements to be executed if test expression is true; else

 

statements to be executed if test expression is false;

 

Flowchart of if...else statement


 

Example of if...else statement

Write a C program to check whether a number entered by user is even or odd

 

#include <stdio.h> int main(){

int num;

 

printf("Enter a number you want to check.\n"); scanf("%d",&num);

 

if((num%2)==0) //checking whether remainder is 0 or not. printf("%d is even.",num);

 

else

 

printf("%d is odd.",num); return 0;

}

 

Output 1

 

Enter a number you want to check. 25

 

25 is odd.

 

Output 2

 

Enter a number you want to check. 2

2 is even.

 

3 IF.. ELSEIF..ELSE

Nested if...else statement (if...elseif....else Statement)

The if...else statement can be used in nested form when a serious decision are involved.

Syntax of nested if...else statement.

if (test expression)

 

statements to be executed if test expression is true; else

 

if(test expression 1)

 

statements to be executed if test expressions 1 is true; else

if (test expression 2)

 

.

.

 

.

else

 

statements to be executed if all test expressions are false;

 

How nested if...else works?

 

If the test expression is true, it will execute the code before else part but, if it is false, the control of the program jumps to the else part and check test expression 1 and the process continues. If all the test expression are false then, the last statement is executed.

 

The ANSI standard specifies that 15 levels of nesting may be continued.

 

Example of nested if else statement

Write a C program to relate two integers entered by user using = or > or < sign.

#include <stdio.h> int main(){

 

int numb1, numb2;

 

printf("Enter two integers to check".\n); scanf("%d %d",&numb1,&numb2);

 

if(numb1==numb2) //checking whether two integers are equal. printf("Result: %d=%d",numb1,numb2);

else

 

if(numb1>numb2) //checking whether numb1 is greater than numb2. printf("Result: %d>%d",numb1,numb2);

 

else

 

printf("Result: %d>%d",numb2,numb1); return 0;

 

}

 

Output 1

 

Enter two integers to check. 5 3

 

Result: 5>3

 

Output 2

 

Enter two integers to check. -4 -4

 

Result: -4=-4

 

 

 

 

4 SWITCH....CASE STATEMENT

 

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. If a programmar has to choose one among many alternatives if...else can be used but, this makes programming logic complex. This type of problem can be handled in C programming using switch...case statement.

 

Syntax of switch...case

switch (expression)

{

 

case constant1:

 

codes to be executed if expression equals to constant1; break;

case constant2:

 

codes to be executed if expression equals to constant3; break;

 

.

.

 

.

default:

 

codes to be executed if expression doesn't match to any cases;

}

 

 

 

In switch...case, expression is either an integer or a character. If the value of switch expression matches any of the constant in case, the relevant codes are executed and control moves out of the switch...case statement. If the expression doesn't matches any of the constant in case, then The default statement is executed.


 

Example of switch...case statement

Write a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and perform the corresponding calculation on the operands.

 

/* C program to demonstrate the working of switch...case statement */

 

/* Program to create a simple calculator for addition, subtraction, multiplication and division */

 

# include <stdio.h> int main(){

 

char operator; float num1,num2;

 

printf("Enter operator +, - , * or / :\n"); operator=getche();

 

printf("\nEnter two operands:\n"); scanf("%f%f",&num1,&num2); switch(operator)

 

{

 

case '+': printf("num1+num2=%.2f",num1+num2); break;

 

case '-':

printf("num1-num2=%.2f",num1-num2); break;

 

case '*': printf("num1*num2=%.2f",num1*num2); break;

 

case '/': printf("num2/num1=%.2f",num1/num2); break;

 

default:

 

/* if operator is other than +, -, * or /, error message is shown */ printf(Error! operator is not correct");

break;

 

}

return 0;

 

}

 

Output

 

Enter operator +, -, * or / :

 

/

 

Enter two operators: 34 3

 

num2/num1=11.33

 

Notice break statement at the end of each case, which cause switch...case statement to exit. If break statement are not used, all statements below that case statement are also executed.

 

 

5 GOTO

 

In C programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

 

Syntax of goto statement

goto label;

 

.............

.............

 

.............

label:

 

statement;

 

In this syntax, label is an  identifier. When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code/s after it.

 


 

Example of goto statement


 

/* C program to demonstrate the working of goto statement.*/

 

# include <stdio.h> int main(){

 

float num,average,sum; int i,n;

 

printf("Maximum no. of inputs: "); scanf("%d",&n); for(i=1;i<=n;++i){

 

printf("Enter n%d: ",i); scanf("%f",&num); if(num<0.0)

 

goto jump; /* control of the program jumps to label jump */ sum=sum+num;

}

 

jump: average=sum/(i-1);

 

printf("Average: %.2f",average); return 0;

 

}

 

Output

 

Maximum no. of inputs: 4

 

Enter n1: 1.5

Enter n2: 12.5

 

Enter n3: 7.2

Enter n4: -1

 

Average: 7.07

 

Though goto statement is included in ANSI standard of C, use of goto statement should be reduced as much as possible in a program.

 

Reasons to avoid goto statement

 

Though, using goto statement give power to jump to any part of program, using goto statement makes the logic of the program complex and tangled. In modern programming, goto statement is considered a harmful construct and a bad programming practice.

 

The goto statement can be replaced in most of C program with the use of break and continue statements. In fact, any program in C programming can be perfectly written without the use of goto statement. All programmer should try to avoid goto statement as possible as they can.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.