Skip to main content

Control Statements: Decisions in C programming

     Since decision making statements control the flow of execution, they also fall under the category of control statements. Following are decision making statements:
1. if statements
2. if....else statements
3. else if statement
4. Nested if...else statement
5. switch statement

 if statement:
The if statement is a powerful decision making statement and is used to control the flow of execution of statements. This is a bi-directional condition control statements. This statement is used to test a condition and take one of two possible actions, If the condition is true then a single statement or a block of statements is executed (one part of the program), other wise another single statement or a block of statements is executed (other part of the program). In C, any non-zero value is regarded as true while zero is regarded as false.
syntax :
if (condition) if (condition)
statement1 ; { statement1 ;
- - - - -
statement n ;
}
Here if the condition is true (non-zero) then statement1 is executed, and if it is false(zero), then the next statement which is immediately after the if control statement is executed.
For eg:
|* ### Program to check whether the number is –ve or +ve ### *|
#include<stdio.h>
main()
{
int num ;
printf (“Enter a number to be tested:” ) ;
scanf (“%d”, &num) ;
if (num<0)
printf(“The number is negative”) ;
printf (“value of num is : %d|n”, num) ;
getch() ;
}
Output : 1st run
Enter a number to be tested : -6
The number is negative
Value of num is : -6
2nd run
Enter a number to be tested : 6
Value of num is : 6

if...else statement:
The if..else statement is an extension of the simple if statement. It is used when there are two possible actions – one when a condition is true and the other when it is false.
The syntax is :
if (condition) if (condition)
statement1 ; {
else statement ;
statement2; } - - - -
else
{
statement ;
- - - -
}
Here if the condition is true then statement1 is executed and if it is false then statement2 is executed. After this the control transfers to the next statement which is immediately after the if...else control statement.
|* Program to check whether the number is even or odd *|
#include<stdio.h>
main()
{
int num, remainder ;
printf (“Enter a number:”) ;
scanf (“%d”, &num) ;
remainder = num%2 ; |* modular division *|
if (remainder = = 0) |* test for even *|
printf (“Number is even|n”) ;
else
printf (“ Number is odd|n”) ;
}
Output:
Enter a number : 15
Number is odd.


Nested if ...else statement:

We can have another if... else statement in the if block or the else block. This is called nested if..else statement.
 For example
if(condition1)
{
if(condition2)
statementA1;
else
statement A2;
                 //Here, we have if...else inside both if block and else block
}
else
{
if(condition3)
statementB1;
else
statementB2;
}

|* Program to find whether a year is leap or not *|
#include<stdio.h>
main()
{
int year,
printf (“Enter year: ”) ;
scanf (“%d”, &year) ;
if(year%100 = = 0)
{
if(year%400 = = 0)
printf (“Leap year \n”) ;
else
printf (“Not leap year\n”) ;
}
This can also be written in place of nested if else as
if ((year%4 = = 0 && year %100!=0) | | year%400 = = 0)
printf (“%d is a leap year\n”, year) ;
else
printf (“%d is not a leap year\n”, year) ;
|* Program to find largest number from three given number *|
#include<stdio.h>
main()
{
int a, b, c, large ;
printf (“Enter three numbers : ”) ;
scanf (“%d%d%d”, &a, &b, &c) ;
if (a>b)
{
if (a>c)
large = a ;
else
large – c ;
}
else
{
if (b>c)
large = b ;
else
large = c ;
}
printf (“Largest number is %d\n”, large) ;
} /* End of main() */
Output:
Enter the numbers: 3 4 5
Largest num is 5
else if statement:
This is a type of nesting in which there is an if...else statement in every else part except the last else part. This type of nesting is frequently used in programs and is also k/a else if ladder.

if(condition1) if(condition1)
statementA ; statementA ;
else elseif(condition2)
if(condition2) statementB ;
statementB ; elseif(condition3)
else statementC ;
if (condition3) else
statementC ; statement D ;
else
statement D ;

|* Program to find out the grade of a student when the marks of 4 subjects are given. The
method of assuming grade is as
per>=80 grade = A
per<80 and per>=60 grade = B
per<60 and per>=50 grade = C
per<50 and per>=40 grade = D
per<40 grade = F
Here Per is percentage *|

#include<stdio.h>
main()
{
float m1, m2, m3, m4, total, per ;
char grade ;
printf (“Enter marks of 4 subjects : ”) ;
scanf (“%f%f%f%f”,&m1, &m2, &m3, &m4) ;
total = m1+m2+m3+m4 ;
per = total /4 ;
if(per>=80)
grade = ‘A’ ;
elseif(per>=60)
grade = ‘B’ ;
elseif(per>=50)
grade = ‘C’ ;
elseif(per>=40)
grade = ‘D’ ;
else
grade = ‘F’ ;
printf(“Percentage is %f\n Grade is %c\n”, per, grade) ;
}

Equivalent code in simple if statement:
if(per>=80)
grade = ‘A’ ;
if(per<80 && per>=60)
grade = ‘B’ ;
if(per<60 && per>=50)
grade = ‘D’ ;
if(per<40)
grade = ‘F’ ;


In else_if ladder whenever a condition is found true other conditions are not checked, while in if statement all the conditions will always be checked wasting a lot of time and moreover the conditions here are more lengthy.

Comments

Popular posts from this blog

Object Oriented Programming

Object Oriented Programming Programming paradigm that represents the concept of "objects" that have data fields (attributes that describe the object) and associated procedures known as methods Programming methodology based on objects, instead of just functions and procedures Focuses on data rather than process As individual objects can be modified without affecting other aspects of the program, it is easier for programmers to structure and organize software programs Easier to update and change programs written in object-oriented languages Simula was the first object oriented programming language Eg: C++, Java, etc. Features of OOPS Objects Referred as instance of class Basic run-time entities in an object-oriented system a person, a place, a bank account, a table of data, etc can be an object They occupy space in memory that keeps its state  Each object contains data and code to manipulate the data  Classes Blue print or prototype  which ...

Passing arrays to functions in C programming

Like any other variables, we can also pass entire array to a function. An array name can be named as an argument for the prototype declaration and in function header. When we call the function no need to subscript or square brackets. When we pass array that pass as a call by reference because the array name is address for that array. /* Program to illustrate passing array to function */ #include<stdio.h> void display(int) ; /* function prototype */ main( ) { int num[5] = {100, 20, 40, 15, 33, i ; clrscr( ) ; printf (“\n The content of array is \n”) ; for (i=0; i<5; i++) display (num[i]) ; /*Pass array element fo fun */ getch{ } ; } void display(int n) { printf (“\t%d”, n ) ; } Output:     The content of array is 100      20       40       15 3 /* Program to read 10 numbers from keyboard to store these num into array and then c...

Software Development Life Cycle

Software Crisis In earlier days, same codes needed to be written time and again increasing the complexity and time required for development This resulted in software not being completed on time and the cost increased than expected Solution: Object Oriented Programming Software Evolution Investigation Feasibility Study Analysis Design Implementation Maintenance Investigation First step in preparation of the IS includes the preliminary study of proposed information system investigates the information needs of the users and determines the resource requirement, costs, benefits and feasibility of a proposed project 2. Feasibility Study feasibility study is an analysis of the practicality of an idea focuses on helping answer the essential question of “should we proceed with the proposed project idea?” The feasibility study is conducted in four different areas: 1. Organizational feasibility 2. Economic feasibility 3. Technical feasibility 4. Operation...