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

Chapter 3

The document discusses different types of conditional statements in C including if-else statements, else if statements, ternary operators, and switch statements. It provides examples of using if-else, else if, and ternary operators to check conditions and execute different code blocks based on the results. Key concepts covered include using curly braces, the break keyword in switch statements, and properties of switch statements like case order and nested switches.

Uploaded by

KESHAV SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Chapter 3

The document discusses different types of conditional statements in C including if-else statements, else if statements, ternary operators, and switch statements. It provides examples of using if-else, else if, and ternary operators to check conditions and execute different code blocks based on the results. Key concepts covered include using curly braces, the break keyword in switch statements, and properties of switch statements like case order and nested switches.

Uploaded by

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

Chapter 3 (Conditional Statements)

Types -
1. if-else
if(Condition) {
//do something if true
}
else{
//do something if false
}

For ex.-
#include<stdio.h>

int main() {
int age;
printf("enter age : ");
scanf("%d", &age);

if (age > 18) {


printf("they are adult \n");
printf("they can vote \n");
printf("they can drive \n");
}
else {
printf("they are not adult" \n);
}

printf("thank you");
return 0;
}

Note : Curly brackets are compulsory when there are multiple statements, not on
single statement. But it is recommended to use in both types so that chances of
error are low.

else if (normally used when there are many conditioning staements)

if(Condition) {
//do soemthing if true
}
else if (Condition 2) {
//do something if 1st is false and 2nd is true
}

For ex-
#include<stdio.h>

int main() {
int age;
printf("enter age : ");
scanf("%d", &age);
if(age >= 18) {
printf("adult \n");
}
else if(age > 13 && age <18) {
printf("teenager \n");
}
else {
printf("child");
}

return 0;
}

If - If mutiple statements are there and all are independnet irrespective to other
and we have to check all statements seperately, then we can use 'If' in every line.

Ternary Operator

Syntax -
Condition? do Something if True : do Something if false

for ex-
age >= 18 ? printf("adult \n") : printf("not adult \n")

Note : here " : " stands for otherwise

2. Switch

switch(number) {
case C1: //do something
break;
case C2: //do somthing
break;
default: //do something
}

used in same syntax. and writing break; after a case is compulsory otherwise it
will print all results below the actual result instead of only printing actual
result.

Properties of Switch:
a. Cases can be in any order.
b. Nested switch (switch inside switch) are allowed.

Example of nested if :
int main() {
printf("enter number : ");
scanf("%d", &number);

if(number >= 0) {
printf("positive \n");
if(number % 2==0) {
printf("even \n");
}
else {
printf("odd \n");
}

}
else {
printf("negative \n");
}
}
Q. Write a program to check if a student passed or failed.
marks > 30 is pass
marks <= 30 is fail

Q. Write a program to give grades to a student


marks < 30 is C
30 <= marks < 70 is B
70 <= marks < 90 is A
90 <= marks <= 100 is A+

Q. Will this code :

int x = 2;

if(x = 1) {
printf("x is equal to 1");
} else {
printf("x is not equal to 1");
}

a. give error
b. print "x is equal to 1"
c. print "x is not equal to 1"

Q. Write a Program to find if a characcter entered by a user is upper case or not.

Q. Write a program to check if a given number is Armstrong number or not.

Q. Write a prgram to check if the given number is a natural number.

You might also like