Experiment No 03 (Conditional Statement)
Experiment No 03 (Conditional Statement)
Programming Fundamentals
Experiment no 3
Implementation of Conditional Statements
1. Objective:
If Statement
If-else Statement
Nested if Statement
Nested if else Statement
Switch statement
Logical Operators
Useful Concepts:
Ternary operators:
The conditional operator evaluates an expression, returning one value if that expression evaluates
to true, and a different one if the expression evaluates as false. Its syntax is:
condition ? result 1: result 2
If condition is true, the entire expression evaluates to result1, and otherwise to result2.
Example:
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c << '\n';
}
Conditional Statement
The conditional statements are used to execute a set of statements after testing a condition. The
conditional statements are also called selection statements
i. Example Code
int main()
{
int a;
cout<<”enter a number =:’
Cin>>a;
if (a%3==0)
{
Cout<<" The number is divisible by 3“;
}
Example 2
#include <iostream>
using namespace std;
int main()
{
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
return 0;
}
Output:
The “if..else” Statement
In this statement, one condition and two blocks of statements are given. Either one of the two
blocks of statements is executed after evaluating a condition. If the given condition is true, the
first block of statements is executed. But, If the given condition is false, the first block of
statements is ignored and the second block following the else is executed.
Syntax of the “if else Statement”
If (condition)
Statement – 1;
else
Statement – 2;
In the case of multiple statements, the syntax is written as:
if (condition)
{
Statement – 1;
Statement – 2;
Statement – m;
}
else
Statement 3;
Statement 4;
Statement n
Example Code
#include <stdio.h>
Using namespace std;
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
cout<<"i is 10";
// Since is not 10
// Then execute the else statement
else
cout<<"i is 20";
return 0;
}
Example 2:
#include <iostream>
using namespace std;
int main(){
int num=70;
if( num < 100 ){
/* This cout statement will only execute,
* if the above condition is true
*/
cout<<"number is less than 100";
}
Nested If Statement
When an “if statement” is used within another “if statement”, it is called the “nested if
statement”. The “nested if statement” is used for multi way decision-making. The syntax of
“nested if statement” is:
if (condition-1)
{
if (condition-2)
{
}
if (condition-2)
{
}
else
Example Code
int main() {
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Output:
Switch statement
A switch statement allows a variable to be tested for equality against a list of values. Each value
is called a case, and the variable being switched on is checked for each case.
When to use a switch?
The switch is similar to the if…else…if ladder. However, it generates a cleaner and easy-to-
understand code. The switch is also faster compared to the if…else…if ladder. Use the switch
statement when you need to compare the value of a variable against a set of other values.
The break Keyword
The break keyword is used inside the switch statement. It prevents the code from running into
the next case. It terminates a statement sequence.
When the C++ compiler encounters a break keyword, execution of the switch terminates, and
control jumps to the line that comes after the switch statement. The use of a break statement in a
switch is optional. If not used, execution continues to the next case.
Syntax:
The syntax for a switch statement in C++ is as follows −
switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
Logical Operators
The logical operators are used to combine relational expressions or relational conditions.
The output of a logical expression is also in a logical form. Its value is either true or false.
In C++, following logical operators are involved:
• && (AND Operator)
• || (OR Operator)
• ! (NOT Operator)
Example Code:
For example, we might want to know if the value of variable x is between 10 and 20. This is
actually two conditions: we need to know if x is greater than 10, and also whether x is less then
20.
void main()
{
int n;
cout<<"Enter a number: “;
cin>>n;
if (n >=10 && n<= 20)
cout<<"Your value is between 10 and 20“;
else
cout<<"You value is not between 10 and 20“;
}
1. Write a program that will used ternary operator and enter the age of a person
and print the person is a child or an adult.
2. Write a program to input a number from the key board and then print the
message “It is an odd number” if the given number is odd number. Else print
“It is an even number”.
3. Write a program to input two numbers and then find out whether these
numbers are equal or different.
4. Write a program that input a year and check it is a leap year or not.
5. Write a program in C++ to input marks obtained by a student in a subject. The
total marks are 100. Find out the grade of the student by using the if-else nested
structure. The grade is:
If marks are equal to or greater than 90, grade is A.
If marks are equal to or greater than 70 and less than 90, grade is B.
If marks are equal to or greater than 50 and less than 70, grade is C.
If marks are less than 50, grade is “F”.
6. Write a program that input a number and check whether it is divisible by 5,10
and 15?
7. Write a program that input two numbers and find if 2nd number is square of 1st?
8. Write a program to input two integer values and find whether the 1st and the 2nd
number is greater?
9. Write a program which take input from user and check that whether that letter
is vowel or not?
10. Create a Calculator that performs basic arithmetic functions using the switch
Statement.