0% found this document useful (0 votes)
13 views35 pages

PSS Unit 3

The document covers decision-making constructs in C programming, including if statements, if-else statements, nested if-else, if-else-if ladders, and switch statements. It also discusses operators in C, such as arithmetic, relational, logical, bitwise, and assignment operators, along with looping statements like for loops, while loops, and do-while loops. Additionally, it explains infinite loops and nested loops with examples for better understanding.

Uploaded by

karishmasuga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views35 pages

PSS Unit 3

The document covers decision-making constructs in C programming, including if statements, if-else statements, nested if-else, if-else-if ladders, and switch statements. It also discusses operators in C, such as arithmetic, relational, logical, bitwise, and assignment operators, along with looping statements like for loops, while loops, and do-while loops. Additionally, it explains infinite loops and nested loops with examples for better understanding.

Uploaded by

karishmasuga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT-III Decision Making – Branching & Looping

Decision making – Relational Operators – Conditional statement,


Looping Statements – Nested loops – Infinite loops – Switch Statements

DECISION MAKING/ CONDITIONAL STATEMENT

Decision-making in C involves using conditional and control flow


statements to guide the program's execution based on specific
conditions. Here’s a breakdown of the primary decision-making
constructs in C:

1. if Statement
The if statement is the most simple decision-making statement. It is
used to decide whether a certain statement or block of statements will
be executed or not i.e if a certain condition is true then a block of
statements is executed otherwise not
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}

1
Example of if in C
// C program to illustrate If statement

#include <stdio.h>

int main()

int i = 10;

