CnotesUnit2
CnotesUnit2
2. Types of if Statements in C
There are four main types of if statements in C:
1. Simple if Statement
2. if-else Statement
3. Nested if-else Statement
4. else-if Ladder
3. Simple if Statement
Definition
Syntax
if (condition) {
// Code to execute if the condition is true
}
Syntax Explanation
Example
#include <stdio.h>
main() {
int num = 10;
if (num > 0) {
printf("Number is positive\n");
}
Explanation
Output:
Number is positive
4. if-else Statement
Definition
Extends the if statement to execute one block if the condition is true, and another if it
is false.
Syntax
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example
#include <stdio.h>
main() {
int num = -5;
if (num >= 0) {
printf("Positive number\n");
} else {
printf("Negative number\n");
}
Explanation
Negative number
Syntax
if (condition1) {
if (condition2) {
// Code if both conditions are true
} else {
// Code if condition1 is true but condition2 is false
}
} else {
// Code if condition1 is false
}
Example
#include <stdio.h>
main() {
int num = 0;
if (num >= 0) {
if (num == 0) {
printf("Number is zero\n");
} else {
printf("Positive number\n");
}
} else {
printf("Negative number\n");
}
Explanation
Output:
Number is zero
6. else-if Ladder
Definition
Syntax
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else if (condition3) {
// Code for condition3
} else {
// Code if none of the conditions are true
}
Example
#include <stdio.h>
main() {
int marks = 75;
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 80) {
printf("Grade B\n");
} else if (marks >= 70) {
printf("Grade C\n");
} else {
printf("Grade D\n");
}
Explanation
Output:
Grade C
Summary
Statement Type Purpose Executes When
Runs a block of code if a condition is
Simple if Condition is true
true
True → if block, False → else
if-else Runs one block if true, another if false
block
Nested if-else if-else inside another if-else One condition inside another
else-if Ladder Checks multiple conditions in sequence First true condition executes
Switch Statement in C
The switch statement in C is a multi-way decision-making statement.
It allows one variable to be tested against multiple possible values.
It is an alternative to multiple if-else statements, making the code more readable and
efficient.
The switch statement evaluates an expression and executes the block of code that
matches a specified case.
If no cases match, an optional default case is executed.
Syntax
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
case value3:
// Code to execute if expression == value3
break;
default:
// Code to execute if no case matches
}
Syntax Explanation
switch (expression):
o expression is evaluated first.
o It must be an integer, char, or enum (not float or string).
case value:
o Each case represents a possible value for the expression.
o If expression matches value, the corresponding block executes.
break;
o Stops execution after a case is matched.
o If missing, the execution falls through to the next case.
default:
o Executes only if none of the cases match.
o It is optional but recommended.
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;
default:
printf("Weekend\n");
}
Explanation
Wednesday
switch (num) {
case 1:
printf("One\n");
case 2:
printf("Two\n");
case 3:
printf("Three\n");
default:
printf("Other\n");
}
Explanation
Output:
Two
Three
Other
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
default:
printf("Fail\n");
}
return 0;
}
Output:
Good
switch vs if-else
Loops in C
Loops are control structures that repeat a block of code multiple times.
They are used when we need to execute a set of statements multiple times with a
condition.
Types of Loops in C:
1. while loop
2. do-while loop
3. for loop
4. Nested Loops (Loop inside another loop)
1. while Loop
Definition
The while loop executes repeatedly as long as the given condition is true.
If the condition becomes false, the loop terminates.
Syntax
while (condition) {
// Code to execute
}
Syntax Explanation
Example
#include <stdio.h>
main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++; // Increment to prevent infinite loop
}
}
Output
1
2
3
4
5
2. do-while Loop
Definition
Similar to while, but executes at least once, even if the condition is false.
Condition is checked after executing the loop body.
Syntax
do {
// Code to execute
} while (condition);
Syntax Explanation
Example
#include <stdio.h>
main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
Output
1
2
3
4
5
4. for Loop
Definition
The most commonly used loop, best for situations where the number of iterations is
known.
It includes initialization, condition, and update in a single line.
Syntax
for (initialization; condition; update) {
// Code to execute
}
Syntax Explanation
Example
#include <stdio.h>
main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
Output
1
2
3
4
5
5. Nested Loops
Definition
Syntax
for (initialization; condition; update) {
for (initialization; condition; update) {
// Inner loop code
}
}
Explanation
6. Summary Table
Loop Type When to Use Execution Condition
When condition-based looping is
while Runs while the condition is true
needed
When the loop must execute at least
do-while Executes first, then checks condition
once
When the number of iterations is
for Executes until condition is false
fixed
Nested When working with tables, Each inner loop completes for every outer
Loops matrices, patterns loop iteration
1. goto Statement
Definition
The goto statement jumps to a labeled statement in the program.
It can transfer control forward or backward within the same function.
Not recommended as it makes code harder to read (considered bad practice).
Syntax
goto label;
...
label:
// Code to execute
Syntax Explanation
Example
#include <stdio.h>
main() {
int num = 1;
if (num == 1)
goto jump; // Jump to label
jump:
printf("Jumped to this label!\n");
Output
Jumped to this label!
2. break Statement
Definition
Syntax
break;
Syntax Explanation
break; immediately exits the loop and resumes execution after the loop block.
Output
CopyEdit
1
2
3. continue Statement
Definition
The continue statement skips the remaining code inside the loop for the current
iteration and moves to the next iteration.
Unlike break, it does not exit the loop but simply skips the rest of the current
iteration.
Syntax
continue;
Syntax Explanation
When continue; is encountered, the remaining statements in the current iteration are
skipped, and the loop moves to the next iteration.
Output
1
2
4
5
printf("\nUsing continue:\n");
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}
return 0;
}
Output
Using break:
1 2
Using continue:
1 2 4 5
6. Summary Table
Jump
Function Effect on Execution Use Case
Statement
goto
Jumps to a labeled Unconditionally jumps to another Avoid unless necessary
statement part of the code (bad practice)
break Exits the loop or Stops loop execution completely When we need to exit
switch early
continue
Skips the current Moves to next iteration, skipping When we need to skip
iteration remaining statements certain iterations