Oct 14, 2019

CONDITIONAL STATEMENT (if, if-else, else if)

THE DECISION CONTROL STATEMENT:

“The decision control statement also known as conditional statement which used in program to check the condition between vales and run statement and block of statement on the basis of that condition”.
  In C and C++ the beginning programs are sequential and that execute normally one line after another. But in some circumstance we have to execute some instruction on the basis of some condition if the condition become true then the instruction or block of code has to run otherwise not.  Now here we use decision control statement and the program falls in the conditional statement program type.
  Basically the compilers use three major decision making instructions. Which are,
           a.       If statement
           b.      If- else statement
           c.       Switch statement
 Else-if clause also used. Less popular structure also use for decision making which is conditional operator.

IF STATEMENT:

 A conditional statement instruction which execute a statement or block of statement depending on whether the expression in conditions is true or not “.   
The first and the popular decision making instruction which is used when only condition to be check.

SYNTAX:

  The general form of if statement look like this,
If (condition)
  {
    Execute this ;
  }

Note: The condition following the keyword if statement is always enclosed in a pair of parenthesis.  Where if the condition became true then the instruction is execute under the control of if statement otherwise instead the program skip past it. Also if only one statement is execute under control of if statement then no need to enclosed the statement in pair of braces.
  But how do we express the condition in C? As by general rule we use relational operators to express the condition. The relational operator allows us/C to compare between two values. Conditions may be this type see in the table. Let x and y be two variables.

This expression (condition)
Is true if
X==y
X equal to y
x!= y
X is not equal to y
X < y
X is less than y
X > y
X is greater than y
X <= y
X is less then equal to y
X >= y
X is greater than equal to y


Example program:
 #include<stdio.h>
void main (void
{
int a =11;
if (a>=8)
printf (“it is not an octal number”);
getch ();
}

IF ELSE STATEMENT:

The conditional statement which run a statement or block of statement depending on the condition expression, if the condition true then run/execute the statement following keyword if statement otherwise the statements which following the keyword  else statement”.

SYNTAX:

  The general form of if statement look like this,
if (condition)
{
Execute this;
}
else
{
Execute this;
}

Example program:
#include<stdio.h>
void main (void
{
int a =11;
if (a<=7)
printf (“it’s an octal number”);
else
printf(“it’s not  an octal number”);
getch ();
}


         ·         Else-if clause, click to read more 
         ·         Nested if statement,  click to read more
         ·         Switch statement, click to read more
         ·         Programs to explained conditionalstatement, click to read more
if statement in c, if else statement,
conditional statement



      Download page in pdf
 download conditional statement pdf       

0 comments:

Post a Comment

Please do not enter any spam link in the comment box