Introduction of decision making statements:
Conditional statements, also called decision control structures, such as if, if-else, and switch, are used for making decisions in C programs.
These statements, sometimes referred to as Decision-Making Statements, evaluate one or more conditions to determine whether a specific block of code should be executed. They guide the flow of program execution based on the conditions evaluated.
Need for Conditional Statements
In real life, we often face situations that require making decisions about what to do next. Similarly, in programming, conditional statements help us decide which block of code to execute based on certain conditions. For instance, in C, you might use a statement like if x occurs, then execute y; otherwise, execute z. There are also ways to handle multiple conditions, such as using else-if statements: if x occurs, then execute p; else if y occurs, execute q; otherwise, execute r. This approach allows for more complex decision-making scenarios.
Types of Conditional statements in c:
conditional statements in c:
1.if 2.if-else
3.if-else-if 3.Nested-if
4.Switch
1.if:
The if statement is the simplest form of decision-making in C. It determines whether a specific statement or block of statements should be executed based on a condition. If the condition evaluates to true, the block of statements is executed; otherwise, it is skipped

Syntax of if Statement:
if (condition)
{
// Statements to execute if
// condition is true
}
In this syntax, condition is evaluated as either true or false. If the condition is true, the statements inside the curly braces {} are executed. If the condition is false, the statements are not executed. If you omit the curly braces, only the first statement following the if condition is considered part of the if block.
example
#include<stdio.h>
int main()
{
int i = 50;
if (i > 15) {
printf("50 is greater than 15");
}
2.if-else:
The if statement alone executes a block of code only if a condition is true. However, if you want to execute a different block of code when the condition is false, you use the else statement. The if-else construct allows you to specify two separate blocks of code: one to execute if the condition is true and another to execute if the condition is false.
Syntax of if-else Statement:
if (condition)
{
// Statements to execute if condition is true
}
else
{
// Statements to execute if condition is false
}In this syntax:
The block of code inside the if block is executed if the condition evaluates to true.The block of code inside the else block is executed if the condition evaluates to false.This structure ensures that one of the two blocks of code is always executed, depending on the condition

example for if-else:
#include<stdio.h>
int main() {
int number = 10;
if (number > 5) {
printf("The number is greater than 5.\n");
} else {
printf("The number is not greater than 5.\n");
}
return 0;
}
Explanation:
The if statement checks if number is greater than 5.If number is indeed greater than 5, the program prints "The number is greater than 5."If number is not greater than 5, the else block executes and prints "The number is not greater than 5."
3.if-else-if ladder
The if-else-if ladder in C is used to handle multiple conditions when you need to choose among several options. The ladder consists of a series of if, else if, and else statements. The conditions are evaluated in order from top to bottom. As soon as a true condition is found, its associated block of code is executed, and the rest of the ladder is skipped. If none of the conditions is true, the final else block (if provided) is executed

if (condition1)
{
// Code to execute if condition1 is true
}
else if (condition2)
{
// Code to execute if condition2 is true
}
else if (condition3)
{
// Code to execute if condition3 is true
}
else
{
// Code to execute if none of the above conditions are true
}
example code for if-else-if ladder
#include <stdio.h>
int main() {
int number = 15;
if (number > 20) {
printf("The number is greater than 20.\n");
}
else if (number > 10) {
printf("The number is greater than 10 but not greater than 20.\n");
}
else if (number > 0) {
printf("The number is greater than 0 but not greater than 10.\n");
}
else {
printf("The number is 0 or less.\n");
}
return 0;
}
Explanation:
The first if checks if number is greater than 20. If true, it prints the corresponding message and skips the remaining conditions.
If the first condition is false, the else if checks if number is greater than 10. If true, it prints the corresponding message.
If neither of the previous conditions is true, the next else if checks if number is greater than 0. If true, it prints the corresponding message.
If none of the above conditions are true, the else block is executed, printing that the number is 0 or less.
4.Switch statement:
The switch statement in C provides a way to select among multiple possible execution paths based on the value of a variable. It can be a cleaner and more readable alternative to using a long if-else-if ladder, especially when dealing with many discrete values.
Syntax of switch Statement:
switch (variable)
{
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
// Additional cases…
default:
// Code to execute if no cases match
}

click here to get example with real life scenario
Scenario: Choosing a Beverage
Imagine you are at a café and you have a menu with different beverages. Depending on the choice you make, you will get a different beverage. You can use a switch statement to handle this decision based on the beverage code.
Beverage Codes:
1: Coffee
2: Tea
3: Juice
4: Water
Here's how you might use a switch statement to handle this choice:
#include <stdio.h>
int main()
{
int beverageCode = 2;
switch (beverageCode) {
case 1:
printf("You chose Coffee.\n");
break;
case 2:
printf("You chose Tea.\n");
break;
case 3:
printf("You chose Juice.\n");
break;
case 4:
printf("You chose Water.\n");
break;
default:
printf("Invalid choice. Please select a valid beverage.\n");
}
return 0;
}
Explanation:
beverageCode: Represents the user's choice. In this example, beverageCode is set to 2.
switch (beverageCode): Compares the value of beverageCode with each case.
case 1: If beverageCode is 1, it prints "You chose Coffee."
case 2: If beverageCode is 2, it prints "You chose Tea." This is the matching case in this example.
case 3 and case 4*: Handle other beverage choices.
default: Catches any invalid codes that don't match the defined cases.
In this case, the output will be:
output:
You chose Tea.
This example demonstrates how the switch statement can simplify the process of handling multiple options based on a single value, making the code more readable and easier to manage compared to multiple if-else statements.
Define Looping or Iteration in c
looping or iteration refers to the process of executing a block of code repeatedly based on a condition or a set of conditions. It allows a program to perform repetitive tasks efficiently without writing the same code multiple times.
click here to see Example using a real-life scenario to illustrate loops.
Scenario: New Year’s Eve Countdown
Imagine you are creating a countdown timer that counts down from 10 seconds to 1 second before the New Year. You want to print each second remaining, and then print a message when the countdown is complete.
list of Loop Statements in C
1.The for statements
2.The while statements
3.The do-while statement
1.for Loop in c?
A for loop in C programming is used to repeat a block of code a specific number of times. It’s like setting up a machine to perform a task repeatedly without having to manually write the same code over and over.
syntax:
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
click here to see Example using a real-life scenario to illustrate loops.
Imagine you have a task where you need to print out a “Hello, World!” message 5 times. Instead of writing the print statement 5 times, you can use a for loop to do it automatically.
Example program for For loop:
Here’s a simple C program using a for loop to print “Hello, bhanu” 5 times
#include <stdio.h>
int main() {
// Loop will execute 5 times
for (int i = 0; i < 5; i++) {
printf("Hello, bhanu!\n");
}
return 0;
}
Explanation :
for (int i = 0; i < 5; i++);
This line sets up the for loop.
int i = 0 initializes a counter variable i to 0.
i < 5 is the condition that keeps the loop running as long as i is less than 5.
i++ increases the counter i by 1 each time the loop runs.
printf("Hello, bhanu!\n");: This line prints the message each time the loop runs
So, the for loop will run 5 times, printing "Hello, bhanu!" each time it iterates.
Example programs:
C program to print factorial of N:
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
if (n < 0) {
printf(“Factorial is not defined for negative numbers.\n”);
} else {
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf(“Factorial of %d = %llu\n”, n, factorial);
}
return 0;
}
Explanation:
Input:
printf(“Enter a positive integer: “); prompts the user to enter a number.
scanf(“%d”, &n); reads the number entered by the user and stores it in the variable n.
Factorial Calculation:
if (n < 0) checks if the number is negative, as factorials are not defined for negative integers.
for (i = 1; i <= n; i++) initializes a loop from 1 to n.
factorial *= i; multiplies the current value of factorial by i in each iteration, effectively calculating the factorial of n.
printf(“Factorial of %d = %llu\n”, n, factorial); prints the computed factorial. unsigned long long is used to handle large results.
End:
return 0;
returns 0 to indicate that the program executed successfully.
C program to print given multiplication table:
#include <stdio.h>
int main() {
int n, i;
printf("Enter a number to print its multiplication table: ");
scanf("%d", &n);
printf("Multiplication table for %d:\n", n);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Explanation:
Input:
printf("Enter a number to print its multiplication table: "); prompts the user to enter a number.
scanf("%d", &n); reads the user input and stores it in the variable n.
Printing the Multiplication Table:
printf("Multiplication table for %d:\n", n);
prints the header for the table.
for (i = 1; i <= 10; i++)
starts a loop from 1 to 10.
printf("%d x %d = %d\n", n, i, n * i);
prints the multiplication result for each value of i.
End:
return 0;
ends the main function and returns 0, indicating successful execution.
C program to print number from 1 to 100 with divisible by 9:
#include <stdio.h>
int main() {
int i;
printf("Numbers from 1 to 100 divisible by 9 are:\n");
for (i = 1; i <= 100; i++) {
if (i % 9 == 0) {
printf("%d\n", i);
}
}
return 0;
}
Explanation:
Looping Through Numbers:for (i = 1; i <= 100; i++) iterates through numbers from 1 to 100.
Check Divisibility:
if (i % 9 == 0) checks if the current number i is divisible by 9.
Print Numbers:printf("%d\n", i); prints the number if it is divisible by 9.
End:
return 0;
ends the main function and returns 0, indicating successful execution.
while Loop in C
A while loop in C programming is used to repeatedly execute a block of code as long as a specified condition remains true. Unlike a for loop, you don’t specify the number of iterations in advance; instead, you control the loop by a condition that determines when to stop.
syntax:
while(test condition)
{
body of loop
}
click here for example of real-life scenario
Imagine you are filling a cup with water. You keep adding water until the cup is full. The number of times you need to add water isn’t fixed—it’s based on how full the cup gets. This is similar to a while loop.
Example program for while loop:
Here’s a simple C program using a while loop to print “Hello, bhanu” 5 times:
#include <stdio.h>
int main() {
int count = 0; // Initialize a counter variable
// Loop will execute as long as count is less than 5
while (count < 5) {
printf("Hello,bhanu\n");
count++; // Increase the counter by 1 each time
}
return 0;
}
Explanation
int count = 0;: This initializes a counter variable count to 0.
while (count < 5): This is the test condition for the while loop.
The loop continues to execute as long as count is less than 5.
printf("Hello, bhanu\n");: This line prints the message each time the loop runs.
count++;: This increases the value of count by 1 after each iteration of the loop.
The while loop will keep running and printing "Hello, bhanu!" until the condition count < 5 becomes false (i.e., when count reaches 5).
C program to print sum of n natural numbers using while loop:
#include<stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
i++;
}
printf("Sum of the first %d natural numbers is %d\n", n, sum);
return 0;
}
Explanation:
Include the Standard Input Output Library:
include This library is needed for input and output functions like printf and scanf.
Main Function:
int main() {
This is the entry point of the C program.
Variable Declarations:
int n, sum = 0, i = 1;
n will store the user input for the number of natural numbers.
sum will accumulate the total sum of the natural numbers.
i is the loop counter, starting from 1 (the first natural number).
User Input:printf("Enter a positive integer: ");
scanf("%d", &n);printf displays a message asking the user to enter a positive integer.scanf reads the integer input from the user and stores it in n.
Input Validation:
program with a noWhile Loop to Calculate Sum:while (i <= n) {
sum += i;
i++;
}
The while loop continues as long as i is less than or equal to n.
Inside the loop, sum += i adds the current value of i to sum.
i++ increments the value of i by 1 to move to the next natural number.
Output the Result:
printf("Sum of the first %d natural numbers is %d\n", n, sum);
Prints the calculated sum in a formatted message.
Exit the Program:
return 0;
Returns 0 to indicate successful completion of the program.
This program effectively uses a while loop to sum the natural numbers from 1 up to n and then prints the result.
C program to print the sum of digits of given number:
#include<stdio.h>int main() {
int num, sum = 0;
printf("Enter an integer: ");
scanf("%d", &num);
num = (num < 0) ? -num : num;
while (num > 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of the digits is %d\n", sum);
return 0;
}
C program for to check the given number is a palindrome or not:
#include <stdio.h>
int main() {
int num, originalNum, reversedNum = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = nuym;
// Reverse the number
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
// Check if the original number and reversed number are the same
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0;
}
Explanation:
1. Libraries:
- #include <stdio.h>: Includes the Standard Input Output library for printf and scanf.
2. Variable Initialization:
- int num, originalNum, reversedNum = 0, remainder;:
- num will store the user input.
- originalNum keeps the original value of num.
- reversedNum will store the reversed number.
- remainder helps in extracting digits.
3. User Input:
- printf("Enter an integer: "); prompts the user.
- scanf("%d", &num); reads the integer input.
4. Handle Negative Numbers:
- If num is negative, it prints a message and exits the program as negative numbers cannot be palindromes.
5. Reverse the Number:
- while (num != 0): Continues until num becomes 0.
- remainder = num % 10;: Extracts the last digit of num.
- reversedNum = reversedNum * 10 + remainder;: Constructs the reversed number.
- num /= 10;: Removes the last digit from num.
6. Palindrome Check:
- Compares originalNum and reversedNum. If they are equal, the number is a palindrome; otherwise, it is not.
7. Output:
- printf("%d is a palindrome.\n", originalNum); or printf("%d is not a palindrome.\n", originalNum); displays the result.
8. Program Exit:
- return 0;: Indicates successful completion of the program.
Do-while Loop in C
A do-while loop in C programming is similar to a while loop, but with one key difference: the condition is tested after the loop’s body has executed. This means that the loop’s body will always run at least once, even if the condition is false initial
Key Points
Post-Condition Testing:
The condition is evaluated after the loop’s body runs.
Guaranteed Execution:
The loop body executes at least once, regardless of the condition.
syntax
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
click here for example of real-life scenario
Imagine you are filling out a form on a website that asks if you want to receive promotional emails. Even if you initially say “No,” the form will display the options to you before asking you to confirm. This is like a do-while loop: the options are shown at least once before checking your response.
Example program:
Here’s a simple C program using a do-while loop to print “Hello, World!” 3 times:
#include<stdio.h>
int main() {
int count = 0; // Initialize the counter variable
// Loop will execute at least once and continue while count is less than 3
do {
printf("Hello, World!\n");
count++; // Increase the counter by 1 each time
} while (count < 3); // Condition is checked after the body executes
return 0;
Explanation:
Initialization:
int count = 0;
starts the counter at 0.
Body:
printf("Hello, World!\n");
prints the message.
Update:
count++;
increments the counter by 1.
Condition: while (count < 3);
checks if the counter is less than 3 after executing the body.
C program to check the given number is Armstrong or not:
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, digits = 0;
int tempNum;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
tempNum = num;
while (tempNum != 0) {
tempNum /= 10;
digits++;
}
num = originalNum;
do {
remainder = num % 10;
result += pow(remainder, digits);
num /= 10;
} while (num != 0);
if (result == originalNum) {
printf("%d is an Armstrong number.\n", originalNum);
} else {
printf("%d is not an Armstrong number.\n", originalNum);
}
return 0;
}
Explanation:
countDigits(int num) Function: This function calculates the number of digits in the number.
Main Program:Input: Reads an integer from the user.
Initialization: Stores the original number and calculates the number of digits.
Armstrong Calculation:Uses a do-while loop to process each digit of the number:Extracts the last digit with num % 10.
Adds the digit raised to the power of the number of digits to result.
Removes the last digit with num /= 10.
Comparison: Checks if the result equals the originalNum and prints the result.
This program will accurately determine if the entered number is an Armstrong number.
C program for to print even and odd number do while:
#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
printf("Even numbers from 1 to %d:\n", num);
i = 1;
do {
if (i % 2 == 0) {
printf("%d ", i);
}
i++;
} while (i <= num);
printf("\nOdd numbers from 1 to %d:\n", num);
i = 1;
do {
if (i % 2 != 0) {
printf("%d ", i);
}
i++;
} while (i <= num);
printf("\n");
return 0;
}
difference between while loop and do-while loop

