0% found this document useful (0 votes)
87 views13 pages

Lab 4

Conditional statements in C programming are used to make decisions based on conditions that are evaluated to be true or false. There are several types of conditional statements including if, if-else, if-else-if ladder, and nested if statements. The if statement performs operations if a condition is true, while the if-else statement performs one set of operations if true and another if false. The if-else-if ladder statement checks multiple conditions in a ladder-like structure. Nested if statements allow placing if statements within other conditionals.

Uploaded by

Ibrahim Khan
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)
87 views13 pages

Lab 4

Conditional statements in C programming are used to make decisions based on conditions that are evaluated to be true or false. There are several types of conditional statements including if, if-else, if-else-if ladder, and nested if statements. The if statement performs operations if a condition is true, while the if-else statement performs one set of operations if true and another if false. The if-else-if ladder statement checks multiple conditions in a ladder-like structure. Nested if statements allow placing if statements within other conditionals.

Uploaded by

Ibrahim Khan
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/ 13

Conditional Statements in C programming are used to make decisions based on the conditions.

These conditions are specified by a set of conditional statements having boolean expressions
which are evaluated to a boolean value true or false. This process is called decision making in
‘C.’

Types of conditional statements in C


1. If statement
2. If-Else statement
3. If Else-If ladder statement
4. Nested If statement

If Statement:
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario where we
need to perform the different operations for the different conditions. The syntax of the if
statement is given below:

1. if(expression){
2. //code to be executed
3. }

If-else Statement:
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and the other is for the
incorrectness of the condition. Here, we must notice that if and else block cannot be executed
simultaneously. Using if-else statement is always preferable since it always invokes an
otherwise case with every if condition. The syntax of the if-else statement is given below:

1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }

If else-if ladder Statement:


The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario
where there are multiple cases to be performed for different conditions. In if-else-if ladder
statement, if a condition is true then the statements defined in the if block will be executed,
otherwise if some other condition is true then the statements defined in the else-if block will be
executed, at the last if none of the condition is true then the statements defined in the else
block will be executed. There are multiple else-if blocks possible. It is similar to the switch case
statement where the default is executed instead of else block if none of the cases is matched.
The syntax of If else-if statement is given below:

1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }

Nested If Statement:
Nested if statement in C is the nesting of if statement within another if statement and nesting
of if statement with an else statement. Once an else statement gets failed there are times when
the next execution of statement wants to return a true statement, there we need nesting of if
statement to make the entire flow of code in a semantic order. Nested if statement in C plays a
very pivotal role in making a check on the inner nested statement of if statement with an else
very carefully. The syntax of nested if statement is given below:

//check if the first condition holds


if (condition 1) {

//if the second condition holds


if (condition 2) { do
something
}
//if the second condition does not hold
else { do something else
}

}
// if the first condition does not hold
else{

//if the third condition holds


if (condition 3) { do
something
}
//if the third condition does not hold
else { do something else
}

Level: 1
1. What would be the output of:

#include <stdio.h>
int main(){ int a =
5;
int b,c; if
(a==4){
b = 6;
c = 7;
}
printf("%d %d\n",b,c);
}
2. What would be the value of:

'A'>'a'

3. What would be the output of:

#include <stdio.h>
int main(){ if (1){
printf("Hello\n");
}
}

4. What is the error in the code:

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


int a = 5;
if (a=4){
printf("Hello\n");
}
}
THERE IS NO ERROR!!

5. What would be the output of:

#include <stdio.h>
int main(){
if(1){
if(2>4 && 5>3){
printf("COOL\
n"); }
}
}

Level: 2
1. Take values of length and breadth of a rectangle from user and check if it is square or not.
2. Take two int values from user and print greatest among them.

3. A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity Suppose,
one unit will cost 100.
Judge and print total cost for user.
4. A company decided to give bonus of 5% to employee if his/her year of service is more
than 5 years.
Ask user for their salary and year of service and print the net bonus amount.

5. A school has following rules for grading system:


a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
6. Take input of age of 3 people by user and determine oldest and youngest among them.

7. Write a C program to print absolute vlaue of a number entered by user. E.g.INPUT: 1


OUTPUT: 1
INPUT: -1 OUTPUT: 1
8. If
x = 2 y = 5 z = 0 then find values of the
following expressions: a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)

9. A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from
user Number of classes held
Number of classes attended.
And print percentage of class
attended
Is student is allowed to sit in exam or not.
10. Modify the above question to allow student to sit if he/she has medical cause. Ask
user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
Level: 3
1. Write a C program to check if a year is leap year or not. If a year is divisible by 4
then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be
divisible by 400.
2. A 4 digit number is entered through keyboard. Write a C program to print a new
number with digits reversed as of orignal one. E.g.-
INPUT : 1234 OUTPUT : 4321
INPUT : 5982 OUTPUT : 2895

3. Write a C program to check whether a entered character is lowercase ( a to z ) or


uppercase ( A to Z ).
4. Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using
following rules print their place of service.
if employee is female, then she will work only in urban areas.
if employee is a male and age is in between 20 to 40 then he may work in anywhere if
employee is male and age is in between 40 to 60 then he will work in urban areas only.
And any other input of age should print "ERROR".

You might also like