0% found this document useful (0 votes)
4 views3 pages

ITE 12 Hands on

The document explains logical conditions and conditional statements in C programming, including 'if', 'else', 'else if', and 'switch' statements. It provides examples for each type of statement, illustrating their usage for decision-making based on conditions. Additionally, it covers nested if statements, demonstrating how to structure complex conditions within other conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

ITE 12 Hands on

The document explains logical conditions and conditional statements in C programming, including 'if', 'else', 'else if', and 'switch' statements. It provides examples for each type of statement, illustrating their usage for decision-making based on conditions. Additionally, it covers nested if statements, demonstrating how to structure complex conditions within other conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ITE 12

Usual logical conditions from mathematics:


 Less than: a < b
 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

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 (&&, ||).

Use if-else if:


✅ You need to check ranges (>, <, >=, <=).
✅ You need complex conditions (&&, ||).
✅ You are working with floating-point numbers or strings.

if else statement example:

_____________________________________________________________________________________________________________________
#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

if else if statement 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;
}

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.

Use switch if:


✅ You are checking one variable against specific fixed values.
✅ You are using integers, characters, or enums (not floats or strings).
✅ You want a cleaner, more structured approach when there are many cases.

Switch statement example:


#include <stdio.h>

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.

You might also like