Conditional statements in C


Conditional Statements:


Sometimes we want a program to select an action from two or more alternatives. This
requires a deviation from the basic sequential order of statement execution. Such
programs must contain two or more statements that might be executed, but have some
way to select only one of the listed options each time the program is run. This is known
as conditional execution.


if statement:


Statement or set of statements can be conditionally executed using if statement. Here,
logical condition is tested which, may either true or false. If the logical test is true (non
zero value) the statement that immediately follows if is executed. If the logical
condition is false the control transfers to the next executable statement.

The general syntax of simple if statement is:
if (condition)
statement_to_execute_if_condition_is_true;
or
if (condition)
{
statement 1;
statement 2;
_ _ _ _;
}


if – else statement:


The if statement is used to execute only one action. If there are two statements to be
executed alternatively, then if-else statement is used. The if-else statement is a two
way branching. The general syntax of simple if - else statement is:
if (condition)
statement_to_execute_if_condition_is_true;
else
statement_to_execute_if_condition_is_false;

Where, statement may be a single statement, a block, or nothing, and the else
statement is optional. The conditional statement produces a scalar result, i.e., an
integer, character or floating point type.
It is important to remember that an if statement in C can execute only one statement
on each branch (T or F). If we desire that multiple statements be executed on a
branch, we must block them inside of a { and } pair to make them a single
compound statement. Thus, the C code for the flowchart segment above would be:

 

Example:
main()
{
int num;
printf(― Enter a number : ―);
scanf(―%d‖,&num);
if (num % 2 == 0)
printf(― Even Number ―);
else
printf(― Odd Number ―);

}


Nested if statement:


The ANSI standard specifies that 15 levels of nesting must be supported. In C, an else
statement always refers to the nearest if statement in the same block and not already
associated with if.
Example:
main()
{
int num;
printf(― Enter a number : ―);
scanf(―%d‖,&num);
if( num > 0 )
{
if( num % 2 == 0) printf(―Even
Number―);
else
printf(―Odd Number―);

}
else
{
if( num < 0 )
printf(―Negative Number―);
else
printf(― Number is Zero―);

}
}


 if-else-if Ladder


When faced with a situation in which a program must select from many processing
alternatives based on the value of a single variable, an analyst must expand his or her
use of the basic selection structure beyond the standard two processing branches
offered by the if statement to allow for multiple branches. One solution to this is to
use an approach called nesting in which one (or both) branch(es) of a selection
contain another selection. This approach is applied to each branch of an algorithm until
enough additional branches have been created to handle each alternative. The general
syntax of a nested if statement is:
if (expression)
statement1
else if (expression)
statement2
..
..
else
statement3

Example:
#include <stdio.h>
void main (void)
{
int N; /* Menu Choice */ printf ("MENU OF
TERMS\n\n");
printf ("1. Single\n");
printf ("2. Double\n");
printf ("3. Triple\n");
printf ("4. Quadruple\n\n");
printf ("Enter the numbe (1-4): ");
scanf ("%d", &N);
if (N == 1) printf ("one");
else if (N == 2) printf ("two");
else if (N == 3) printf ("three");
else if (N == 4) printf ("four");
else printf ("ERROR");

}

 


The ? : operator (ternary):


The ? (ternary condition) operator is a more efficient form for expressing simple
if statements. It has the following form:
expression1 ? expression2 : expression3
It simply states as:
if expression1 then expression2 else expression3
Example:
Assign the maximum of a and b to
z: main()
{
int a,b,z;
printf(―\n Enter a and b ―);
scanf(―%d%d‖,&a,&b);
z = (a > b) ? a : b; printf(―Maximum
number: %d‖, z);
}
which is the same as:
if (a > b)
z = a;
else
z = b;

Custom Search

%d bloggers like this: