Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views19 pages

Chapter 4 - If Statement in C

if statement in c programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Chapter 4 - If Statement in C

if statement in c programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

If statement

●​ The if statement is a decision-making statement in


C.
●​ It lets the program check a condition.
●​ If the condition is true, the program runs a block of
code.
●​ If the condition is false, that block of code is skipped.​
🛠️ Basic form:
if (condition) {

// code to run if condition is true

🧠 How it works (step-by-step):


1.​The computer looks at the condition (a logical test
like x > 10).​

2.​If the condition is true (yes), it runs the code inside {


}.​

3.​If the condition is false (no), it skips the code inside


{ } and continues with the rest of the program.​

🧪 Example 1: Check if a number is positive

#include <stdio.h>
int main() {

int n;

printf("Enter a number: ");

scanf("%d", &n);

if (n > 0) {

printf("The number is positive.\n");

printf("This line runs always.\n");

return 0;

●​ If user enters 5, condition n > 0 is true → prints


"The number is positive."​

●​ If user enters -2, condition is false → skips the


message.​
🧪 Example 2: if…else (optional)
Sometimes you want to do one thing if the condition is
true, another if it’s false.


For that, you use if…else:

#include <stdio.h>

int main() {
int n;

printf("Enter a number: ");

scanf("%d", &n);

if (n > 0) {

printf("Positive\n");

} else {

printf("Not positive\n");

return 0;

if–else if Statement
●​ The if–else if ladder is used when you have multiple conditions to test, one after
another.​

●​ The program checks conditions in order:​

○​ If one condition is true → it runs that block and skips the rest.​

○​ If none are true → it goes to the final else.​


🛠️ General Syntax
if (condition1) {
// code if condition1 is true
}
else if (condition2) {
// code if condition2 is true
}
else if (condition3) {
// code if condition3 is true
}
else {
// code if none of the above are true
}

#include <stdio.h>
int main() {
int day = 3;
printf("Day Number: %d\n", day);
// Check the day of the week
if (day == 1) {
printf("Day Name: Monday\n");
} else if (day == 2) {
printf("Day Name: Tuesday\n");
} else if (day == 3) {
printf("Day Name: Wednesday\n");
} else if (day == 4) {
printf("Day Name: Thursday\n");
} else if (day == 5) {
printf("Day Name: Friday\n");
} else if (day == 6) {
printf("Day Name: Saturday\n");
} else if (day == 7) {
printf("Day Name: Sunday\n");
} else {
printf("Invalid day Number\n");
}

return 0;
}

🧪 Example: Check if number is positive, negative, or zero


#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

if (n > 0) {
printf("Positive\n");
}
else if (n < 0) {
printf("Negative\n");
}
else {
printf("Zero\n");
}

return 0;
}

Nested if?
●​ A nested if means an if statement inside another if statement.​

●​ It is used when you need to check multiple conditions step by


step.​

🛠️ General Syntax
if (condition1) {
if (condition2) {
// Code runs only if BOTH condition1 and condition2 are true
}
}
🧪 Example 1: Check if a number is positive and even
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

if (n > 0) { // First check: positive?


if (n % 2 == 0) { // Second check: even?
printf("Number is positive and even.\n");
} else {
printf("Number is positive but odd.\n");
}
} else {
printf("Number is not positive.\n");
}

return 0;
}
Examples

1. Check if a number is positive


Algorithm

1.​ Start​

2.​ Read a number (n)​

3.​ If n > 0 → Print "Positive"​

4.​ Else → Print "Not Positive"​

5.​ Stop​

Flowchart (description):

C Program

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0) {
printf("Positive\n");
} else {
printf("Not Positive\n");
}
return 0;
}
2. Check if a number is even
Algorithm

1.​ Start​

2.​ Read n​

3.​ If n % 2 == 0 → Print "Even"​

4.​ Else → Print "Odd"​

5.​ Stop​

Flowchart:​
Start → Input n → Decision (n % 2 == 0?) → Yes: Even, No: Odd → End

C Program

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}

