Conditional Branching-Loops
Conditional Branching-Loops
Unit 2: Conditional
branching & Loops
1
Conditional Statements
Conditional Statements in C programming are used to make decisions
based on the conditions.
Conditional statements execute sequentially when there is no condition
around the statements.
It is also called as branching as a program decides which statement to
execute based on the result of the evaluated condition.
SYNTAX:
Program illustrates the use of if construct in 'C'
programming:
If-else statement
This statement also has a single condition with two different
blocks. One is true block and other is false block.
SYNTAX:
Program illustrates the use of if-else construct
in 'C' programming:
Main()
{
int a=5;
If(a%2==0)
{
printf(“The given number is even”);
}
Else
{
Printf(“the given number is odd”);
}
Return 0;
}
Nested If-else statement
When an if statement occurs within another if statement, then
such type of is called nested if statement.
SYNTAX:
Program illustrates the use of Nested if-else
construct in 'C' programming:
If…else if ladder statement
The if-else-if statement is used to execute one code from multiple
conditions.
It is also called multipath decision statement.
It is a chain of if..else statements in which each if statement is
associated with else if statement and last would be an else statement.
SYNTAX:
Program illustrates the use of if..else if ladder
construct in 'C' programming:
Switch statement
Switch statement acts as a substitute for a long if-else-if ladder that is
used to test a list of cases. A switch statement contains one or more
case labels which are tested against the switch expression. When the
expression match to a case then the associated statements with that
case would be executed.
SYNTAX:
Program illustrates the use of switch construct in 'C'
programming:
LOOPS
• A Loop executes the sequence of statements many times
until the stated condition becomes false.
• A loop consists of two parts, a body of a loop and a control
statement. The control statement is a combination of some
conditions that direct the body of the loop to execute until the
specified condition becomes false.
• The purpose of the loop is to repeat the same code a number
of times.
C programming language provides us with three types of loop
constructs:
goto can transfer the program’s within the same block and
there must a label, where you want to transfer program’s
control.
The syntax of goto statement is:
goto label;
... .. ...
... .. ...
label:
statement;