Professional Documents
Culture Documents
CFP Lab 9
CFP Lab 9
9
IMPLEMENTATION OF CONDITIONAL STATEMENTS IN C++
Objectives:
Introduction:
Theory:
IF statement:
Syntax of if statement:
if (condition)
statements;
Fig 1
if ... else statement is a two way branching statement. It is similar to if statement but the only
difference is if the condition is false then a different block of statements is executed which is
inside else statement.
if (condition)
statements;
else
statements;
Fig 2
if ... else if ... else statement is used for multiple branching. When there are two or more
conditions that needs to be checked to decide which block of statement is to be executed, it is
used. The number of else if statements depends upon the number of conditions to be checked.
if (condition 1)
statements;
else if (condition 2)
statements;
}
... ... ...
else if (condition n)
statements;
else
statements;
Fig 3
LAB TASKS
1. Write program to check percentage of a student and display pass if it is greater than
40.
#include <iostream>
#include <conio.h>
int main()
int marks;
cin>>marks;
if (marks>=40)
else
getch();
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int year;
cin>>year;
else
getch();
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int number;
cin>>number;
if(number>0)
cout<<"number is positive";
else if(number<0)
cout<<"number is negative";
else
cout<<"number is zero";
getch();
#include <iostream>
#include <conio.h>
int main()
int number;
if(number % 2 ==0)
else
return 0;
#include <iostream>
#include <conio.h>
void main()
int num1,num2;
if(num1 = num2)
else
getch();
#include <iostream>
#include <conio.h>
void main()
int bill,unit;
if(units<300)
bill=units*2;
bill=units*5;
getch();
#include <iostream>
#include <conio.h>
int main()
float percent;
cin>>percent;
if (percent>=80)
}
else if (percent>=60)
else if (percent>=50)
else if (percent>=40)
else
getch();
8. Write a Program to check whether a number is greater than or smaller than other.
#include <iostream>
#include <conio.h>
void main()
int a,b,c;
cout << "Enter three numbers: ";
cin >>a>>b>>c;
if(a<b)
if(a<c)
else
if(b<c)
else
getch();
#include <iostream>
#include <conio.h>
void main()
char ch;
cout<<"Enter a character\n";
cin>>ch;
cout<<ch<<" is a vowel.\n";
else
getch();
String:
Strings are objects that represent sequences of characters. The standard string class provides
support for such objects with an interface similar to that of a standard container of bytes, but
adding features specifically designed to operate with strings of single-byte characters.
#include <iostream>
#include <conio.h>
void main()
char name[50];
cin>>name;
getch();
#include <iostream>
#include <conio.h>
void main()
{char name[50];
cout<<”enter your name:”;
cin>>name;
cin.getline(name,50);
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 switch
case.
Syntax:
switch(expression) {
case constant-expression:
statement(s);
break; /* optional */
case constant-expression:
statement(s);
break; /* optional */
default: /* Optional */
statement(s);
1. The expression used in a switch statement must have an integral or enumerated type,
or be of a class type in which the class has a single conversion function to an integral
or enumerated type.
2. You can have any number of case statements within a switch. Each case is followed
by the value to be compared to and a colon.
3. The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.
4. When the variable being switched on is equal to a case, the statements following that
case will execute until a break statement is reached.
5. When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
6. Not every case needs to contain a break. If no break appears, the flow of control will
fall through to subsequent cases until a break is reached.
7. A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases
is true. No break is needed in the default case.
Fig 1
Lab Task
#include<iostream>
#include<conio.h>
void main()
int n;
clrscr();
switch(n)
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
cout<<“thursday is the fifth day of the week”;
break;
case 6:
break;
case 7:
break;
default:
cout<<“not a day”;
getch();
12. Write a program to display month name according to the month number using
switch statement.
#include<iostream>
#include<conio.h>
void main()
int n;
clrscr();
cin>>n;
switch(n)
{
case 1:
cout<<"January\n";
break;
case 2:
cout<<"February\n";
break;
case 3:
cout<<"March\n";
break;
case 4:
cout<<"April\n";
break;
case 5:
cout<<"MAy\n";
break;
case 6:
cout<<"June\n";
break;
case 7:
cout<<"July\n";
break;
case 8:
cout<<"August\n";
break;
case 9:
cout<<"September\n";
break;
case 10:
cout<<"October\n";
break;
case 11:
cout<<"November\n";
break;
case 12:
cout<<"December\n";
break;
default:
break;
getch(); }
#include <iostream>
#include<conio.h>
int main()
char oper;
float num1,num2;
cout<<"enter an operator";
cin>>oper;
cin>>num1>>num2;
switch(oper)
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
break;
getch();
14. Write a program to check for vowel or consonant using switch case.
#include<conio.h>
#include<iostream.h>
Void main()
char ch;
cin>>ch;
switch(ch)
case 'A':
cout<<"Vowel";
break;
case 'E':
cout<<"Vowel";
break;
case 'I':
cout<<"Vowel";
break;
case 'O':
cout<<"Vowel";
break;
case 'U':
cout<<"Vowel";
break;
case 'a':
cout<<"Vowel";
break;
case 'e':
cout<<"Vowel";
break;
case 'i':
cout<<"Vowel";
break;
case 'o':
cout<<"Vowel";
break;
case 'u':
cout<<"Vowel";
break;
default:
cout<<"consonant";
getch();
LAB SESSION