Break and Continue statements:
1.goto statement
2.Break statement
3.continue statement
1.goto statement:
The goto statement in C is used to perform an unconditional jump to a labeled statement within the same function. This can be useful in certain scenarios, but it is generally advised to use it sparingly because it can make code harder to understand and maintain.
Syntax of goto Statement:
goto label;
…
label:
// Code to execute when the goto statement jumps to this label
Key Points:
Label:
A label is an identifier followed by a colon (:). It marks a location in the code where execution can jump to.goto label;: The goto statement jumps to the labeled statement identified by label.
#include <stdio.h>
int main() {
int i = 0;
// Using goto to jump to the label 'skip'
if (i == 0) {
goto skip; // Jump to the label 'skip'
}
// This code will be skipped
printf("This will be skipped.\n");
skip:
// Code execution resumes here
printf("This code is executed.\n");
return 0;
}
Explanation:
The goto skip; statement causes the program to jump to the skip: label.The line printf("This will be skipped.\n"); is not executed because of the goto statement.The program resumes execution at the skip: label, printing "This code is executed."
2.Break statements:
The break statement in C is used to exit a loop prematurely, regardless of the loop’s condition. When break is encountered within a loop, it immediately terminates the loop and transfers control to the statement following the loop.
Here’s a summary of how break works:
In Loops: When a break statement is executed inside a for, while, or do-while loop, it stops the loop and jumps to the code immediately after the loop block.In switch Statements: The break statement also exits from a switch case, preventing fall-through to subsequent cases.

Example :
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
printf("%d\n", i);
}
printf("Loop exited. i = %d\n", i);
return 0;
}
Explanation:
The for loop is set to iterate from 1 to 10.When i equals 5, the break statement is executed.This break statement stops the loop immediately.After exiting the loop, the program prints "Loop exited. i = 5"
3.continue statement:
The continue statement in C is used to skip the remaining code inside a loop for the current iteration and proceed directly to the next iteration of the loop. Unlike the break statement, which exits the loop entirely, continue only affects the current iteration by bypassing the rest of the loop’s body and moving to the next iteration.
Usage in Loops:
In for Loops:
Skips the remaining statements in the current iteration and goes to the next iteration, where the loop condition is re-evaluated. In while and do-while Loops: Skips to the condition check and continues with the next iteration.
Example:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of the loop body if i is even
}
printf("%d\n", i); // This line is skipped if i is even
}
return 0;
}
Explanation:
The for loop iterates from 1 to 10.The if statement checks if i is even.If i is even, the continue statement is executed, which skips the printf statement for that iteration and proceeds to the next iteration of the loop.If i is odd, the printf statement is executed, printing the value of i.
Difference Between break and continue statements:
