Wednesday, August 28, 2019

Conditional statements in programming languages

Conditional Statements In Programming Languages

Conditional statements in programming languages

In every programming language to specify the order of execution of particular statements we need to make decisions on the basis of the nature of some particular  expressions that if they are true(1) or false(0).
To do so we have following conditional statements: -
If statement
If-else statement
Nested if-else statement
If-else-if ladder
Switch statement
The above listed statements have different scopes and manner of usage which we shall discuss below in detail.
 These statements work till the condition is true and when the condition is false then the compiler simply skips the statements and passes the command to the statement written outside these conditional statements.

If statement: -


The single if statement is used when the given statement is true. And if the statement is false then the compiler skips the statement under ‘if’ condition.

Syntax: -

if(expression)
{
Statements //codes to be executed
}


Working: -


If the expression is true then the statements written within parenthesis are executed.
If the expression is false then the statements outside the parenthesis are executed.

Example for if statement: -

#include<stdio.h>
#include<conio.h>
void main()
{
Int a;
clrscr();
printf(“enter the number::”);
scanf(“%d”,&a);
if(a%2==0)
{
printf(“%d is even”,a);
}
getch();
}


Output:

enter the number::8
8 is even


If-else statement: -


The if-else statement is used when there are possibilities of the condition of getting true or false. We may also denote this as two way selection statement.

Syntax: -

ifexpression)
{
Statements //codes to be executed if expression is true
}
else
{
Statements //codes to be executed if expression is false
}


Working: -


If the expression is true then the if block will be executed.
If the expression is false then the control will be passed to else block and it will be executed.

Example for if-else statement: -

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“enter a number::”);
scanf(“%d”,&a);
if(a<0)
{
printf(“number is negative”);
}
else
{
printf(“number is positive”);
}
getch();
}


Output:

enter a number::67
number is positive


Nested if-else statement: -


The nested form of if-else statement is used when there are more than one expression/ condition to be checked and operated. This statement could also be denoted as multi-way selection statement.

Syntax: -

if(expression 1)
{
if(expression 2)  //if expression 1 is true
{
Statements //codes to be executed if expression 2 is true
}
else
{
Statements //codes to be executed if expression 2 is false
}
}
else
{

Statements //codes to be executed if expression 1 is false
}


Working: -


If the condition is true then the control enters the if block and executes the sub conditions under it (further execution will be same as of if-else statement).
If the condition is false then the compiler executes the else block.

Example for nested if-else statement: -


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter 3 numbers::\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
If(a>c)
{
printf(“a is greatest”);
}
else
{
printf(“c is greatest”);
}
}
else
{
If(b>c)
{
printf(“b is greatest”);
}
else
{
printf(“c is greatest”);
}
}
getch();
}



Output:

enter 3 numbers::
6
5
7
c is greatest


if-else-if  ladder: -


A chain of if-else-if statement where each if statement is associated with an else-if condition is used when we have to execute one code from multiple conditions.
The last statement is always an else statement.

Syntax: -

if(expression 1)
{
Statements  //codes to be executed if expression 1 is true
}
else if(expression 2)
{
statements   //codes to be executed if expression 2 is true
}
else
{
Statements  //codes to be executed if all the expressions are false
}

  

Working: -


if the expression is true then the if block will be executed.
If the expression is false then the control passes to else if statement and if the expression (within else-if) is true then the respective block will be executed and so on.
If the expression (within else-if) is false then the compiler will execute the else block

Note: - 

The compiler will check all the expressions in each and every else-if block inspite of their numbers.

Example for if-else-if ladder: -

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“enter marks::”);
scanf(“%d”,&a);
if(a>=90)
{
printf(“Grade::A”);
}
else if((a>=70)&&(a<90))
{
printf(“Grade::B”);
}
else if((a>=50)&&(a<70))
{
printf(“Grade::C”);
}
else
{
printf(“Fail!!”);
}
getch();
}



Output: -

enter marks::59
Grade::C


Switch statement: -


Sometimes our source code become complex due to long if-else-if ladders and hence reduces the effectiveness of the program.
To solve this problem C-programming language provide us the concept of switch statements.
A switch statement is the substitute to the if-else-if ladder.

Syntax:-

switch(expression)
{
case 1:
{
Statements //codes to be executed if expression satisfies case 1
break;
}
case 2:
{
Statements //codes to be executed if expression satisfies case 2
break;
}
case 3:
{
Statements //codes to be executed if expression satisfies case 3
break;
}
default
{
Statements //codes to be executed if expression doesn’t satisfy any case
}
}



Working: -


According to the expression the respective case statements are executed and at the end of each case statement there is a break statement which breaks the loop and passes the control out of the switch statement.
If any of the case expression is not true then the compiler will execute the default statement.

Note: -


There should be a colon (:) following the case label.
Case label should be constants.
There must be a default statement.
Break statement is optional.

Example for switch statement: -

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int a,b,c;
printf(“enter the numbers\n”);
scanf(“%d%d”,&a,&b);
printf(“enter the operation to be performed\n”);
scanf(“ %c”,&ch);
switch(ch)
{
case '+':
c=a+b;
printf(“add=%d”,c);
break;
case '-':
c=a-b;
printf(“sub=%d”,c);
break;
case '*':
c=a*b;
printf(“mul=%d”,c);
break;
case '/':
c=a/b;
printf(“div=%d”,c);
break;
case '%':
c=a%b;
    printf(“mod=%d”,c);
break;
default:
printf(“invalid selection”);
}
getch();
}


Output: -

enter the numbers::
2
3
enter the operation to be performed::
+
add=5




No comments:

Post a Comment