2. Repeating important sections of the program.
Selecting between optional section of a program
Decision
Making
Statement
Decision
Making and
Branching
IF Statement
SWITCH
Statement
Conditional
Operator
Statement
GOTO
Statement
Decision
Making and
Looping
The WHILE
Loop
The do-while
Loop
The FOR
Loop
3. • DECISION MAKING AND BRANCHING
In programming the order of execution of instructions may
have to be changed depending on certain conditions.
This involves a kind of decision making to see whether a
particular condition has occurred or not and then direct the
computer to execute certain instructions accordingly.
• DECISION MAKING AND LOOPING
Execution of the statement or set of statement repeatedly is
known as LOOPING.
This may be executed a specified number of times and this
depends on the satisfaction of a test condition.
A program loop is made up of two parts one part is known as
body of the loop and the other is known as control condition.
Depending on the control condition statement , the statements
within the loop may be executed.
4. The IF statement is the powerful decision making
statement and is used to control the flow of execution of
the statements.
When decision making in a program they are simply the
result of computation in which the final result is either
TRUE or FALSE.
The value zero (0) is considered to be FALSE in program.
Any positive or negative value is considered to be TRUE.
Levels of complexity for IF:
1.Simple IF statement
2.IF…..ELSE statement
3.NESTED IF…..ELSE statement
4.ELSE…..IF ladder
5. It is used to control the flow of execution of the
statements and also to test logically whether the
condition is true or false.
Syntax:
if (condition)
{
statement ;
}
6. If the condition is true then the statement following
the “if” is executed if it is false then the statement is
skipped.
( Flow chart of Simple if statement )
Test
Condition
Executable X - Statement
TRUE
7. The if…..else statement is an extension of the simple if
statement.
Syntax:
if (condition)
{
statement 1; (if the condition is TRUE this statement will be executed)
}
else
{
statement 2; (if the condition is FALSE this statement will be executed)
}
8. It is used to execute some statements when the
condition is true and execute some other statements
when the condition is false depending on the logical
test.
( Flow chart of if…..else statement )
Test
Condition
Executable X - StatementExecutable Y - Statement
TRUEFALSE
9. When a series of if…..else statements are occurred in a program, we can write
an entire if…..else statement in another if…..else statement called NESTING.
Syntax:
If (condition 1)
{
if (condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
if (condition 3)
statement 3;
else
statement 4;
}
11. When a series of decisions are involved we have to use more than one
if…..else statement called as multiple if’s. Multiple if…..else statements
are much faster than a series of if…..else statements, since their
structure when any of the condition is satisfied.
Syntax:
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
----------------
----------------
else if (condition n)
statement n;
else
statement x;
12. ( Flow chart of else…..if ladder )
Test
Condition 1
Statement 1
TRUE
Test
Condition
3
FALSE
Statement 2
TRUETest
Condition 2
TRUE
Statement 2
FALSE
FALSE
Test
Condition
n
Statement n
Statement X
TRUE
FALSE
13. The control statement which allows us to make decision from
the number of choices is called switch or switch case
statement.
It is a multi way decision statement, it test the given variable
or expression against a list of case values.
switch (expression)
{
case constant 1:
block 1
break;
case constant 2:
block 2
break;
----------
----------
default:
default block
break;
}
Statement –x;
14. (Selection process of the SWITCH Statemen
Switch
expression
Block-1
Block-2
default
block
Statement-x
Expression = value -1
Expression = value -2
(no match) default
15. This operator is a combination of ? And : , and takes
three operands.
General form:
conditional expression ? exp-1:exp-2;
This concept says that if this expression is true then
whatever we are writing after this question mark
statement, it will be executed.
And if this expression is false then whatever
we are writing after this colon statement, it will be
executed.
16. Like many other languages, C supports the GOTO
statement to branch unconditionally from one point to
another in the program.
Although it may not be essential to use the GOTO
statement in a Highly Structured Language.
General form:
Not recommended but may be used occasionally.
goto label;
……….
……….
……….
label:
statement;
17. A WHILE loop has one control expression, and
executes as long as that expression is true.
A WHILE loop is an entry controlled loop.
Syntax:
While (condition)
{
statement (s);
increment or decrement loop counter
}
19. The body of the loop may not be executed if the condition is not
satisfied in while loop.
Since the test is done at the end of the loop, the statement in the
braces will always be executed at least once.
The statements in the braces are executed repeatedly as long as
the expression in the parentheses is true.
Make a note that do while loop ends in a ; (semicolon)
Note that Do…..While Looping Statement is Exit Controlled
Looping Statement.
Syntax:
initialize loop counter;
Do
{
statement (s);
increment or decrement loop counter
}
While (condition);
21. The for loop is another repetitive control structure, and is
used to execute set of instruction repeatedly until the
condition becomes false.
To set up an initial condition and then modify some value to
perform each succeeding loop as long as some condition is
true.
The three expressions:
expr1 – sets up the initial condition,
expr2 – tests whether another trip through the loop should
be taken,
expr3 – increments or updates thing after each trip.
Syntax:
for(expr1; expr2; expr3)
{
body of the loop;
}