Introduction To Programming EC-105

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Introduction to Programming

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

• Selection statements cause one section of code


to be executed or not depending on a
conditional clause.
– if statement
– switch statement
The if statement

• Used where the execution of an


action depends on the satisfaction of
a condition. T

– if condition is true -> action is done


The if statement

• Used where the execution of an


action depends on the satisfaction of F

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

Syntax Logical or Relational

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

• if is a single-selection statement which


performs an indicated action only when the
condition is TRUE; otherwise the action is
skipped.

• if-else is a double-selection statement which


performs an action when the condition is
TRUE and a different action when the
condition is FALSE.
If - else
• Used where there are
different actions to be
executed depending on the
result of a given condition.
– if condition is true -> action is
done
– if condition is false -> a
different action is done
If - else
• Syntax:
if (condition)
{statement1; statement 2;…. statement n;
}
else
{statement1; statement 2;….statement n;
}

• Note: if the specified condition is true, then all the


statements in the first curly braces will be executed, else all
the statements in the next curly braces will be executed.
If - else
• Determine whether a number is positive or
negative.

• 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;

Output (if condition is true)


 a = b+1;
 the cout statement is ignored in this case because of the
missing { }
Some notes
• An if can exist without an else.
• An else, without an if has no meaning.
The else if keyword

• Used where there are multiple actions to be


executed based on different conditions.
– if condition is true -> action is done
– if condition is false -> next condition is checked
The else if keyword
• Syntax:
if (condition)
{statement1; statement 2;…. statement n;
}
else if (condition 2)
{statement1; statement 2;….statement n;
}
else
{statement1; statement 2;….statement n;
}
The else if keyword
if (x==0)
{cout << " x is ZERO" <<endl;}
else if (x>0)
{cout << " x is positive"
<<endl;}
else
{cout << " x is negative"
<<endl;}
The else if keyword
• Additional alternative control paths
• Conditions evaluated in order until one is met;
inner statement (or statements) are then
executed
• If multiple conditions are true, only first
condition is executed, the remaining are
ignored
Nested if statements

• An if statement within the


executable block of another if
statement
• Use?
• Used where several conditions
need to be met for the
execution of an action.
• Is this completely true? How?
Nested if statements

• Conditions are checked in order,


i.e. the inner condition is
checked only if the outer
condition is satisfied.
– if condition is true -> inner
condition is checked
– if inner condition is true -> action
is done
Nested if statements
• Syntax:
if (condition)
{if (condition)
statement1;//(…. to n)
else
statement;
}
else
{statement1;statement 2;….
statement n;
}
Nested if statements

• Determine whether the number


is positive even, positive odd, or
zero.
• Pseudocode
IF number is greater than 0,
IF number is divisible by 2,
THEN print “positive even”
ELSE print “positive odd”
ELSE print “zero”
Nested if statements
if (num>0)
{if ((num%2)==0)
cout << "positive even" <<
endl;
else
cout << "positive odd" <<
endl;
}
else
cout << "zero" << endl;
Nested if statements
1. if (x%4 == 0)
{if (x%2 == 0)
To associate else with outer if statement:
y= 2;
use braces
}
else
y= 1;

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”;

if (day == 3) else if (day == 3)


cout << "Tuesday”; cout<< "Tuesday”;
if (day == 4) else if (day == 4)
cout << “Wednesday”; cout <<"Wednesday”;

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;

• Runs the same code for either 'A' or 'a'

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.

You might also like