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

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...

Explanation of Graphics in C programming

   The header file graphics.h should be included to use graphics related built in functions. Again, the variable gDriver is an integer variable that specifies graphics driver to be used. Device drivers are small programs which talk directly to the hardware. Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. The initialization of this variable to DETECT is to set another variable gMode to the highest resolution available for the detected driver.  The value of this variable gDriver may be another than DETECT (e.g. 1 for CGA, 9 for VGA.). The variable gMode is an integer that specifies initial graphics mode.       The built in function initgraph() is for graphics initialization. The syntax for this is as   initgraph(&graphics_driver, &graphics_mode, path_to_driver);   path_to_driver specifies the directory path where initgraph() looks for graphics drivers ( .BGI files). Thi...

BIOS

BIOS stands for Basic Input Output System. It is is kind of OS pre- attached in the system Design to detect every I/O devices It holds basic drivers -checks all I/O devices -Loads main OS -Responsible for starting System switched on manufactured by AMI,Phonix,award,Baiostar POST error test, boots trap load, BIOS is unintrusive system. CMOS and BIOS are two different components of the motherboard. CMOS work as storage if BIOS. A basic software program containing all BIOS is permanently stored in the ROM. every essencial holds program Tresh passing without knowinig is called intrusive.