Académique Documents
Professionnel Documents
Culture Documents
STATEMENTS
TRANSFER OF CONTROL
Two constructs
If statement
if
if-else
Switch statement
THE BASIC IF STATEMENT
Syntax
if (Expression)
Action; Expression
false
If the Expression is true true
then execute Action
{
Statement1;
Statement2;
.
.
.
}
Program to check whether the number is divisible
by 3.
main()
{
int n;
cout<< “Enter a number”;
cin>> n;
If (n%3 ==0)
{
Cout<<“Number is divisible”;
}
}
EXAMPLE
int m = 5;
int n = 10;
if (m < n)
{
++m;
++n;
}
cout << " m = " << m << " n = " n << endl;
}
THE IF-ELSE STATEMENT
Syntax
if (Expression)
Action1
else
Action2 Expression
If Expression is true then execute
Action1 otherwise execute Action2 false
true
if (v == 0)
{
cout << "v is 0"; Action1 Action2
}
else
{
cout << "v is not 0";
}
IF…….ELSE STATEMENT
Use for making two way
decision.
For Single Statement:
if (condition)
Statement-1;
Else
Statement-2;
For multiple Statement:
if (condition)
{
Statement1;
Statement2;
}
Else
{
Statement1;
Statement2;
}
FINDING THE MAX
int Value1;
int Value2;
int Max;
Switch statement
An advanced construct
MULTIPLE SELECTION
}
cout<< “Wednesday”; if structure. Why?
else if (day == 4)
{
cout<< “Thursday”;
}
else if (day == 5)
{
cout<< “Friday”;
}
else if (day = 6)
{
cout<< “Saturday”;
}
else {
cout<< “Error - invalid
day.\n”;
}
AN IF-ELSE-IF STATEMENT
if ( number < 0 )
{
cout << number << " is negative" << endl;
}
else if (number > 0 )
{
cout << number << " is positive" << endl;
}
else
{
cout << number << " is zero" << endl;
}
Write a program that inputs test score of a
student and display his grade on the following
criteria:
Test Score Grade
>= 90 A
80 – 89 B
70 – 79 C
60 – 69 D
Below 60 F
Write a program that input a salary. If the salary
is 20000 or more, it deducts 7% of salary. If
the salary is 10000 or more but less than
20000, it deducts 1000 from the salary. If
salary is less than 10000, It deduct nothing.
THE SWITCH MULTIPLE-SELECTION STRUCTURE
switch ( integer expression )
{
case constant1 :
statement(s)
break ;
case constant2 :
statement(s)
break ;
...
default: :
statement(s)
break ;
}
A SWITCH STATEMENT
Char ch;
Cout<<“Enter any character”;
Cin>>ch;
switch (ch)
{
case 'a': case 'A':
cout << ch << " is a vowel" << endl;
break;
case 'e': case 'E':
cout << ch << " is a vowel" << endl;
break;
.
.
.
case 'u': case 'U':
cout << ch << " is a vowel" << endl;
break;
default:
cout << ch << " is not a vowel" << endl;
}
Write a program that input number of day of
week and displays the name of the day. For
example if user enters 1, it displays “Monday”
and so on.
Int n;
Cout<<“Enter any number”;
Cin>>n;
switch (n)
{
case 1:
cout << “Monday" << endl;
break;
case 2:
cout << “Tuesday” << endl;
break;
.
.
.
case 7:
cout << Sunday" << endl;
break;
default:
cout << “ Invalid Number”;
}
Write a program that inputs grade of a student
and display his test score on the following
criteria:
Test Score Grade Test Score
>= 90 A >= 90
80 – 89 B 80 – 89
70 – 79 C 70 – 79
60 – 69 D 60 – 69
Below 60 F Below 60
THE SWITCH STATEMENT
switch (grade)
{
case ‘A’:
cout << “Grade is between 90 & 100”;
break;
case ‘B’:
cout << “Grade is between 80 & 89”;
break;
case ‘C’:
cout << “Grade is between 70 & 79”;
break;
case ‘D’:
cout << “Grade is between 60 & 69”;
break;
case ‘F’:
cout << “Grade is between 0 & 59”;
break;
default:
cout << “You entered an invalid grade.”;
}