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

6-Conditional Statements If Else

The document provides an overview of conditional statements in C programming, specifically focusing on if, else, else if, and switch statements. It includes syntax examples, explanations of how to use these statements, and practical exercises for students to apply their knowledge. Additionally, it covers the ternary operator as a shorthand for if-else statements and presents real-life examples of conditional logic.

Uploaded by

eishaahmed17
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 views9 pages

6-Conditional Statements If Else

The document provides an overview of conditional statements in C programming, specifically focusing on if, else, else if, and switch statements. It includes syntax examples, explanations of how to use these statements, and practical exercises for students to apply their knowledge. Additionally, it covers the ternary operator as a shorthand for if-else statements and presents real-life examples of conditional logic.

Uploaded by

eishaahmed17
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/ 9

DEPARTMENT OF AVIONICS

ENGINEERING

SUBJECT : Computer Programming Lab

LAB NO : 06

TITLE : Conditional statements – if/else


SUBMITTED TO : Ms. Maha Intakhab Alam
SUBMITTED BY :
BATCH : Avionics 10
SECTION :
Marks Obtained :

Remarks:

DEADLINE:

DATE OF SUBMISSION:
1 CONDITIONS AND IF STATEMENTS
You have already learned that C supports the 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

You can use these conditions to perform different actions for different decisions.

C has the following 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

2 THE IF STATEMENT
Use the if statement to specify a block of code to be executed if a condition
is true.

Syntax
if (condition) {
// block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.

In the example below, we test two values to find out if 20 is greater than 18. If
the condition is true, print some text:

Example
if (20 > 18) {
printf("20 is greater than 18");
}
Try it Yourself »

We can also test variables:

Example
int x = 20;
int y = 18;
if (x > y) {
printf("x is greater than y");
}

Try it Yourself »

Example explained

In the example above we use two variables, x and y, to test whether x is greater
than y (using the > operator). As x is 20, and y is 18, and we know that 20 is

greater than 18, we print to the screen that "x is greater than y". rcises
Exercise:

Print "Hello World" if x is greater than y.

int x = 50;
int y = 10;
(x y) {
printf("Hello World");
}

3 THE ELSE STATEMENT


Use the else statement to specify a block of code to be executed if the condition
is false.

Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."

Try it Yourself »
Example explained
In the example above, time (20) is greater than 18, so the condition is false.
Because of this, we move on to the else condition and print to the screen "Good
evening". If the time was less than 18, the program would print "Good day".

4 THE ELSE IF STATEMENT


Use the else if statement to specify a new condition if the first condition
is false.

Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}

Example
int time = 22;
if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."

Try it Yourself »
Example explained
In the example above, time (22) is greater than 10, so the first
condition is false. The next condition, in the else if statement, is also false,
so we move on to the else condition since condition1 and condition2 is
both false - and print to the screen "Good evening".

However, if the time was 14, our program would print "Good day."

5 SHORT HAND IF...ELSE (TERNARY OPERATOR)


There is also a short-hand if else, which is known as the ternary
operator because it consists of three operands. It can be used to replace
multiple lines of code with a single line. It is often used to replace simple if else
statements:

Syntax
variable =
(condition) ? expressionTrue : expressionFalse;

Instead of writing:

Example
int time = 20;
if (time < 18) {
printf("Good day."
);
} else {
printf("Good evening.");
}

Try it Yourself »

You can simply write:

Example
int time = 20;
(time < 18) ? printf("Good day.") : printf("Good evening.");

Try it Yourself »

It is completely up to you if you want to use the traditional if...else statement or


the ternary operator.
6 REAL-LIFE EXAMPLES
This example shows how you can use if..else to "open a door" if the user
enters the correct code:

Example
int doorCode = 1337;

if (doorCode == 1337) {
printf("Correct code.\nThe door is now open.");
} else {
printf("Wrong code.\nThe door remains closed.");
}

Try it Yourself »

This example shows how you can use if..else to find out if a number is
positive or negative:

Example
int myNum = 10; // Is this a positive or negative number?

if (myNum > 0) {
printf("The value is a positive number.");
} else if (myNum < 0) {
printf("The value is a negative number.");
} else {
printf("The value is 0.");
}

Try it Yourself »

Find out if a person is old enough to vote:

Example
int myAge = 25;
int votingAge = 18;

if (myAge >= votingAge) {


printf("Old enough to vote!");
} else {
printf("Not old enough to vote.");
}

Try it Yourself »
Find out if a number is even or odd:

Example
int myNum = 5;

if (myNum % 2 == 0) {
printf("%d is even.\n", myNum);
} else {
printf("%d is odd.\n", myNum);
}

Practice Exercises
1. Write a C program that takes an integer as input and checks if it is
even or odd. If the number is even, the program should print "Even",
and if it is odd, the program should print "Odd".

2. Write a C program that takes three integers as input and finds the
largest among them.
#include <stdio.h>
int main() {
int num1, num2, num3, largest;

// Prompting the user to enter three integers


printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Assuming the first number is the largest initially


largest = num1;

// Checking if the second number is larger than the current largest


if (num2 > largest) {
largest = num2;
}

// Checking if the third number is larger than the current largest


if (num3 > largest) {
largest = num3;
}

// Printing the largest number


printf("The largest number is: %d\n", largest);

return 0;
}
3. Write a C program that takes the marks of a student in five subjects
(out of 100) as input and calculates the total marks and average
marks. Also, the program should display a message indicating whether
the student has passed or failed. Consider the pass mark to be 40 in
each subject.

4. Write a C program that takes an integer as input and checks whether it


is a prime number or not. A prime number is a positive integer greater
than 1 that has no positive integer divisors other than 1 and itself.

5. Write a C program that checks whether a given year is a leap year or


not. A leap year is divisible by 4, but not divisible by 100 unless it is
also divisible by 400. The program should prompt the user to enter a
year and then print whether it is a leap year or not.
6. Write a C program that takes a student's score as input and calculates
their grade based on the following criteria:
A score of 90 or above: Grade A
A score between 80 and 89: Grade B
A score between 70 and 79: Grade C
A score between 60 and 69: Grade D
A score below 60: Grade F The program should prompt the user to
enter the score and then print the corresponding grade.

You might also like