3. Find largest of two numbers


Algorithm

1.​ Start​
2.​ Read a, b​

3.​ If a > b → Print a is largest​

4.​ Else → Print b is largest​

5.​ Stop​

Flowchart:​
Start → Input a, b → Decision (a > b?) → Yes: Print a largest → No: Print b largest → End

C Program

#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) {
printf("%d is largest\n", a);
} else {
printf("%d is largest\n", b);
}
return 0;
}

4. Check if a character is vowel


Algorithm

1.​ Start​

2.​ Read ch​

3.​ If ch in {a, e, i, o, u, A, E, I, O, U} → Print "Vowel"​

4.​ Else → Print "Not Vowel"​

5.​ Stop​
Flowchart:​
Start → Input ch → Decision (ch is vowel?) → Yes: Vowel → No: Not Vowel → End

C Program

#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') {
printf("Vowel\n");
} else {
printf("Not Vowel\n");
}
return 0;
}

5. Check if year is leap year


Algorithm

1.​ Start​

2.​ Read year​

3.​ If (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) → Leap year​

4.​ Else → Not leap year​

5.​ Stop​

Flowchart:​
Start → Input year → Decision (conditions?) → Yes: Leap year → No: Not leap year → End

C Program

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("Leap Year\n");
} else {
printf("Not Leap Year\n");
}
return 0;
}

6. Check eligibility to vote


Algorithm

1.​ Start​

2.​ Read age​

3.​ If age >= 18 → Eligible​

4.​ Else → Not eligible​

5.​ Stop​

Flowchart:​
Start → Input age → Decision (age >= 18?) → Yes: Eligible → No: Not eligible → End

C Program

#include <stdio.h>
int main() {
int age;
printf("Enter age: ");
scanf("%d", &age);
if (age >= 18) {
printf("Eligible to vote\n");
} else {
printf("Not eligible to vote\n");
}
return 0;
}

7. Check if a number is zero


Algorithm

1.​ Start​

2.​ Read n​

3.​ If n == 0 → Print "Zero"​

4.​ Else → Print "Not Zero"​

5.​ Stop​

Flowchart:​
Start → Input n → Decision (n == 0?) → Yes: Zero → No: Not Zero → End

C Program

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n == 0) {
printf("Zero\n");
} else {
printf("Not Zero\n");
}
return 0;
}
8. Check pass or fail
Algorithm

1.​ Start​

2.​ Read marks​

3.​ If marks >= 40 → Pass​

4.​ Else → Fail​

5.​ Stop​

Flowchart:​
Start → Input marks → Decision (marks >= 40?) → Yes: Pass → No: Fail → End

C Program

#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 40) {
printf("Pass\n");
} else {
printf("Fail\n");
}
return 0;
}

9. Find absolute value


Algorithm

1.​ Start​
2.​ Read n​

3.​ If n < 0 → n = -n​

4.​ Print n​

5.​ Stop​

Flowchart:​
Start → Input n → Decision (n < 0?) → Yes: n = -n → Print n → End

C Program

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0) {
n = -n;
}
printf("Absolute value: %d\n", n);
return 0;
}

10. Check if divisible by 5 and 11


Algorithm

1.​ Start​

2.​ Read n​

3.​ If (n % 5 == 0 && n % 11 == 0) → Print "Divisible by both"​

4.​ Else → Print "Not divisible by both"​

5.​ Stop​
Flowchart:​
Start → Input n → Decision (n % 5 == 0 && n % 11 == 0?) → Yes: Divisible → No: Not divisible
→ End

C Program

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 5 == 0 && n % 11 == 0) {
printf("Divisible by both 5 and 11\n");
} else {
printf("Not divisible by both 5 and 11\n");
}
return 0;
}

You might also like