PPC Unit 2 New
PPC Unit 2 New
Conditional Statements
Conditional statements are used to execute different blocks of code based on certain
conditions.
If Statement
If-Else Statement
If-Else Statement: A statement that executes a block of code ifa certain
condition is true, and another block of code if the condition is false.
Example:
Page 1
#include <stdio.h>
int main () {
int num;
printf ("Enter a number: ");
Scanf("%d", &num);
if (num % 2 == 0) {
printf("Even number\n") ;
) else {
printf("Odd number\n") ;
)
return 0;
If-Else If Statement
Example:
#include <stdio.h>
int main ( ) {
int num;
printf("Enter a number: ") ;
Scanf ("%d", &num) ;
if (num > 0) {
printf("Positive number\n") ;
) else if (num < 0) {
printf("Negative number\n");
} else {
printf("Number is zero\n");
return 0;
Switch Statement
Page 2
Switch Statement: A statement that executes a block of code based on
the value of a variable.
Example:
Iteration Statements
While Loop
While Loop: A loop that executes a block of code as long as a certain
condition is true.
Example:
Page 3
#include <stdio. h>
int main () {
int num = l;
while (num 3){
printf(tHNUM VALUE %d\n", num) ;
num++;
}
return 0;
Do-While Loop
Do-While Loop: A loop that executes a block of code once, and then
repeats the loop as long as a certain condition is true.
int main() {
int i= 1;
do {
printf("Enter your roll number:"):
scanf("%f", &rollno) ;
i++;
} while (i < 4);
return 0;
For Loop
For Loop: A loop that executes a block of code for a specified number of
iterations.
Syntax: for (initialization; expression; increment/dec rement) { statement; }
Example:
Page 4
#include <stdio. h>
int main ( ) {
int sum = 0:
for (int i = l; i <= 5; itt) {
Sum = Sum +i;
Jump Statements
Break Statement
Example:
#include <stdio.h>
int main () {
int i;
for (i = 0; i < 10; i++) {
if (i * 4) (
break;
printf("%d\n", i) ;
}
return 0;
}
Continue Statement
Continue Statement: A statement that skips the rest of the code in a loop
and continues with the next iteration.
Example:
Page 5
#include <stdio . h>
int main () {
int i;
for (i = it+) {
if (i =
continue;
printf("%d\n", i) ;
return 0;
)
User Defined Functions: Functions that are created by the user to perform
a specific task.
Example:
void display () {
printf(" Good morning! !\n");
}
int main ( ) {
display ();
return 0;
Function Parameters
Function Parameters: Variables that are defined within a function to
receive values from the calling function.
Example:
Page 6
#include <stdio . h>
int main ( ) {
show(18, 66);
return 0;
Recursion
Example:
#include <stdio.h>
void myfunction () {
prir
rintf ("Hello everyone\n");
myfunction () ;
int main ( ) {
myfunction ();
return 0;
}
Example:
Page 7
#include <stdio. h>
struct student {
int id;
int age;
float average;
int main() {
struct student obj;
obj.id = 005;
obj.age = 20;
obj.average = 73.3;
printf("ID is %d\n", obj.id) ;
printf("Age is %d\n", obj.age);|
printf("Average is %f\n", obj .average);
return 0;