Introduction To Programming EC-105
Introduction To Programming EC-105
Introduction To Programming EC-105
EC-105
Lecture 4
Selection/Conditional statements
Program types
• All programs can be written in terms of only
three control structures, namely,
– the sequence structure,
– the selection structure (Branching) and
– the repetition structure (Looping).
Selection/Conditional statements
a condition.
– if condition is true -> action is done
– if condition is false -> action is not done
The if statement
Any statement that
results in True or False
if (condition)
T
{ statement1; F
statement 2;
….
statement n;
}
The if statement
• Develop a system that tells the user that he is
over-speeding if his speed crosses120km/hr.
• Pseudocode
IF speed is greater than 120km/hr
THEN print “over-speeding”
The if statement
if (speed>120)
{cout<<"over speeding";
cout << "reduce speed now";
} T
… F
…
…
If - else
• Pseudocode
• IF number is greater than or equal to 0,
THEN print “positive”
• ELSE print “negative”
If - else
if (number>=0)
cout<<"number is positive";
else
cout<<"number is negative";
If - else
• TRY ME! : for even numbers add half the
number to y. For odd numbers, subtract half.
if (x%2==0)
y += x/2;
else
y -= x/2;
Some notes
• If there is only one statement following the if,
then the curly braces are optional.
• If there are multiple statements, the braces
are necessary.
• Same is true for the statements following the
else.
Some notes
– E.g.
if (a<b)
a=b+1;
b = 10;
cout<<"print A"<<a;
2. if (x < 0)
if (y != 4)
z = y * x;
else
z = y / x;
else if (y > 4)
z = y + x;
else
z = y - x;
If else-if
if (day == 1) if (day == 1)
cout<< "Sunday”; cout<< "Sunday”;
if (day == 2) else if (day == 2)
cout << "Monday”; cout<< "Monday”;
Here, each if condition will be Here, the compiler will skip the
checked even if the true one is remaining if conditions
found earlier on. following the true one.
switch-statement Syntax
• switch (controlling expression)
{
case Constant_1: statement_Sequence_1
break;
case Constant_2:
Statement_Sequence_2
break;
case Constant_n:
Statement_Sequence_n
break;
default:
Default_Statement_Sequence
}
33
The Controlling Statement
• A switch statement's controlling statement
must return one of these types
- A bool value
- An enum constant
- An integer type
- A character
• The value returned is compared to the
constant values after each "case"
- When a match is found, the code for that case is used
34
The break Statement
• The break statement ends the switch-statement
- Omitting the break statement will cause the code
for the next case to be executed!
- Omitting a break statement allows the use of
multiple case labels for a section of code
• case 'A':
case 'a':
cout << "Excellent.";
break;
35
The default Statement
•If no case label has a constant that matches
the controlling expression, the statements
following the default label are executed
- If there is no default label, nothing happens when
the switch statement is executed
- It is a good idea to include a default section
36
The switch statement
cout <<"Enter a number between 1 & 3";
cin >> a;
switch(a)
{
case 1:
cout <<"it is case 1\n";
break;
case 2:
cout <<"it is case 2\n";
break;
case 3:
cout <<"it is case 3\n";
break;
default:
cout <<"it is default";
break;
}
Home task (Try to do these without
Loops)
• Program to Check input character as Vowel or
Consonant
• Program to Find the Largest Number Among
Three integers
• Program to Make a Simple Calculator to Add,
Subtract, Multiply or Divide Using
switch...case
• Write a program which takes 5 integers as
input and sort them in ascending order.