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

Difference between Binary mode and Text Mode in C programming

There are mainly three difference between binary and text mode. They are as follow.               i.       In text mode, a special character EOF whose ASCII value is 26 is inserted after the last character in the file to mark the end of file. But, there is no such special character present in the binary mode. The binary mode files keep track of the end of file from the number of characters present in the directory entry of the file.             ii.       In text mode of file, text and numbers are stored as one character per byte. For example, the number 1234 occupies two bytes in memory but it occupies 4 bytes (one byte per character) in the file. But, in binary mode, the number occupies the number of bytes as it occupies in the memory. Thus,  the above number occupies two bytes in file also in the case o...

CPU (Central Processing Unit)

PGA reffered as pin grid array in which pins of CPU are lined up in a straight format. SPGA referred as staggered pin grid array. in which pins of CPU are arranged staggered format. LGA reffered to as Land Grid Array in which pins are available within inside the socket but not in the CPU in other words in line grid array, in CPU ther are no pinsbut insteadpins areattached with in a socket which contact with with the CPU. for e.g. LGA775 socket(no pins on cpu) has better cooling system. better contact and better locking(climbing). LGA1155: Letest generation I socket, also reffered as sandy bridge, Turbo boost overclocking. More resent than LGA 1156 socket. LGA1156: ability to north bridge Doul channel DDR3 optional integreted graphics PCI express LGA1366: hi end core i series socket integreted tiple channal memmory controller external control bridge(HUB) Upgrading CPU: need to check its core suppert check multiprocessor supports or not check on the ...

Keywords in C programming

There are certain reserved words, called keywords that have standard, predefined meanings in C. These keywords can be used only for their intended purpose, they cannot be used as programmer-defined identifiers. The standard keywords are: auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struc, switch, typedef, union, unsigned, void, volatile, while. The keywords are all lowercase. Since upper and lowercase characters are not equivalent, it is possible to utilize an uppercase keyword as an identifier. Normally, however, this is not done, as it is considered a poor programming practice.