KEMBAR78
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
PROGRAMMING FUNDAMENTALS
Lecturer:
Syed M Waqas Shah
NUML Multan
LECTURE 5
National University of Modern Languages (NUML)
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.
– if condition is true -> action is done
T
The if statement
• Used where the execution of an
action depends on the satisfaction of
a condition.
– if condition is true -> action is done
– if condition is false -> action is not done
F
The if statement
Syntax
if (condition)
{ statement1;
statement 2;
….
statement n;
}
Condition to be
checked
Any statement that
results in True or False
T
F
The if statement
• Develop a system that tells the user that he is
over-speeding if his speed crosses120km/hr.
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"<<endl;
cout << "reduce speed now";
}
…
…
…
T
F
T
F
The if statement (Example)
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";
NOTE: {} are not used here
If – else (example)
If - else
• TRY ME! : for positive numbers add half the
number to y. For negative numbers, subtract
half.
If – else (cont.)
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;
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;}
If, Else-if Example
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
The if , else keyword with Multiple
Conditions
• Print A if the given number is positive And less
than 10, else print B.
IF- else (multiple condition)
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 statement (example)
Why else-if
If else-if
if (day == 1)
cout<< "Sunday”;
if (day == 1)
cout<< "Sunday”;
if (day == 2)
cout << "Monday”;
else if (day == 2)
cout<< "Monday”;
if (day == 3)
cout << "Tuesday”;
else if (day == 3)
cout<< "Tuesday”;
if (day == 4)
cout << “Wednesday”;
else if (day == 4)
cout <<"Wednesday”;
Here, each if condition will be
checked even if the true one is
found earlier on.
Here, the compiler will skip the
remaining if conditions
following the true one.
switch-statement Syntax
• switch (selector) // controlling statement
{
case label_1: statement_Sequence_1
break;
case label_2:
Statement_Sequence_2
break;
case label_n:
Statement_Sequence_n
break;
default:
Default_Statement_Sequence
}
33
The Controlling Statement
• The value in switch statement 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
Switch (example)
• Take input from the user. Print the following
lines w.r.t to the input
– If user input = 1 Print “It is case 1”
🡪
– If user input = 2 Print “It is case 2”
🡪
– If user input = 3 Print “It is case 3”
🡪
– Otherwise Print “default”
🡪
The switch statement
cout <<"Enter a number ";
cin >> a;
switch(a)
{
case 1:
cout <<"it is case 1n";
break;
case 2:
cout <<"it is case 2n";
break;
case 3:
cout <<"it is case 3n";
break;
default:
cout <<"it is default";
break;
}
Switch (example)
Write a program that takes an integer as input from user and do following things
∙ If the number is multiple of 5 then display "Number is a multiple of 5"
∙ If it is not multiple of 5, then check whether it is a multiple of 3 or 2 and display "The
number is multiple of 3 but not 5" or "The number is multiple of 2 but not 5".

Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx

  • 1.
    PROGRAMMING FUNDAMENTALS Lecturer: Syed MWaqas Shah NUML Multan LECTURE 5 National University of Modern Languages (NUML)
  • 2.
  • 3.
    Program types • Allprograms can be written in terms of only three control structures, namely, – the sequence structure, – the selection structure (Branching) and – the repetition structure (Looping).
  • 4.
    Selection/Conditional statements • Selectionstatements cause one section of code to be executed or not depending on a conditional clause. – if statement – switch statement
  • 5.
    The if statement •Used where the execution of an action depends on the satisfaction of a condition. – if condition is true -> action is done T
  • 6.
    The if statement •Used where the execution of an action depends on the satisfaction of a condition. – if condition is true -> action is done – if condition is false -> action is not done F
  • 7.
    The if statement Syntax if(condition) { statement1; statement 2; …. statement n; } Condition to be checked Any statement that results in True or False T F
  • 8.
    The if statement •Develop a system that tells the user that he is over-speeding if his speed crosses120km/hr.
  • 9.
    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”
  • 10.
    The if statement if(speed>120) {cout<<"over speeding"<<endl; cout << "reduce speed now"; } … … … T F T F
  • 11.
  • 12.
    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.
  • 13.
    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
  • 14.
    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.
  • 15.
    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”
  • 16.
    If - else if(number>=0) cout<<"number is positive"; else cout<<"number is negative"; NOTE: {} are not used here
  • 17.
    If – else(example)
  • 18.
    If - else •TRY ME! : for positive numbers add half the number to y. For negative numbers, subtract half.
  • 19.
    If – else(cont.)
  • 20.
    Some notes • Ifthere 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
  • 21.
    Some notes – E.g. if(a<b) a=b+1; cout<<"print A"<<a; Output (if condition is true) ⮚ a = b+1; ⮚ the cout statement is ignored in this case because of the missing { }
  • 22.
    Some notes • Anif can exist without an else. • An else, without an if has no meaning.
  • 23.
    The else ifkeyword • 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
  • 24.
    The else ifkeyword • Syntax: if (condition) {statement1; statement 2;…. statement n; } else if (condition 2) {statement1; statement 2;….statement n; } else {statement1; statement 2;….statement n; }
  • 25.
    The else ifkeyword if (x==0) {cout << " x is ZERO" <<endl;} else if (x>0) {cout << " x is positive" <<endl;} else {cout << " x is negative" <<endl;}
  • 26.
  • 27.
    The else ifkeyword • 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
  • 28.
    The if ,else keyword with Multiple Conditions • Print A if the given number is positive And less than 10, else print B.
  • 29.
  • 30.
    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?
  • 31.
    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
  • 32.
    Nested if statements •Syntax: if (condition) {if (condition) statement1;//(…. to n) else statement; } else {statement1;statement 2;…. statement n; }
  • 33.
    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”
  • 34.
    Nested if statements if(num>0) {if ((num%2)==0) cout << "positive even" << endl; else cout << "positive odd" << endl; } else cout << "zero" << endl;
  • 35.
  • 36.
  • 37.
    If else-if if (day== 1) cout<< "Sunday”; if (day == 1) cout<< "Sunday”; if (day == 2) cout << "Monday”; else if (day == 2) cout<< "Monday”; if (day == 3) cout << "Tuesday”; else if (day == 3) cout<< "Tuesday”; if (day == 4) cout << “Wednesday”; else if (day == 4) cout <<"Wednesday”; Here, each if condition will be checked even if the true one is found earlier on. Here, the compiler will skip the remaining if conditions following the true one.
  • 38.
    switch-statement Syntax • switch(selector) // controlling statement { case label_1: statement_Sequence_1 break; case label_2: Statement_Sequence_2 break; case label_n: Statement_Sequence_n break; default: Default_Statement_Sequence } 33
  • 39.
    The Controlling Statement •The value in switch statement is compared to the constant values after each "case" - When a match is found, the code for that case is used 34
  • 40.
    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
  • 41.
    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
  • 42.
    Switch (example) • Takeinput from the user. Print the following lines w.r.t to the input – If user input = 1 Print “It is case 1” 🡪 – If user input = 2 Print “It is case 2” 🡪 – If user input = 3 Print “It is case 3” 🡪 – Otherwise Print “default” 🡪
  • 43.
    The switch statement cout<<"Enter a number "; cin >> a; switch(a) { case 1: cout <<"it is case 1n"; break; case 2: cout <<"it is case 2n"; break; case 3: cout <<"it is case 3n"; break; default: cout <<"it is default"; break; }
  • 44.
  • 45.
    Write a programthat takes an integer as input from user and do following things ∙ If the number is multiple of 5 then display "Number is a multiple of 5" ∙ If it is not multiple of 5, then check whether it is a multiple of 3 or 2 and display "The number is multiple of 3 but not 5" or "The number is multiple of 2 but not 5".

Editor's Notes

  • #15 Evaluate condition if (x%2==0) If true, evaluate inner statement y += x/2; Otherwise, do nothing
  • #16 Evaluate condition if (x%2==0) If true, evaluate inner statement y += x/2; Otherwise, do nothing
  • #18 Evaluate condition if (x%2==0) If true, evaluate inner statement y += x/2; Otherwise, do nothing