You are on page 1of 35

PROG0101 Fundamentals of Programming

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.

There are following types of conditional statements in C.


• If statement
• If-Else statement
• Nested If-else statement
• If-Else If ladder
• Switch statement
if statement
• The if statement is a powerful decision making statement which can
handle a single condition or group of statements. These have either
true or false action....
When only one condition occurs in a statement, then simple if
statement is used having one block.

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:

1. The while loop


2. The do-while loop
3. The for loop
Types of Loops in C
Depending upon the position of a control statement in a
program, looping in C is classified into two types:

1. Entry controlled loop(while)


2. Exit controlled loop(do while)

• In an entry controlled loop, a condition is checked before


executing the body of a loop. It is also called as a pre-
checking loop.
• In an exit controlled loop, a condition is checked after
executing the body of a loop. It is also called as a post-
checking loop.
While Loops in C
It is an entry-controlled loop.
In while loop, a condition is evaluated before processing a body
of the loop.
If a condition is true then and only then the body of a loop is
executed. After the body of a loop is executed then control again
goes back at the beginning, and the condition is checked if it is
true, the same process is executed until the condition becomes
false. Once the condition becomes false, the control goes out of
the loop.
After exiting the loop, the control goes to the statements which
are immediately after the loop.
In while loop, if the condition is not true, then the body of
a loop will not be executed, not even once.

SYNTAX of While Loop:


Program illustrates while loop in C programming example:
Do While Loops in C
It is called an exit-controlled loop.
In the do-while loop, the body of a loop is always executed at
least once. After the body is executed, then it checks the
condition. If the condition is true, then it will again execute the
body of a loop otherwise control is transferred out of the loop.
A do...while loop in C is similar to the while loop except that the
condition is always executed after the body of a loop.

SYNTAX of do...while loop


Program illustrates do-while loop in C programming example:
Difference between while and do-while

The critical difference between the while and do-while loop is


that in while loop the while is written at the beginning.
In do-while loop, the while condition is written at the end and
terminates with a semi-colon (;)
For loop in C
A for loop is a more efficient loop structure in 'C'
programming. The general structure of for loop syntax in C is
as follows:

• The initial value of the for loop is performed only once.


• The condition is a Boolean expression that tests and
compares the counter to a fixed value after each iteration,
stopping the for loop when false is returned.
• The incrementation/ decrementation increases (or decreases)
the counter by a set value.
Program illustrates the for loop in C programming example:
In C, the for loop can have multiple expressions separated by
commas in each part.

Also, we can skip the initial value expression, condition and/or


increment by adding a semicolon.
Nested Loop
C programming allows to use one loop inside another loop.
Loops can also be nested where there is an outer loop and an inner loop.
For each iteration of the outer loop, the inner loop repeats its entire cycle.
Break Statement

Suppose we are writing a program to search for a particular


number among 1000 numbers. In the 10th iteration, we have
found the desired number. At this point, we don't want to
transverse the remaining 990 numbers instead we want the
loop to terminate and continue with the execution of statement
following the loop. This is where the break statement comes
into the play.

When break statement is encountered within the loop, the


program control immediately breaks out of the loop and
resumes execution with the statement following the loop.
Its syntax is :
break;
Example:
Continue Statement
The continue statement is used to prematurely end the current
iteration and move on to the next iteration. When
the continue statement is encountered in a loop, all the
statements after the continue statement are omitted and the
loop continues with the next iteration.

When the continue statement is encountered in the while and


do while loop, the control is transferred to the test condition
and then the loop continues whereas in the for loop
when continue statement is encountered the control is
transferred to the update expression, then the condition is
tested.
Its syntax is:
continue;

Difference between break statement and continue


statement:
When break statement is encountered it breaks out of
the loop, but when the continue statement is
encountered, the loop is not terminated instead the
control is passed to the beginning of the loop.
Example:
goto and labels

goto is a jumping statement in c language, which transfer the


program’s control from one statement to another statement
(where label is defined).

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;

Label is defined by the following syntax:


label_name:
• label_name should be a valid identifier name.
• : (colon) should be used after the label_name.
Example:

You might also like