0% found this document useful (0 votes)
530 views

Conditional Control Structure Chap - 04 - Class - 10

The document discusses different conditional control structures in C including if, if-else, else-if, switch, and nested selection structures. It provides the syntax and explains the logic/flow for each structure. The if statement executes code if the condition is true, if-else executes one block if true and another if false. Else-if allows checking multiple conditions. Switch compares a variable to case values. Nested structures can have selections within selections. A ternary operator provides a short-hand for simple if-else assignments.

Uploaded by

abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
530 views

Conditional Control Structure Chap - 04 - Class - 10

The document discusses different conditional control structures in C including if, if-else, else-if, switch, and nested selection structures. It provides the syntax and explains the logic/flow for each structure. The if statement executes code if the condition is true, if-else executes one block if true and another if false. Else-if allows checking multiple conditions. Switch compares a variable to case values. Nested structures can have selections within selections. A ternary operator provides a short-hand for simple if-else assignments.

Uploaded by

abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CONDITIONAL CONTROL

STRUCTURE IN C
By Abbas Ghalib
CONTENTS
▪ Control Structure
▪ Control statement
▪ Conditional statement
▪ Structure of IF Statement
▪ Structure if IF-ELSE Statement
▪ Structure if ELSE-IF Statement
▪ SWITCH Statement
▪ Nested Selection Structure
CONTROL STRUCTURE
▪ In our daily life, we have to make different decisions.
▪ For example:
We will plan a trip to Muree tomorrow if there is snow fall tomorrow.
But what if there is no snow fall tomorrow????
May be, we have few other choices.

▪ In Computer Programming,
We come across many situations where we must make decisions
and perform operations depending upon the users input not the
program.
▪ Control structures are used to implement these decisions.
CONTROL STATEMENT
▪ An instruction that determines the sequence of execution of other
statements.
▪ It controls the flow of execution of program statements.
CONDITIONAL STATEMENT
▪ An instruction in Programming language that contains a condition.
▪ When a conditional statement is executed, first the condition is evaluated
and then based on the result (True / False), a particular statement or set of
statements is executed.
▪ C language has the following Conditional statements:
1. if
2. if-else
3. else-if
4. switch

▪ Note: Conditions are Boolean expressions whose results may be True or


False.
STRUCTURE OF IF STATEMENT
▪ General syntax:
If(condition)
{
Block of statements
}

▪ When this statement is executed, the condition is


evaluated first.
▪ If the result is True, then the block of statements
within the braces will be executed.
▪ If the result is False, then block of statements within
the braces will be skipped and control will be
transferred to statements after the if block if any
exists.
▪ If there is only one statement to be execute in the if
block, then braces are not required.
STRUCTURE OF IF-ELSE STATEMENT
▪ The if-else statement is used in situation where we
have two choices i-e some code is to be executed if
a condition is True and some other code to be
executed if the condition is False.
▪ General syntax:
If(condition)
{
Block of statements
}
Else
{
Block of statements
}
STRUCTURE OF IF-ELSE STATEMENT
▪ When this statement is executed, the
condition is evaluated first.
▪ If the result is True, then the block of
statements following if will be
executed and the block of statements
following else will be skipped..
▪ If the result is False, then block of
statements following else will be
executed and the block of statements
following if will be skipped.
▪ If there is only one statement to be
execute in the if or else block, then
braces are not required.
STRUCTURE OF ELSE-IF STATEMENT
▪ The else-if combines more than two
conditions.
▪ It allows the programmer to make a decision
based on several conditions.
STRUCTURE OF ELSE-IF STATEMENT
▪ General syntax:
if(condition-1)
{
Block of statements
}
else-if(condition-2)
{
Block of statements
}
.
.
else
{
Block of statements
}
STRUCTURE OF
ELSE-IF STATEMENT
• When this statement is executed, condition-1 is
evaluated, if it is True then the block of statements
following if is executed and if it is False, the next
condition is evaluated.
• If any condition is True, then block of statements
following that condition will be executed.
• If none of the conditions is True then the block of
statements following else is executed automatically.
• If there is only one statement to be execute in the if,
else-if or else block, then braces are not required.
STRUCTURE OF SWITCH STATEMENT
▪ General Syntax
switch(expression)
{
     case const-1:
          statements;
           break;
     case const-2:
          statements;
           break;
.
.
.
     default:
          statements;
}
STRUCTURE OF SWITCH STATEMENT
▪ The switch statement is very similar to if-else
statement. It is used when multiple choices are
given and only one choice is to be selected.
▪ When switch statement is executed, the
expression is evaluated. Based on the result of
expression one of the cases in the
switch statement is executed. The result of
expression is compared with the constant
value after the keyword case. If the result
matches the constant value after any case then
the statements under that case are executed.
▪ In switch statement, it is allowed to use a
variable within the parenthesis instead of an
expression based on which statements under a
case can be executed.
STRUCTURE OF SWITCH STATEMENT
▪ The purpose of break statement is to exit the
body of the switch statement after executing
the statements under a case and transfer
control to the first statement following the end
of the switch statement.
▪ If no case is matched, then the statements
under the default keyword are executed. Its
use is optional. If it is not used, then
the control exits from the body of the
switch statement and goes to the first
statement following the end of the
switch statement.
▪ The expression should be of type int, char but
not float.
ADVANTAGE AND LIMITATION OF
SWITCH STATEMENT
▪ The switch statement allows a variable to be compared against a
list of constants values. When there is a match to a case, the
statements following that case will execute until a
break statement is reached. This makes the logic of program
simple and easy to understand.
▪ The switch statement has a limitation. It is not allowed to use
relational operators in the expression of switch statement. For
example, to check for passing marks, the condition (marks>32)
cannot be used in switch statement. It is also not possible to
check for a range such as (marks >= 70) && (marks <= 80) in a
switch statement, for this programmer must use if, if-else or
else-if statement.
NESTED SELECTION
STRUCTURE
The selection structure within another
selection structure is known as nested
selection structure.
Sometimes, it is required in
computer programming to use such structures.
C language supports such structures. In C
language, the programmer can have a
selection structure (if, if-else, else-if or
switch statement) inside another selection
structure.
CONDITIONAL
(TERNARY)
OPERATOR
General syntax:
condition? expression 1 : expression 2;
When this expression is evaluated, the
condition is evaluated. If it is true, the entire
conditional expression takes on the value of
expression 1. if it is false, the conditional
expression takes on the value of expression 2.
The entire conditional expression take on a
value and can therefore be used in an
assignment statement.
CONDITIONAL (TERNARY) OPERATOR
▪ FOR EXAMPLE
a = (k>15)? X*y : x+y;

▪ This statement is very similar to if-else statement:


if (k>15)
        a = x * y;
else
        a = x + y;

Note:
Some programmers may prefer if-else rather than using conditional operator
because it is easy to understand.

You might also like