0% found this document useful (0 votes)
2 views14 pages

Understanding If-Else Statements in C++

The document announces that homework 2 has been assigned involving if statements and parametric functions, with a deadline of October 14, 2015, and provides details on the conditional execution of code using if-else statements, including their syntax and flow, examples of their use, and nested if statements.

Uploaded by

Hamza Yousif
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Understanding If-Else Statements in C++

The document announces that homework 2 has been assigned involving if statements and parametric functions, with a deadline of October 14, 2015, and provides details on the conditional execution of code using if-else statements, including their syntax and flow, examples of their use, and nested if statements.

Uploaded by

Hamza Yousif
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Announcements

 Homework 2 is assigned at SUCourse


 About if statements and parametric functions
 The use of “if” is natural
 The use of “functions” is enforced
• The functions that you have to implement are explained in
the homework document
 There are some other rules
• Please read the homework specification very carefully
 Deadline is Wednesday, October 14, 2015, 19:00

CS201 – Introduction to Computing – Sabancı University 1


Conditional Execution – Sections 4.2,
4.3, 4.4 and 4.7
 Up to now, we have seen that instructions are executed
unconditionally one after another.
 What if you want to execute an instruction depending on a
condition?
 Selection: choose from among many options according to a
criteria
 E.g.
If response is yes do this, else do that
 E.g.
If year is a leap year, number of days is 366, else 365
 You achieve conditional execution with if else statements

CS201 – Introduction to Computing – Sabancı University 2


Syntax
if (condition)
statementtrue;
else
statementfalse;

 If condition is TRUE then statementtrue is executed; if


condition is FALSE then statementfalse is executed.
 else and statementfalse are optional
if (condition)
statementtrue;
 if condition is FALSE then nothing will be executed and
execution continues with the next statement in the program

CS201 – Introduction to Computing – Sabancı University 3


More Syntax
 Condition must be in brackets
 What happens if you have several statements to execute?
 write your statements within curly brackets
 book recommends using this all the time even if you
have only one statement (defensive programming)
if (condition)
{
statementtrue 1;
statementtrue 2; Definiton
... Compound block:
statementtrue N;
Statements written
}
else within matching curly
{ brackets
statementfalse 1;
statementfalse 2;
...
statementfalse N;
}
CS201 – Introduction to Computing – Sabancı University 4
Flow diagram of if-else

test
condition

true false

true false
statements statements

next
statement

CS201 – Introduction to Computing – Sabancı University 5


Example (not in the book)
 Write a program that inputs two integer numbers and
displays the minimum one.
 Two solutions
 using if and else together
 using only if (no else)

CS201 – Introduction to Computing – Sabancı University 6


Solution 1 – with_if_else.cpp
int main ()
{
int num1, num2, min;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

if (num1 < num2) //check if first number is smaller than the second one
{
min = num1; //if so minimum is the first number
}
else
{
min = num2; //otherwise minimum is the second number
}

cout << "minimum of these two numbers is: " << min << endl;
return 0;
}

CS201 – Introduction to Computing – Sabancı University 7


Solution 2 – with_if.cpp (no else)
int main ()
{
int num1, num2, min;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

min = num1; // default assignment - minimum is the first number


if (num2 < min) //check if second number is smaller than the first one
{
min = num2; //if so update the minimum, if not do nothing
}

cout << "minimum of these two numbers is: " << min << endl;
return 0;
}

CS201 – Introduction to Computing – Sabancı University 8


Nested if statements
 if/else statements are inside other if/else statements
 Method to select from multiple choices
 Example: input a numeric grade and display messages
according to its value
0 .. 50 low
51 .. 70 average
71 .. 100 good
otherwise invalid grade
 Several solutions exist (we’ll see three) – not in the book
 first two solution: ifs are after elses
• see if_after_else.cpp and if_after_else2.cpp
 third solution: ifs are after ifs
• see if_after_if.cpp

CS201 – Introduction to Computing – Sabancı University 9


Nested if-else Syntax (else after if)
if (condition_1)
 if condition_1 is TRUE then
statements_1true are executed.
{
All subsequent elses are skipped.
statements_1true;
 if condition_1 is FALSE then check
}
condition_2. If condition_2 is TRUE
else if (condition_2)
then
{
statements_2true are executed.
statements_2true;
All subsequent elses are skipped.
}
 if both condition_1 and condition_2
else if (condition_3)
are FALSE then check condition_3. If
{ condition_3 is TRUE then
statements_3true; statement_3true are executed.
} All subsequent elses are skipped.
. .
. .
. .
else  if all of the conditions are FALSE then
{ statement_allfalse are executed.
statements_allfalse;
} CS201 – Introduction to Computing – Sabancı University 10
Nested if-else Syntax (if after if)
For three conditions, use analogy for more condition cases
if (condition_1) if condition_1 is TRUE then check
{ condition_2
if (condition_2) if condition_2 is also TRUE then
{ check condition_3
if (condition_3)
if condition_3 is also TRUE
{
statements_alltrue are executed,
statements_alltrue;
}
else if condition_1 and 2 are TRUE
{ but condition_3 is FALSE
statements_true_1and2; statements_true_1and2
} are executed,
}
else if condition_1 is TRUE but
{ condition_2 is FALSE
statements_true_1; statements_true_1
}
are executed,
}
else
{
if condition_1 is FALSE
statements_1_false; statements_1_false are executed.
CS201 – Introduction to Computing – Sabancı University 11
}
Short-circuit Evaluation
 Some subexpressions in Boolean expressions are not evaluated if the
entire expression’s value is already known using the subexpression
evaluated so far
 Rule: Evaluate the first (leftmost) boolean subexpression. If its value is
sufficient to judge about the value of the entire expression, then stop
there. Otherwise continue evaluation towards right.
if (count != 0 && scores/count < 60)
{ cout << "low average" << endl;
}
 In this example, if the value of count is zero, then first subexpression
becomes false and the second one is not evaluated.
 In this way, we avoid “division by zero” error (that would cause to
crash the execution of the program)
 Alternative method to avoid division by zero without using short-
circuit evaluation:
if (count != 0)
{ if (scores/count < 60)
{ cout << "low average warning" << endl;
}
}
CS201 – Introduction to Computing – Sabancı University 12
Dangling Else Problem
if ( x % 2 == 0)
if ( x < 0 )
cout << x << " is an even, negative number" << endl;
else
cout << x << " is an odd number << endl;

 Do the above messages make sense?


 The problem is that it displays “odd number” for positive even
numbers and zero.
 Reason is that, although indentation says the reverse, else
belongs to second (inner) if
 Solution: use braces (see next slide)

CS201 – Introduction to Computing – Sabancı University 13


Solution to Dangling Else Problem
if ( x % 2 == 0)
{
if ( x < 0 )
cout << x << " is an even, negative number"<< endl;
}
else
{
cout << x << " is an odd number << endl;
}
 Now else belongs to first if

CS201 – Introduction to Computing – Sabancı University 14

You might also like