PSS Unit 3
PSS Unit 3
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) {
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) {
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)
// Nested - if statement
// is true
if (i < 12)
5
printf("i is smaller than 12 too\n");
else
else {
if (i == 20) {
// Nested - if statement
// is true
if (i < 22)
else
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;
#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
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;
}
#include <stdio.h>
8
int main()
int var = 2;
switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
break;
return 0;
Output
Case 2 is executed
OPERATORS:
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:
#include <stdio.h>
// Driver code
int main()
int i = 0;
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
// loop body
// 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
// loop
#include <stdio.h>
// Driver code
int main()
while (1)
return 0;
Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...
20
return 0;
}
Output
This loop will run forever.
This loop will run forever.
This loop will run forever.
...
NESTED LOOPS:
Syntax:
for ( initialization; condition; increment ) {
21
for ( initialization; condition; increment ) {
#include <stdio.h>
int main()
int arr[2][3][2]
= { { { 0, 6 }, { 1, 7 }, { 2, 8 } },
{ { 3, 9 }, { 4, 10 }, { 5, 11 } } };
22
for (int j = 0; j < 3; ++j) {
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:
Syntax:
while(condition) {
while(condition) {
#include <stdio.h>
int main()
int end = 5;
int i = 1;
printf("\n");
int j = 1;
while (j <= i) {
j = j + 1;
25
i = i + 1;
return 0;
Output
26
do while loop in C
Syntax:
do{
do{
27
Syntax:
do{
while(condition) {
#include <math.h>
#include <stdio.h>
void primeFactors(int n)
28
// Print the number of 2s that divide n
while (n % 2 == 0) {
n = n / 2;
while (n % i == 0) {
n = n / i;
if (n > 2)
29
printf("%d ", n);
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:
#include <stdio.h>
int main()
30
{
int i = 0;
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.
#include <stdio.h>
int main()
int i = 0;
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()
int var = 2;
switch (var) {
case 1:
printf("Case 1 is executed");
break;
34
case 2:
printf("Case 2 is executed");
break;
default:
break;
return 0;
Output
Case 2 is executed
35