if (i > 15) {

printf("10 is greater than 15");

printf("I am Not in if");

Output
I am Not in if

2. if-else Statement
he if-else statement in C is a conditional control structure that
allows the program to execute one block of code if a specified
condition is true, and a different block of code if the condition is
false.

2
Syntax of if else in C:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

Example of if-else:
// C program to illustrate If statement

#include <stdio.h>

int main()

int i = 20;

if (i < 15) {

printf("i is smaller than 15");

else {

3
printf("i is greater than 15");

return 0;

Output
i is greater than 15
3. Nested if-else Statement
A nested if in C is an if statement that is the target of another if
statement. Nested if statements mean 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.
Syntax of Nested if-else
if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}

4
Example of Nested if-else
// C program to illustrate nested-if statement

#include <stdio.h>

int main()

int i = 10;

if (i == 10) {

// First if statement

if (i < 15)

printf("i is smaller than 15\n");

// Nested - if statement

// Will only be executed if statement above

// is true

if (i < 12)

5
printf("i is smaller than 12 too\n");

else

printf("i is greater than 15");

else {

if (i == 20) {

// Nested - if statement

// Will only be executed if statement above

// is true

if (i < 22)

printf("i is smaller than 22 too\n");

else

printf("i is greater than 25");

return 0;

i is smaller than 15
i is smaller than 12 too
4. if-else-if Ladder
The if else if statements are used when the user has to decide among
multiple options. The C if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement
will be executed. if-else-if ladder is similar to the switch statement.
Syntax of if-else-if Ladder
if (condition)
statement;
6
else if (condition)
statement;
.
.
else
statement;

Example of if-else-if Ladder


// C program to illustrate nested-if statement

#include <stdio.h>

int main()

int i = 20;

if (i == 10)

printf("i is 10");

else if (i == 15)

printf("i is 15");

else if (i == 20)

printf("i is 20");

else

printf("i is not present");

7
Output
i is 20
5. switch Statement in C
The switch case statement is an alternative to the if else if ladder that
can be used to execute the conditional code based on the value of the
variable specified in the switch statement. The switch block consists of
cases to be executed based on the value of the switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}

Example of switch Statement


// C Program to illustrate the use of switch statement

#include <stdio.h>

8
int main()

// variable to be used in switch statement

int var = 2;

// declaring switch cases

switch (var) {

case 1:

printf("Case 1 is executed");

break;

case 2:

printf("Case 2 is executed");

break;

default:

printf("Default Case is executed");

break;

return 0;

Output
Case 2 is executed
OPERATORS:

An operator in C can be defined as the symbol that helps us to perform


some specific mathematical, relational, bitwise, conditional, or logical
computations on values and variables. The values and variables used
with operators are called operands. So we can say that the operators
are the symbols that perform operations on operands.

9
Types of Operators in C
C language provides a wide range of operators that can be classified
into 6 types based on their functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operations in C
The arithmetic operators are used to perform arithmetic/mathematical
operations on operands. There are 9 arithmetic operators in C
language:

2. Relational Operators in C

10
The relational operators in C are used for the comparison of the two
operands. All these operators are binary operators that return true or
false values as the result of comparison.

3. Logical Operator in C
Logical Operators are used to combine two or more
conditions/constraints or to complement the evaluation of the original
condition in consideration. The result of the operation of a logical
operator is a Boolean value either true or false.

4. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then the
calculation is performed on the operands. Mathematical operations
such as addition, subtraction, multiplication, etc. can be performed at
the bit level for faster processing.

11
5. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left
side operand of the assignment operator is a variable and the right side
operand of the assignment operator is a value. The value on the right
side must be of the same data type as the variable on the left side
otherwise the compiler will raise an error.

6. Other Operators
Apart from the above operators, there are some other operators
available in C used to perform some specific tasks. Some of them are
discussed here:
sizeof Operator
 sizeof is much used in the C programming language.
 It is a compile-time unary operator which can be used to compute
the size of its operand.
 The result of sizeof is of the unsigned integral type which is usually
denoted by size_t.
 Basically, the sizeof the operator is used to compute the size of the
variable or datatype.
 Syntax
 sizeof (operand)

12
Comma Operator ( , )
 The comma operator (represented by the token) is a binary operator
that evaluates its first operand and discards the result, it then
evaluates the second operand and returns this value (and type).
 The comma operator has the lowest precedence of any C operator.
 Comma acts as both operator and separator.
 Syntax
 operand1 , operand2
Conditional Operator ( ? : )
 The conditional operator is the only ternary operator in C++.
 Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return the
result of Expression2 otherwise if the condition(Expression1) is false
 Syntax
 operand1 ? operand2 : operand3;

LOOPING STATEMENTS:
Loops in programming are used to repeat a block of code until the
specified condition is met. A loop statement allows programmers to
execute a statement or group of statements multiple times without
repetition of code.
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops the test condition
is checked before entering the main body of the loop. For Loop and
While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is
evaluated at the end of the loop body. The loop body will execute at
least once, irrespective of whether the condition is true or false. do-
while Loop is Exit Controlled loop.

13
for Loop
for loop in C programming is a repetition control structure that allows
programmers to write a loop that will be executed a specific number of
times. for loop enables programmers to perform n number of steps
together in a single line.
Syntax:
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
Example:
for(int i = 0; i < n; ++i)
{
printf("Body of for loop which will execute till n");
}
In for loop, a loop variable is used to control the loop. Firstly we
initialize the loop variable with some value, then check its test
condition. If the statement is true then control will move to the body and
the body of for loop will be executed. Steps will be repeated till the exit
condition becomes true. If the test condition will be false then it will
stop.
 Initialization Expression: In this expression, we assign a loop
variable or loop counter to some value. for example: int i=1;
 Test Expression: In this expression, test conditions are performed.
If the condition evaluates to true then the loop body will be executed
and then an update of the loop variable is done. If the test

14
expression becomes false then the control will exit from the loop. for
example, i<=9;
 Update Expression: After execution of the loop body loop variable
is updated by some value it could be incremented, decremented,
multiplied, or divided by any value.
for loop Equivalent Flow Diagram:

Example:

// C program to illustrate for loop

#include <stdio.h>

// Driver code

int main()

int i = 0;

for (i = 1; i <= 10; i++)

15
printf( "Hello World\n");

return 0;

Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
While Loop
While loop does not depend upon the number of iterations. In for loop
the number of iterations was previously known to us but in the While
loop, the execution is terminated on the basis of the test condition. If
the test condition will become false then it will break from the while loop
else body will be executed.
Syntax:
initialization_expression;

while (test_expression)
{
// body of the while loop

update_expression;
}
Flow Diagram for while loop:

16
EXAMPLE:

// C program to illustrate

// while loop

#include <stdio.h>

// Driver code

int main()

// Initialization expression

int i = 2;

// Test expression

while(i < 10)

// loop body

printf( "Hello World\n");

// update expression

i++;

17
}

return 0;

Output

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
do-while Loop
The do-while loop is similar to a while loop but the only difference lies in
the do-while loop test condition which is tested at the end of the body.
In the do-while loop, the loop body will execute at least
once irrespective of the test condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop

update_expression;

} while (test_expression);
Flow Diagram for Do while loop:

18
Output
Hello World
INFINITE LOOPS:

Infinite Loop
An infinite loop is executed when the test expression never becomes
false and the body of the loop is executed repeatedly. A program is
stuck in an Infinite loop when the condition is always true. Mostly this is
an error that can be resolved by using Loop Control statements.
Using for loop:
// C program to demonstrate infinite
// loops using for loop
#include <stdio.h>
// Driver code
int main ()
{
int i;
// This is an infinite for loop
// as the condition expression
// is blank
for ( ; ; )
{
printf("This loop will run forever.\n");
}
return 0;
}

Output
This loop will run forever.
This loop will run forever.

19
This loop will run forever.
...
Using While loop:

// C program to demonstrate

// infinite loop using while

// loop

#include <stdio.h>

// Driver code

int main()

while (1)

printf("This loop will run forever.\n");

return 0;

Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...

Using the do-while loop:


// C program to demonstrate
// infinite loop using do-while
// loop
#include <stdio.h>
// Driver code
int main()
{
do
{
printf("This loop will run forever.\n");
} while (1);

20
return 0;
}
Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...

NESTED LOOPS:

A nested loop means a loop statement inside another loop statement.


That is why nested loops are also called “loop inside loops“. We can
define any number of loops inside another loop.
1. Nested for Loop
Nested for loop refers to any type of loop that is defined inside a ‘for’
loop. Below is the equivalent flow diagram for nested ‘for’ loops:

Nested for loop in C

Syntax:
for ( initialization; condition; increment ) {

21
for ( initialization; condition; increment ) {

// statement of inside loop


}

// statement of outer loop


}
Example: Below program uses a nested for loop to print a 3D matrix of
2x3x2.
C

// C program to print elements of Three-Dimensional Array

// with the help of nested for loop

#include <stdio.h>

int main()

// initializing the 3-D array

int arr[2][3][2]

= { { { 0, 6 }, { 1, 7 }, { 2, 8 } },

{ { 3, 9 }, { 4, 10 }, { 5, 11 } } };

// Printing values of 3-D array

for (int i = 0; i < 2; ++i) {

22
for (int j = 0; j < 3; ++j) {

for (int k = 0; k < 2; ++k) {

printf("Element at arr[%i][%i][%i] = %d\n",

i, j, k, arr[i][j][k]);

return 0;

Output

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11

23
2. Nested while Loop
A nested while loop refers to any type of loop that is defined inside a
‘while’ loop. Below is the equivalent flow diagram for nested ‘while’
loops:

Nested while loop in C

Syntax:
while(condition) {

while(condition) {

// statement of inside loop


}

// statement of outer loop


}
24
Example: Print Pattern using nested while loops

// C program to print pattern using nested while loops

#include <stdio.h>

int main()

int end = 5;

printf("Pattern Printing using Nested While loop");

int i = 1;

while (i <= end) {

printf("\n");

int j = 1;

while (j <= i) {

printf("%d ", j);

j = j + 1;

25
i = i + 1;

return 0;

Output

Pattern Printing using Nested While loop


1
12
123
1234
12345
3. Nested do-while Loop
A nested do-while loop refers to any type of loop that is defined inside a
do-while loop. Below is the equivalent flow diagram for nested ‘do-
while’ loops:

26
do while loop in C

Syntax:
do{

do{

// statement of inside loop


}while(condition);

// statement of outer loop


}while(condition);
Note: There is no rule that a loop must be nested inside its own type. In
fact, there can be any type of loop nested inside any type and to any
level.

27
Syntax:
do{

while(condition) {

for ( initialization; condition; increment ) {

// statement of inside for loop


}

// statement of inside while loop


}

// statement of outer do-while loop


}while(condition);
Example: Below program uses a nested for loop to print all prime
factors of a number.

// C Program to print all prime factors

// of a number using nested loop

#include <math.h>

#include <stdio.h>

// A function to print all prime factors of a given number n

void primeFactors(int n)

28
// Print the number of 2s that divide n

while (n % 2 == 0) {

printf("%d ", 2);

n = n / 2;

// n must be odd at this point. So we can skip

// one element (Note i = i +2)

for (int i = 3; i <= sqrt(n); i = i + 2) {

// While i divides n, print i and divide n

while (n % i == 0) {

printf("%d ", i);

n = n / i;

// This condition is to handle the case when n

// is a prime number greater than 2

if (n > 2)

29
printf("%d ", n);

/* Driver program to test above function */

int main()

int n = 315;

primeFactors(n);

return 0;

Output:

3357
Break Inside Nested Loops
Whenever we use a break statement inside the nested loops it breaks
the innermost loop and program control is inside the outer loop.
Example:

// C program to show working of break statement

// inside nested for loops

#include <stdio.h>

int main()

30
{

int i = 0;

for (int i = 0; i < 5; i++) {

for (int j = 0; j < 3; j++) {

// This inner loop will break when i==3

if (i == 3) {

break;

printf("* ");

printf("\n");

return 0;

Output

***
***
***

31
***
In the above program, the first loop will iterate from 0 to 5 but here if i
will be equal to 3 it will break and will not print the * as shown in the
output.

Continue Inside Nested loops


Whenever we use a continue statement inside the nested loops it skips
the iteration of the innermost loop only. The outer loop remains
unaffected.
Example:

// C program to show working of continue statement

// inside nested for loops

#include <stdio.h>

int main()

int i = 0;

for (int i = 0; i < 5; i++) {

for (int j = 0; j < 3; j++) {

// This inner loop will skip when j==2

if (j==2) {

continue;

32
printf("%d ",j);

printf("\n"); }

return 0;

SWITCH STATEMENT IN C:
The switch case statement is an alternative to the if else if ladder that
can be used to execute the conditional code based on the value of the
variable specified in the switch statement. The switch block consists of
cases to be executed based on the value of the switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}

33
Example of switch Statement
// C Program to illustrate the use of switch statement

#include <stdio.h>

int main()

// variable to be used in switch statement

int var = 2;

// declaring switch cases

switch (var) {

case 1:

printf("Case 1 is executed");

break;
34
case 2:

printf("Case 2 is executed");

break;

default:

printf("Default Case is executed");

break;

return 0;

Output
Case 2 is executed

35

You might also like