Conditional Statement in
C
S.Muthuganesh M.Sc.,B.Ed
if
• The simplest form of the control statement is the If statement. It is very
frequently used in decision making and allowing the flow of program execution.
• Syntax
if(condition)
{
If body code
}
if...else statement
• The if...else statement executes some code if the test expression is true (nonzero)
and some other code if the test expression is false (0).
• If test expression is true, codes inside the body of if statement is
executed and, codes inside the body of else statement is skipped.
• If test expression is false, codes inside the body of else statement is
executed and, codes inside the body of if statement is skipped.
Nested if..else statement
• One block of code will only be executed if two conditions are true.
• Condition 1 is tested first and then condition 2 is tested. The second if condition
is nested in the first.
• The second if condition is tested only when the first condition is true else the
program flow will skip to the corresponding else statement
if else ladder statement
• First of all condition_expression_1 is tested and if it is true then statement1 will
be executed and control comes out of whole if else ladder.
• If condition_expression_1 is false then only condition_expression_2 is tested.
Control will keep on flowing downward, If none of the conditional expression is
true.
• The last else is the default block of code which will gets executed if none of the
conditional expression is true
if(condition_expression_1)
{
statement1;
}
else if (condition_expression_2)
{
statement2;
}
else if (condition_expression_3)
{
statement3;
}
else
{
default statemnet;
}
Switch
• Switch statement is a control statement that allows us to choose only one choice
among the many given choices.
• The switch statement tests the value of given variable (expression) against list of
case values and when match is found, a block statement associated with that
case is executed.
• If there is no match, then default block is executed (If present)
The following rules apply to
a switch statement −
• The expression used in a switch statement must have an integral or enumerated type,
or be of a class type in which the class has a single conversion function to an integral or
enumerated type.
• You can have any number of case statements within a switch. Each case is followed by
the value to be compared to and a colon(:).
• The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements following that
case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases
is true. No break is needed in the default case.
switch(expression)
{
case value 1:
block-1;
break;
case value 2:
block-2;
break;
case value 3:
block-3;
break;
case value 4:
block-4;
break;
default:
default-block;
break;
}
goto
• In C goto statement to branch unconditionally from one point to another in a
program.
• A label is name, any valid variable name, and must be followed by colon(:).
• A label is placed immediately before the statement where the control is to be
transferred.
goto label;
label:
Statements;

Conditional statement in c

  • 1.
  • 2.
    if • The simplestform of the control statement is the If statement. It is very frequently used in decision making and allowing the flow of program execution. • Syntax if(condition) { If body code }
  • 4.
    if...else statement • Theif...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).
  • 6.
    • If testexpression is true, codes inside the body of if statement is executed and, codes inside the body of else statement is skipped. • If test expression is false, codes inside the body of else statement is executed and, codes inside the body of if statement is skipped.
  • 7.
    Nested if..else statement •One block of code will only be executed if two conditions are true. • Condition 1 is tested first and then condition 2 is tested. The second if condition is nested in the first. • The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement
  • 9.
    if else ladderstatement • First of all condition_expression_1 is tested and if it is true then statement1 will be executed and control comes out of whole if else ladder. • If condition_expression_1 is false then only condition_expression_2 is tested. Control will keep on flowing downward, If none of the conditional expression is true. • The last else is the default block of code which will gets executed if none of the conditional expression is true
  • 10.
    if(condition_expression_1) { statement1; } else if (condition_expression_2) { statement2; } elseif (condition_expression_3) { statement3; } else { default statemnet; }
  • 12.
    Switch • Switch statementis a control statement that allows us to choose only one choice among the many given choices. • The switch statement tests the value of given variable (expression) against list of case values and when match is found, a block statement associated with that case is executed. • If there is no match, then default block is executed (If present)
  • 13.
    The following rulesapply to a switch statement − • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon(:). • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal. • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
  • 14.
    switch(expression) { case value 1: block-1; break; casevalue 2: block-2; break; case value 3: block-3; break; case value 4: block-4; break; default: default-block; break; }
  • 16.
    goto • In Cgoto statement to branch unconditionally from one point to another in a program. • A label is name, any valid variable name, and must be followed by colon(:). • A label is placed immediately before the statement where the control is to be transferred. goto label; label: Statements;