Fundamentals
of
Programming Languages
UNIT::3- Decision Control Statement
Unit:: 3 Outline
Decision Making and Branching: Simple If Statement
The if in C is a decision-making statement that is used to execute
a block of code based on the value of the given expression.
It is one of the core concepts of C programming and is used to
include conditional code in our program.
Syntax of if Statement in C:
if(condition)
{
// if body
// Statements to execute if condition is true
}
Decision Making and Branching: Simple If Statement
Flowchart
Decision Making and Branching: Simple If Statement
The working of the if statement in C is as follows:
STEP 1: When the program control comes to the if statement,
the test expression is evaluated.
STEP 2A: If the condition is true, the statements inside the if
block are executed.
STEP 2B: If the expression is false, the statements inside the if
body are not executed.
STEP 3: Program control moves out of the if block and the code
after the if block is executed.
Decision Making and Branching: Simple If Statement
EX:
int main()
{
int num = 9;
// if statement with true condition
if (num < 10) {
printf("%d is less than 10", num);
}
// if statement with false condition
if (num > 20) {
printf("%d is greater than 20",num);
}
return 0;
}
O/P: 9 is less than 10
Decision Making and Branching: If--else Statement
The if-else statement in C is a flow control statement used
for decision-making in the C program.
It is one of the core concepts of C programming.
It is an extension of the if in C that includes an else block along
with the already existing if block.
Syntax of if-else:
if (condition)
{
// code executed when the condition is true
}
else
{
// code executed when the condition is false
}
Decision Making and Branching: If--else Statement
Flowchart:
Decision Making and Branching: If--else Statement
How if-else Statement works?
Working of the if-else statement in C is explained below:
1. When the program control first comes to the if-else block, the
test condition is checked.
2. If the test condition is true:
The if block is executed.
3. If the test condition is false:
The else block is executed
After that, the program control continues to the statements below
the if-else statement.
Decision Making and Branching: if--else if--else ladder
if else if ladder in C programming is used to test a series of
conditions sequentially.
Furthermore, if a condition is tested only when all previous if
conditions in the if-else ladder are false.
If any of the conditional expressions evaluate to be true, the
appropriate code block will be executed, and the entire if-else
ladder will be terminated.
Decision Making and Branching: if--else if--else ladder
Syntax:
if(condition)
{ // Statement of if block
}
else if(condition)
{ // this else if will be executed when condition in if is false and
// the condition of this else if is true
}
…. // once if-else ladder can have multiple else if
else
{ // at the end we put else
}
Decision Making and Branching: if--else if--else ladder
Flowchart:
Decision Making and Branching: if--else if--else ladder
Working Flow of the if-else-if ladder:
1. The flow of the program falls into the if block.
2. The flow jumps to 1st Condition
3. 1st Condition is tested respectively:
If the following Condition yields true, go to Step 4.
If the following Condition yields false, go to Step 5.
4. The present block is executed. Goto Step 7.
5. The flow jumps to Condition 2.
If the following Condition yields true, go to step 4.
If the following Condition yields false, go to Step 6.
6. The flow jumps to Condition 3.
If the following Condition yields true, go to step 4.
If the following Condition yields false, execute the else block.
Goto Step 7.
7. Exits the if-else-if ladder.
Decision Making and Branching: if--else if--else ladder
EX-1:
int main()
{ int n = 0;
if (n > 0) // all Positive numbers will make this condition true
{ printf("Positive"); }
else if (n < 0) // all Negative numbers will make this condition true
{ printf("Negative"); }
else // if a number is neither Positive nor Negative
{ printf("Zero"); }
return 0;
}
Decision Making and Branching: if--else if--else ladder
EX-2:
int main()
{
int marks = 91;
if (marks <= 100 && marks >= 90)
{ printf("A+ Grade");}
else if (marks < 90 && marks >= 80)
{ printf("A Grade"); }
else if (marks < 80 && marks >= 70)
{ printf("B Grade"); }
else if (marks < 70 && marks >= 60)
{ printf("C Grade"); }
else if (marks < 60 && marks >= 50)
{ printf("D Grade"); }
else
{ printf("F Failed"); }
return 0;
}
Decision Making and Branching: Nested if Statement
A nested if in C is an if statement that is the target of another if
statement.
Nested if statements means an if statement inside another if
statement.
Yes, C allow us to nested if statements within if statements, i.e, we
can place an if statement inside another if statement.
Decision Making and Branching: Nested if Statement
Syntax of Nested if statement:
if (condition1)
{ // Executes when condition1 is true
if (condition_2)
{ // statement 1 }
else
{ // Statement 2 }
}
else
{
if (condition_3)
{ // statement 3 }
else
{ // Statement 4 }
}
Decision Making and Branching: Nested if Statement
Flowchart of Nested if-else:
Decision Making and Branching: Nested if Statement
Ex:
#include <stdio.h> else
int main() {
{ if (i == 20)
int i = 10; {
if (i < 22)
if (i == 10) {
{ printf("i is smaller than 22 too\n");
if (i < 15)
{ else
printf("i is smaller than 15\n"); printf("i is greater than 25");
} }
else }
{ return 0;
printf("i is greater than 15"); }
}
}
Decision Making and Branching: Switch Statement in C
Switch case statement evaluates a given expression and based on the
evaluated value(matching a certain condition), it executes the
statements associated with it.
It is used to perform different actions based on different
conditions(cases).
Switch case statements follow a selection-control mechanism and allow
a value to change control of execution.
They are a substitute for long if statements that compare a variable to
several integral values.
The switch statement is a multiway branch statement.
It provides an easy way to dispatch execution to different parts of code
based on the value of the expression.
Decision Making and Branching: Switch Statement in C
Syntax of switch Statement in C:
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;
default: default_statement;
}
Decision Making and Branching: Switch Statement in C
Rules of the switch case statement:
In a switch statement, the “case value” must be of “char”
and “int” type.
There can be one or N number of cases.
The values in the case must be unique.
Each statement of the case can have a break statement. It is
optional.
The default Statement is also optional.
Decision Making and Branching: Switch Statement in C
How it works:
The switch expression is evaluated once
The value of the expression is compared with the values of
each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and stops the
execution
The default statement is optional, and specifies some code to run
if there is no case match
Decision Making and Branching: Switch Statement in C
EX:
#include <stdio.h> case 4:
int day ; printf("Thursday");
print(“Enter day of Week:”); break;
scanf(“%d”,&day); case 5:
printf("Friday");
switch (day) { break;
case 1: case 6:
printf("Monday"); printf("Saturday");
break; break;
case 2: case 7:
printf("Tuesday"); printf("Sunday");
break; break;
case 3: }
printf("Wednesday");
break; // Outputs : "Thursday" (day 4)
Decision Making and Branching: goto Statement in C
The C goto statement is a jump statement which is sometimes
also referred to as an unconditional jump statement.
The goto statement can be used to jump from anywhere to
anywhere within a function using predefined label.
The goto statement can be used to repeat some part of the code
for a particular condition.
It can also be used to break the multiple loops which can't be
done by using a single break statement.
However, using goto is avoided these days since it makes the
program less readable and complicated.
Decision Making and Branching: goto Statement in C
Syntax: In the below syntax 1, the first line tells the compiler to
go to or jump to the statement marked as a label. Here, the label
is a user-defined identifier that indicates the target statement. The
statement immediately followed after ‘label:’ is the destination
statement. The ‘label:’ can also appear before the ‘goto label;’
statement in the below syntax 2
Syntax1 Syntax2
---------------------------- ----------------------------
goto label; label:
. .
. .
. .
label: goto label;
Decision Making and Branching: goto Statement in C
Syntax:
Decision Making and Branching: goto Statement in C
EX:
// function to check even or not int main()
void checkEvenOrNot(int num) {
{ int num = 26;
if (num % 2 == 0) checkEvenOrNot(num);
// jump to even return 0;
goto even; }
else
// jump to odd
goto odd;
Output
even:
printf("%d is even", num); 26 is even
// return if even
return;
odd:
printf("%d is odd", num);
}
Decision Making and Branching: goto Statement in C
// CEX:
program to print numbers // Driver program to test above function
// from 1 to 10 using goto statement
int main()
#include <stdio.h> {
printNumbers();
// function to print numbers from 1 to 10
return 0;
void printNumbers() }
{
int n = 1;
label10:
printf("%d ", n); Output
n++;
1 2 3 4 5 6 7 8 9 10
if (n <= 10)
goto label10;
}
Decision Making and Branching: goto Statement in C
Disadvantages of Using goto Statement
The use of the goto statement is highly discouraged as it
makes the program logic very complex.
The use of goto makes tracing the flow of the program very
difficult.
The use of goto makes the task of analyzing and verifying the
correctness of programs (particularly those involving loops)
very difficult.
The use of goto can be simply avoided by
using break and continue statements.
Decision Making and Looping:
In programming, loops are used to repeat a block of code until
a specified condition is met.
Loops are handy because they save time, reduce errors, and
they make code more readable.
C programming has three types of loops.
1. for loop
2. while loop
3. do...while loop
Decision Making and Looping: while loop in C
The while Loop is an entry-controlled loop in C programming
language.
This loop can be used to iterate a part of code while the given
condition remains true.
Syntax
while (condition)
{
// code block to be executed
}
Decision Making and Looping: while loop in C
How while loop works?
The while loop evaluates the testExpression inside the
parentheses ().
If testExpression is true, statements inside the body
of while loop are executed. Then, testExpression is
evaluated again.
The process goes on until testExpression is evaluated
to false. If testExpression is false, the loop terminates
(ends).
Decision Making and Looping: while loop in C
How while loop works?
The while loop evaluates the testExpression inside the
parentheses ().
If testExpression is true, statements inside the body
of while loop are executed. Then, testExpression is
evaluated again.
The process goes on until testExpression is evaluated
to false. If testExpression is false, the loop terminates
(ends).
Decision Making and Looping: while loop in C
Flowchart:
Decision Making and Looping: while loop in C
EX:
// Print numbers from 1 to 5 Output:
12345
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
Decision Making and Looping: do-while loop in C
The do and while keywords are used together to form a loop
The do…while in C is a loop statement used to repeat some part of the
code till the given condition is fulfilled.
It is a form of an exit-controlled or post-tested loop where the test
condition is checked after executing the body of the loop.
Due to this, the statements in the do…while loop will always be
executed at least once no matter what the condition is.
Syntax
do
{
// body of do-while loop
} while (condition);
Decision Making and Looping: do-while loop in C
The do and while keywords are used together to form a loop
The do…while in C is a loop statement used to repeat some part of the
code till the given condition is fulfilled.
It is a form of an exit-controlled or post-tested loop where the test
condition is checked after executing the body of the loop.
Due to this, the statements in the do…while loop will always be
executed at least once no matter what the condition is.
Syntax
do
{
// body of do-while loop
} while (condition);
Decision Making and Looping: do-while loop in C
Flowchart:
Decision Making and Looping: do-while loop in C
EX:
#include <stdio.h> Output:
Here, the do-while loop acts as a
int main() counted loop. Run the code and check
{ its output −
// local variable definition
int a = 1; Hello World
Hello World
// while loop execution Hello World
Hello World
do{
Hello World
printf("Hello World\n");
End of loop
a++;
} while(a <= 5);
printf("End of loop");
return 0;
}
Decision Making and Looping: for loop in C
The for loop in C Language provides a functionality/feature to
repeat a set of statements a defined number of times.
The for loop is in itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the
initialization, condition, and updating statements as part of its
syntax.
It is mainly used to traverse arrays, vectors, and other data
structures.
Syntax
for(initialization; check/test expression; updation)
{
// body consisting of multiple statements
}
Decision Making and Looping: for loop in C
Initialization: This step initializes a loop control variable with an
initial value that helps to progress the loop or helps in checking the
condition. It acts as the index value when iterating an array or string.
Check/Test Condition: This step of the for loop defines the condition
that determines whether the loop should continue executing or not. The
condition is checked before each iteration and if it is true then the
iteration of the loop continues otherwise the loop is terminated.
Body: It is the set of statements i.e. variables, functions, etc that is
executed repeatedly till the condition is true. It is enclosed within curly
braces { }.
Updation: This specifies how the loop control variable should be
updated after each iteration of the loop. Generally, it is the
incrementation (variable++) or decrementation (variable–) of the loop
control variable.
Decision Making and Looping: for loop in C
Flowchart:
Decision Making and Looping: for loop in C
EX:
// Print numbers from 1 to 10 Output
#include <stdio.h> 1 2 3 4 5 6 7 8 9 10
int main()
{
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Decision Making and Looping: Nesting of loops In C
Syntax:
for ( initialization; condition; increment )
{
for ( initialization; condition; increment )
{
// statement of inside loop
}
// statement of outer loop
}
Decision Making and Looping: Nesting of loops In C
Syntax:
Decision Making and Looping: Nesting of loops In C
Example:
#include<stdio.h> Output:
Int main()
{ Outer: 1
Inner: 1
int i, j;
Inner: 2
Inner: 3
// Outer loop Outer: 2
for (i = 1; i <= 2; ++i) Inner: 1
{ Inner: 2
printf("Outer: %d\n", i); // Executes 2 times Inner: 3
// Inner loop
for (j = 1; j <= 3; ++j)
{
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
Decision Making and Looping: Nesting of loops In C
Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of
nesting loops in C.
Any number of loops can be defined inside another loop, i.e.,
there is no restriction for defining any number of loops.
The nesting level can be defined at n times.
The "inner loop" will be executed one time for each iteration of
the "outer loop"
You can define any type of loop inside another loop; for example,
you can define 'while' loop inside a 'for' loop.
Decision Making and Looping: Break Statement In C
The break statement plays a crucial role in controlling the flow of
a loop.
It provides a means to exit the loop prematurely, regardless of
whether the loop's termination condition has been satisfied.
In other words, by placing a break statement within the loop
body and specifying a condition, you can determine the exact
moment when the loop should be terminated.
Once this condition is met, the break statement immediately halts
the execution of the loop, allowing the program to proceed to the
next statement outside the current loop iteration.
Decision Making and Looping: Break Statement In C
The syntax for the break statement:
for (initialization; condition; update)
{
// Code block
if (break_condition)
{
break;
}
// More code
}
Decision Making and Looping: Break Statement In C
Flowchart break statement:
Decision Making and Looping: Break Statement In C
How Does The Break Statement Work?
Iteration Check: During each iteration of a loop, the
program checks a specific condition.
Encounter break: If the condition for the break statement is
met, the program encounters the break statement.
Immediate Exit: Upon encountering a break, the control
immediately exits the loop or switch statement, regardless of
whether the loop's conditions are satisfied.
Jump to Next Statement: The control then jumps to the
statement immediately following the terminated loop or
switch.
End of Execution: The loop is effectively terminated, and
the program continues with the rest of the code after the loop
or switch.
Decision Making and Looping: Break Statement In C
Ex.:
#include <stdio.h> Output:
int main() 012345678
{ Exited the loop after i=8
int i;
for(i=0; i<20; i++)
{
printf("%d ",i);
if(i==8) //If this condition is true, break is executed
break;
}
printf("\nExited the loop after i=%d",i);
return 0;
}
Decision Making and Looping: Continue Statement In C
The continue statement allows us to skip a specific iteration in
a loop without stopping the entire loop.
If a condition is met, the iteration is skipped, and the loop
proceeds to the next iteration.
If the condition is not true, the remaining code within the
iteration is executed.
This provides a way to control the flow of execution within a
loop based on specific conditions.
Decision Making and Looping: Continue Statement In C
The syntax for the continue statement:
for (initialization; condition; update)
{
// Code block
if (continue_condition)
{
continue;
}
// More code
}
Decision Making and Looping: Continue Statement In C
The flowchart for the continue statement:
Decision Making and Looping: Continue Statement In C
Ex. <stdio.h>
#include Output:
1 2 3 4 5 6 7 9 10
int main()
{
int i=1;
for(i=1; i<=10; i++)
{
if(i==8)
{
continue;
}
printf("%d ",i);
}
return 0;
}