2. 1. If statement
• This is the most simple form of the branching
statements.
• It takes an expression in parenthesis and an
statement or block of statements expresses, if
the expression is true then the statement or
block of statements gets executed otherwise
these statements are skipped.
NOTE: Expression will be assumed to be true
if its evaluated values is non-zero.
11/14/2017 VIGNAN'S UNIVERSITY 2
4. 2.If Else Statement:
• An if statement can be followed by an optional else
statement, which executes when the Boolean expression is
false.
• Syntax
if(boolean_expression)
{
statement(s);
}
Else
{
statement(s)
}
11/14/2017 VIGNAN'S UNIVERSITY 4
6. 3. Nested IF....ELSE
• The if...else statement executes two different codes
depending upon whether the test expression is true
or false. Sometimes, a choice has to be made from
more than 2 possibilities.
In Nested if .....else, the if and else part can contain
one or more if else statements.
11/14/2017 VIGNAN'S UNIVERSITY 6
8. 4. ELSE .... IF LADDER
• If we are having different – different test
conditions with different – different
statements, then for these kind of
programming we need else if leader. Else if the
leader is not interdependent to any other
statements or any other test conditions.
11/14/2017 VIGNAN'S UNIVERSITY 8
10. ii. Switch Statement
• The switch statement is often faster than nested
if...else (not always). Also, the syntax of switch
statement is cleaner and easy to understand.
• When a case constant is found that matches the
switch expression, control of the program passes
to the block of code associated with that case.
• The break statement is used to prevent the code
running into the next case.
11/14/2017 VIGNAN'S UNIVERSITY 10
12. II. Looping
• Loops provide a way to repeat commands and
control how many times they are repeated. C
provides a number of looping way.
1. While loop
2. do...while loop
3. for loop
11/14/2017 VIGNAN'S UNIVERSITY 12
13. While loop
• The most basic loop in C is the while loop.A
while statement is like a repeating if
statement. Like an If statement, if the test
condition is true: the statments get executed.
The difference is that after the statements
have been executed, the test condition is
checked again. If it is still true the statements
get executed again. This cycle repeats until the
test condition evaluates to false.
11/14/2017 VIGNAN'S UNIVERSITY 13
14. Syntax
while ( expression )
{
Single statement
or
Block of statements;
}
11/14/2017 VIGNAN'S UNIVERSITY 14
16. do...while loop
• do ... while is just like a while loop except that
the test condition is checked at the end of the
loop rather than the start. This has the effect
that the content of the loop are always
executed at least once.
11/14/2017 VIGNAN'S UNIVERSITY 16
19. for loop
• for loop is similar to while, it's just written
differently.for statements are often used to
proccess lists such a range of numbers:
• In the syntax:
expression1 - Initialisese variables.
expression2 - Condtional expression, as long as this
condition is true, loop will keep executing.
expression3 - expression3 is the modifier which may
be simple increment of a variable.
11/14/2017 VIGNAN'S UNIVERSITY 19