Control Flow
Conditional Statements
Conditional statements are used to execute different blocks of code based on certain
conditions.
If Statement
If Statement: A statement that executes a block of code if a certain
condition is true.
Syntax:if (expression) statement; }
Example:
#include <stdio. h>
int main() {
int age;
printf("Enter your age: ");
Scanf("%d", bage);
if (age >= 18) {
printf ("Valid for driving license\n");
}
return 0;
}
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.
Syntax: if (expression) { statement; } else { statement ; }
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
If-Else If Statement: A statement that executes a block of code if a certain
condition is true, and another block of code if the condition is false, and
another block of code if another condition is true.
Syntax: if (expressionl) { statement; ) else if (expression2){ statement; )
else { 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.
Syntax: switch (variable ) {case expressionl: statement ; break; case
expression2 : statement; break; ... default: statement; }
Example:
#include <stdio .h>
int main() {
char alphabet ;
printf ("Enter an alphabet : ");
Scanf("%c", &al phabet);
Switch (alphabet ) {
case a':
Case 'e':
Case i':
Case
case u':
printf("Vowel\n");
break;
default:
printf("Not a vowel\n") ;
}
return 0;
)
Iteration Statements
While Loop
While Loop: A loop that executes a block of code as long as a certain
condition is true.
Syntax: while (condition) { statement; )
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.
Syntax: do { statement; } while (condition) ;
Example:
#include <stdio . h>
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;
printf("Sum of l+2+3+4+5 is: %d\n", sum) ;
return 0;
Jump Statements
Break Statement
Break Statement: A statement that terminates the execution of a loop or
switch 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 & Data Types
User Defined Functions
User Defined Functions: Functions that are created by the user to perform
a specific task.
Example:
#include <stdio. h>
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>
void show (int age, int rollno) {
printf("Your age is %d\n", age);
printf("Your roll number is %d\n", rollno) ;
int main ( ) {
show(18, 66);
return 0;
Recursion
Recursion: A function that calls itself to solve a problem.
Example:
#include <stdio.h>
void myfunction () {
prir
rintf ("Hello everyone\n");
myfunction () ;
int main ( ) {
myfunction ();
return 0;
}
Data Structures UserDefined DataTypes
Data Structures: A collection of data that can be used to store and
manipulate data.
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;