0% found this document useful (0 votes)
8 views36 pages

PPS Unit 3

The document provides an overview of conditional statements, loops, and functions in C programming. It explains the use of if, else, switch statements, and various loop structures such as for, while, and do-while, along with examples. Additionally, it covers user-defined functions, recursion, and their advantages and disadvantages.

Uploaded by

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

PPS Unit 3

The document provides an overview of conditional statements, loops, and functions in C programming. It explains the use of if, else, switch statements, and various loop structures such as for, while, and do-while, along with examples. Additionally, it covers user-defined functions, recursion, and their advantages and disadvantages.

Uploaded by

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

PPS Unit 3

BY APURVA JOSHI
C If ... Else
Conditions and If Statements
You have already learned that C supports the usual logical conditions from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
C has the following conditional
statements:
•Use if to specify a block of code to be executed, if a
specified condition is true
•Use else to specify a block of code to be executed, if the
same condition is false
•Use else if to specify a new condition to test, if the first
condition is false
•Use switch to specify many alternative blocks of code to
be executed
#include <stdio.h>

int main() {
if (20 > 18) {
printf("20 is greater than 18");
}
return 0;
}
#include <stdio.h>

int main() {

int time = 20;

if (time < 18) {

printf("Good day.");

} else {

printf("Good evening.");

return 0;

}
Else-If block
#include <stdio.h>

int main() {

int time = 22;

if (time < 10) {

printf("Good morning.");

} else if (time < 20) {

printf("Good day.");

} else {

printf("Good evening.");

return 0;

}
Number is positive or negative
#include <stdio.h>

int main() {

int myNum = 10;

if (myNum > 0) {

printf("The value is a positive number.");

} else if (myNum < 0) {

printf("The value is a negative number.");

} else {

printf("The value is 0.");

return 0;

}
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.
Basically, 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.
In C, the switch case statement is used for executing one condition from multiple
conditions. It is similar to an if-else-if ladder.
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;
}
For Loop
// Print numbers from 1 to 10
#include <stdio.h>

int main() {
int i;

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


{
printf("%d ", i);
}
return 0;
}
While Loop
While loop
// Print numbers from 1 to 5

#include <stdio.h>

int main() {

int i = 1;

while (i <= 5) {

printf("%d\n", i);

++i;

return 0;

}
Do… While
// Program to add numbers until the user
enters zero using Do… while loop
#include <stdio.h>

int main() {

double number, sum = 0;

// the body of the loop is executed at least once

do {

printf("Enter a number: ");

scanf("%lf", &number);

sum += number;

while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;

}
C break and continue
// Program to calculate the sum of numbers (10 numbers max)
// If the user enters a negative number, the loop terminates

// if the user enters a negative number, break


the loop
#include <stdio.h>
if (number < 0.0) {
int main() {
break;
int i;
}
double number, sum = 0.0;
sum += number; // sum = sum + number;
for (i = 1; i <= 10; ++i) {
}
printf("Enter n%d: ", i);
printf("Sum = %.2lf", sum);
scanf("%lf", &number);
return 0;
}
// Program to calculate the sum of numbers (10 numbers max)
// If the user enters a negative number, it's not added to the result

sum += number; // sum = sum + number;


#include <stdio.h> }
int main() { printf("Sum = %.2lf", sum);
int i; return 0;
double number, sum = 0.0; }
for (i = 1; i <= 10; ++i) {
printf("Enter a n%d: ", i);
scanf("%lf", &number);
if (number < 0.0) {
continue;
}
The Goto Statement
Transfers control to a labeled statement Can make code less readable;
use with caution.
Example:

#include <stdio.h>

void main() {

Label1: printf("Executing Statement 1\n"); // Label1:


Statement 1

if (1 < 2) {

goto Label3;

}
Label2: printf("Executing Statement 2\n"); // Label2:
Statement 2

Label3: printf("Executing Statement 3\n"); // Label 3:


C Functions
A function is a block of code that performs a specific task.
Suppose, you need to create a program to create a circle and color it. You can create
two functions to solve this problem:
•create a circle function
•create a color function
Dividing a complex problem into smaller chunks makes our program easy to understand
and reuse.
Types of function
There are two types of function in C programming:
•Standard library functions
•User-defined functions
Advantages of user-defined function

1.The program will be easier to understand, maintain and debug.


2.Reusable codes that can be used in other programs
3.A large program can be divided into smaller modules. Hence, a large project can be
divided among many programmers.
Recursion Functions
Recursion in programming occurs when a function calls itself directly or indirectly. Recursive
functions are used to solve problems that can be broken down into simpler, repetitive tasks.
Here's a deeper look into recursion:
Key Concepts of Recursion:
1.Base Case:
1. A base case is the condition under which the recursive function stops calling itself.
2. Without a base case, the function would call itself indefinitely, leading to a stack overflow.

2.Recursive Case:
1. The part of the function that calls itself with a modified argument, gradually moving towards the
base case.
Recursion
How Recursion Works:
Each recursive call creates a new instance of the function on the call stack, which means each
instance has its own scope and variables. The function continues to call itself until it hits the
base case, then the call stack unwinds, returning the results back up the chain.
Advantages of Recursion:
•Simplicity: Recursive solutions are often more elegant and easier to understand.
•Solving Complex Problems: Suitable for problems like tree traversal, factorial calculation, and
the Fibonacci sequence, where the problem can be divided into similar sub-problems.
Disadvantages of Recursion:
•Performance: Recursive calls take up more memory and processing time due to the overhead of
maintaining the call stack.
•Risk of Stack Overflow: If the base case is not reached or the problem size is too large, it can
lead to a stack overflow.
#include <stdio.h>

// Recursive function to calculate factorial

int factorial(int n) {

if (n == 0) { // Base case

return 1;

} else {

return n * factorial(n - 1); // Recursive case

int main() {

int number;

printf("Enter a number to find its factorial: ");

scanf("%d", &number);

if (number < 0) {

printf("Factorial of a negative number doesn't exist.\n");

} else {

printf("Factorial of %d is %d\n", number, factorial(number));

return 0;

}
Ref: Types of User-defined Functions in C Programming
Thank you

You might also like