ITE 12 Hands on
ITE 12 Hands on
Conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
if
The if statement in C is used for decision-making. It allows the program to execute a block of code only if a condition is true. If the condition
is false, the block is skipped.
if statement example:
______________________________________________________________________________________________________________________
#include <stdio.h>
int main() {
int num = 10;
if (num > 5) {
printf("Number is greater than 5.\n");
}
return 0;
}
if else
Best when conditions involve ranges (>, <, etc.) or multiple conditions (&&, ||).
_____________________________________________________________________________________________________________________
#include <stdio.h>
int main() {
int num = 3;
if (num > 5) {
printf("Number is greater than 5.\n");
} else {
printf("Number is 5 or less.\n");
}
return 0 ;
}
ITE 12
if else if statement
int main() {
int num = 3;
if (num > 5) {
printf ("Number is greater than 5.\n");
} else if (num == 5) {
printf("Number is exactly 5.\n");
} else {
printf("Number is less than 5.\n");
}
return 0;
}
Switch statement
The case keyword in a switch statement is used to define specific conditions (values) that a variable can match. It acts like individual "if"
conditions inside the switch, making the code more structured and efficient.
int main() {
int num = 3;
switch (num) {
case 5:
printf("Number is exactly 5.\n");
break;
default:
if (num > 5) {
printf("Number is greater than 5.\n");
} else {
printf("Number is less than 5.\n");
}
break;
}
return 0;
}
Explanation:
Since switch works best with exact values, we check for num == 5 directly.
For values other than 5, we use default and handle the greater than or less than logic with an if-else.
The break statement ensures execution does not fall through to other cases.
ITE 12
Nested if
In the programming context, the term "nesting" refers to enclosing a particular programming element inside another similar element. For example,
nested loops, nested structures, nested conditional statements, etc. If an if statement in C is employed inside another if statement, then we call it as
a nested if statement in C.
Nested if example:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
int main() {
int num = 3;
if (num > 5) {
printf("Number is greater than 5.\n");
} else {
if (num == 5) {
printf("Number is exactly 5.\n");
} else {
printf("Number is less than 5.\n");
}
}
return 0;
}
The outer if checks if num > 5.
If num is not greater than 5 (else block), an inner if checks if num == 5.
If num is neither greater than 5 nor exactly 5, it must be less than 5, so the final else handles that case.