Q.1 Explain Simple if Statement With Syntax. Draw the Flowchart and…
Q.1 Explain Simple if Statement With Syntax. Draw the Flowchart and…
Simple if Statement in C
The if statement in C is used to execute a block of code only if a specified condition is true. If
the condition is false, the code inside the if block is skipped, and the program continues with the
rest of the code.
Syntax of if Statement:
if (condition) {
// block of code to be executed if the condition is true
}
#include <stdio.h>
int main() {
int number = 10;
return 0;
}
#include <stdio.h>
int main() {
int number = 10;
#include <stdio.h>
int main() {
int number = -5;
return 0;
}
Q.2 Explain If-Else statement with syntax. Draw the flowchart and illustrate it with the help of
suitable example
If-Else Statement in C
This is useful when you need to perform one operation when a condition is met,
and a different operation when it is not.
if (condition) {
// Block of code to be executed if the condition is true
} else {
// Block of code to be executed if the condition is false
Example of If-Else Statement:
#include <stdio.h>
int main() {
int number = -5;
return 0;
}
Explanation:
+----------------------------+
| Start |
+----------------------------+
|
v
+----------------------------+
| Evaluate condition (e.g., |
| number > 0) |
+----------------------------+
|
+------------+------------+
| |
Yes No
| |
+-------------+ +-------------+
| Execute | | Execute |
| if block | | else block |
+-------------+ +-------------+
| |
v v
+-----------------+ +-----------------+
| End | | End |
+-----------------+ +-----------------+
Example of If-Else Statement
#include <stdio.h>
int main() {
int age = 18;
return 0;
}
Q.3 Explain Else-If Ladder with syntax. Draw the flowchart and illustrate it with the help of
suitable example.
Else-If Ladder in C
if (condition1) {
// Block of code to be executed if condition1 is true
} else if (condition2) {
// Block of code to be executed if condition2 is true
} else if (condition3) {
// Block of code to be executed if condition3 is true
} else {
// Block of code to be executed if none of the above conditions are true
}
#include <stdio.h>
int main() {
int marks = 75;
return 0;
The flowchart illustrates how conditions are checked sequentially in an else-if ladder.
+-----------------------------+
| Start |
+-----------------------------+
|
v
+-----------------------------+
| Evaluate condition1 (e.g., |
| marks >= 90) |
+-----------------------------+
|
+----------+----------+
| |
Yes No
| |
+-----------------+ +-----------------+
| Execute block 1 | | Evaluate condition2 |
+-----------------+ +-----------------+
|
+----------+----------+
| |
Yes No
| |
+-----------------+ +-----------------+
| Execute block 2 | | Evaluate condition3 |
+-----------------+ +-----------------+
|
+----------+----------+
| |
Yes No
| |
+-----------------+ +-----------------+
| Execute block 3 | | Evaluate condition
Q.4 Explain Nested If-Else with syntax. Draw the flowchart and illustrate it with the help of
suitable example.
if (condition1) {
// Code block executed if condition1 is true
if (condition2) {
// Code block executed if condition2 is true
} else {
// Code block executed if condition2 is false
}
} else {
// Code block executed if condition1 is false
}
#include <stdio.h>
int main() {
int age = 25;
int salary = 35000;
return 0;
}
Flowchart for Nested If-Else Statement
The flowchart illustrates how nested conditions are evaluated in the nested if-else structure:
+-------------------------------+
| Start |
+-------------------------------+
|
v
+-------------------------------+
| Is age >= 18? |
+-------------------------------+
|
+----------+----------+
| |
Yes No
| |
+----------------+ +----------------------+
| Is salary >= 30k? | | Print: "Not eligible |
+----------------+ | for loan due to age" |
| +----------------------+
+------------+----------+
| |
Yes No
| |
+-------------------+ +-------------------------------+
| Print: "Eligible | | Print: "Salary insufficient |
| for loan" | | for loan" |
+-------------------+ +-------------------------------+
|
v
+----------------+
| End |
+----------------+
#include <stdio.h>
int main() {
int number = 15;
return 0;
}
Q.5 Explain Switch statement with syntax. Draw the flowchart and illustrate it with the help of
suitable example
Switch Statement in C
The switch statement in C is a control flow structure that allows you to execute
one out of multiple possible blocks of code based on the value of a variable. It is
an alternative to using many if-else-if statements, especially when there are
many conditions to check based on a single variable.
switch (expression) {
case constant1:
// Block of code executed if expression == constant1
break;
case constant2:
// Block of code executed if expression == constant2
break;
case constant3:
// Block of code executed if expression == constant3
break;
default:
// Block of code executed if no case matches
#include <stdio.h>
int main() {
int day = 3;
// Switch statement to print the name of the day based on its number
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0; }
Flowchart for Switch Statement
+-----------------------------+
| Start |
+-----------------------------+
|
v
+-----------------------------+
| Evaluate expression |
+-----------------------------+
|
v
+-----------------------------+
| Is expression == case 1? |
+-----------------------------+
|
+--------+--------+
| |
Yes No
| |
+----------------+ +----------------+
| Execute case 1 | | Is expression |
| block | | == case 2? |
+----------------+ +----------------+
| |
v v
+----------------+ +----------------+
| Break | | Execute case 2 |
+----------------+ | block |
| +----------------+
v |
+-----------------------------+
| End |
+-----------------------------+
#include <stdio.h>
int main() {
int num = 2;
Q.8 Explain while loop with syntax and illustrate it using a suitable example.
While Loop in C
A while loop in C is a control flow statement that repeatedly executes a block of code as long as
a given condition evaluates to true. The condition is checked before the execution of the loop
body, making it a pre-test loop.
while (condition) {
// Code to be executed as long as the condition is true
}
#include <stdio.h>
int main() {
int count = 1;
return 0;
}
Q.9 Explain do-while with syntax and illustrate it using a suitable example.
do-while Loop in C
The do-while loop in C is similar to the while loop but with one key difference: in a do-while loop,
the condition is checked after the loop body is executed. This guarantees that the loop body
will be executed at least once, even if the condition is false initially.
do {
// Code to be executed
} while (condition);
• Code Block: The block of code inside the do section will execute first,
regardless of the condition.
• condition: The condition is evaluated after the code block has been
executed. If the condition is true, the loop will repeat. If the condition is false, the
loop will terminate.
• Semicolon: The do-while loop ends with a semicolon (;) after the while
condition.
#include <stdio.h>
int main() {
int count = 1;
return 0;
}
Q.11 Explain syntax of for loop and illustrate it using suitable example.
for Loop in C
The for loop in C is a control flow statement that allows code to be repeatedly executed based
on a given condition. It is often used when the number of iterations is known in advance or when
you need to iterate over a range of values.
• Initialization: This step is executed only once, at the beginning of the loop. It
is typically used to initialize the loop control variable.
• Condition: The condition is evaluated before every iteration. If the condition
is true, the loop body is executed. If it is false, the loop terminates.
• Increment/Decrement: This step is executed after each iteration of the loop.
It is used to update the loop control variable, usually incrementing or decrementing
it.
#include <stdio.h>
int main() {
// Using a for loop to print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Q.14 Compare the break and continue statements, providing examples to illustrate their
differences.
in C
Both break and continue are control flow statements used in loops and switch statements, but
they have distinct purposes and behaviors.
break Statement:
• The break statement is used to exit the loop (or switch statement) entirely,
regardless of the loop condition.
• Once the break is encountered, the control is transferred to the first
statement after the loop or switch.
continue Statement:
• The continue statement is used to skip the remaining part of the current
iteration in a loop and proceed to the next iteration.
• When continue is encountered, the loop condition is checked again, and if it
is true, the next iteration of the loop starts.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("%d\n", i); // This will print 1 and 2
}
printf("Loop terminated.\n");
return 0;
}
Output:
1
2
Loop terminated.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the rest of the loop when i is 3
}
printf("%d\n", i); // This will print all numbers except 3
}
Return 0;
}
Output:
1
2
4
5
#include <stdio.h>
int main() {
int num, factorial = 1;
// Check if the number is negative, as factorial is not defined for negative numbers
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate factorial using for loop
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply factorial by current number
}
// Output the result
printf("Factorial of %d is %d\n", num, factorial);
}
return 0;
}
#include <stdio.h>
int main() {
int i, j;
return 0;
}
#include <stdio.h>
int main() {
int month, year;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
printf("Month %d has 30 days.\n", month);
break;
case 2: // February
// Checking for leap year for February
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("February %d has 29 days (Leap Year).\n", year);
} else {
printf("February %d has 28 days.\n", year);
}
break;
default:
printf("Invalid month entered. Please enter a month between 1 and 12.\n");
}
return 0;
}