PPS Unit 3
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() {
printf("Good day.");
} else {
printf("Good evening.");
return 0;
}
Else-If block
#include <stdio.h>
int main() {
printf("Good morning.");
printf("Good day.");
} else {
printf("Good evening.");
return 0;
}
Number is positive or negative
#include <stdio.h>
int main() {
if (myNum > 0) {
} else {
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;
#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() {
do {
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
#include <stdio.h>
void main() {
if (1 < 2) {
goto Label3;
}
Label2: printf("Executing Statement 2\n"); // Label2:
Statement 2
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>
int factorial(int n) {
if (n == 0) { // Base case
return 1;
} else {
int main() {
int number;
scanf("%d", &number);
if (number < 0) {
} else {
return 0;
}
Ref: Types of User-defined Functions in C Programming
Thank you