Lecture 5

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

Fundamentals of Software Development

Lecture 5
Previous Lectures
• C Basics
Decision Making
Decision Making
• Decision making is the process of choosing one option
out of several available options given that a specified
condition is satisfied.
• Using logical operators, the programmer specifies one or
more conditions to be assessed by the program.
• Enclosed within the decision making structure, a
statement or statements to be executed if the condition is
determined to be true.
• Not mandatory but most likely happens that other decision
making statements are executed if the condition is
determined to be false may also exist.
• A flowchart of the general form for a type decision making
structure found in most programming languages.
Decision Making
• In C language, the compiler
always assumes any non-null or
non-zero values to be true.
• However, If the value is null or
zero then it assumes false.
• C programming language offers
5 decision making structures or
types.
If Statement
If Statement
• An if statement consists of a logical expression followed by one or more
statements.
• The syntax of an 'if' statement in C programming language is:

if(logical_expression) {
/* statement(s) will execute if the logical expression is true */
}

• If the logical operation evaluates to true, the block of code inside the if
statement will be executed.
• If the logical operation evaluates to false, the first set of code lines after the if
statement(after the curly brackets) will be executed.
If Statement
Interpretation: If the condition evaluates to true,
then conditional code is executed, otherwise, it
is skipped.
Example 1: If statement
"Open Sesame" is a magical phrase in the story of "Ali Baba and the Forty Thieves" the
book One Thousand and One Nights (Arabic magical tales book). The phrase opens the
mouth of a cave in which forty thieves have hidden a stolen treasure. Sesame is derived
from the plant name.
Example 2: If statement
An odd number is an integer which is not multiple of two and its reminder is 1
if divided by 2! Let's try to code this in C by feeding some numbers into our
program below.
Question:

What is wrong with example 1 and 2?


If Else Statement
If…Else Statement
• An if statement can be followed by an else statement to be executed
in case the logical operation is false.
• The syntax of an if...else statement in C programming language is:
if(logical_expression) {
/* statement(s) will execute if the logical expression is true */
}
else {
/* statement(s) will execute if the logical expression is false */
}
• If the logical expression evaluates to true, then the if statement is executed.
Otherwise, the else statement is executed.
• Remember: C programming language assumes any non-zero and non-null
values as true, and if it is either zero or null, then it is assumed as false value.
If…Else Statement

Interpretation: If the condition evaluates to


true, then if code is executed, otherwise, if
code is skipped and else code is executed.
If….Else Statement

If (gross > 100.0)


net= gross-tax
else
net = gross
Example 3: If…Else
Example 4: If…Else
If…Else If…Else
Statement
If…Else If…Else Statement
• For testing various conditions, an if statement can be also followed by
else if.. else statement.
• The if… else if.. else statement is useful for tackling complex decision
makings.
• When using if...else if..else statements, there are few points to keep
in mind:
• An if statement may or may not have an else statement. However,
if it does, then it must come after any else if.
• An if statement may have zero or many else-ifs. However, if it
does, the else statement must come after all the else-ifs.
• In case there are many else ifs, once a logical expression inside
one these else ifs evaluated to true, the others are skipped
(including else statement)
If…Else If…Else Statement
• The syntax of an if...else if...else statement in C programming language is −
if(logical_expression 1) {
/* Executes when the logical expression 1 is true */
}
else if( logical_expression 2) {
/* Executes when the logical expression 2 is true */
}
else if( logical_expression 3) {
/* Executes when the logical expression 3 is true */
}
else{
/* executes when the none of the above condition is true */
}
Example 5: If…else-if
Nested If Statements
Nested If Statements

• In c language, it is legal to have nested if statements, which means you can


use one if statement inside another if statement.
• The syntax for a nested if statement is as follows −
if( logical_expression 1) {
/* Executes when the logical expression 1
is true */ if(logical_expression 2) {
/* Executes when the logical expression 2 is true */
}
}
• You can nest else if...else in the similar way as you have nested if
statements.
Nested If / else if
Statements
Example 6: Nested if

Outer
if Inner
if
Example 7:
Nested if and else-if

inner if
Outer if /else
/else if

inner if
/else if
Tutorial 1: Bonus system
Tutorial 1: Bonus system
Step 1: START

Step 2: Input name, overtime, absent

Step 3: if (overtime – (2/3)*absent > 40) then

payment ←50

else if (overtime – (2/3)*absent > 30) then

payment ←40

else if (overtime – (2/3)*absent > 20) then

payment ←30

else if (OVERTIME–(2/3)*ABSENT > 10) then

payment ←20

else

payment ← 10

endif

Step 4: Print “Bonus for”, name “is”, payment

Step 5: END
Tutorial 1: Bonus system
Switch Statement
Switch Statement
• A switch statement allows a selected variable to be assessed for equality against a
set of values
• In switch statement, each tested value is considered as a case.
• The variable being tested is check for each of these switch cases.
• The syntax for a switch statement in C programming language is as follows:
switch(selected_variable) {
case constant-expression :

statement(s);
break; /* optional */
case constant-expression :

statement(s);
break; /* optional */
/* you can have any number of case statements */
default :
/* Optional */
statement(s);
}
Switch Statement:
Seven Rules!!
1. The expression used in switch must be integral type ( int, char and
enum).
2. A switch statement can have any number of case statements. However,
each case must be followed by the value to be compared and a colon (
not semi-colon)
3. The constant-expression must be of the same data type as the
switched on variable and must be a constant not a variable.
4. When variable matches a case, the statements following that
particular case will be executed until a break statement is found.
5. Once the compiler reaches the break statement, switch terminated
and the next line following the switch statement will be executed.
6. Not every case needs to contain a break.
7. A switch statement can have an optional default case, which must
appear at the end of the switch. This is usually used when none of
the cases are matched. However, A default case does not need a
break.
Example 8:Switch
Statement

Try writing this


code using a if-elseif
on your own!!
Nested Switch Statements
Nested Switch Statements
• It is possible to have a switch as a part of the statement sequence of an outer
switch. Even if the case constants of the inner and outer switch contain
common values, no conflicts will arise.
• The syntax for a nested switch statement is as follows:
Example 9:Nested Switch

Outer
switch
Inner
switch
If-elseif and Switch Statement
• Flow Diagram:
Advantages of switch over if-else ladder
• A switch statement execute much faster than if-else ladder.
• The compiler generates a jump table for a switch during compilation process.
During execution, instead of checking which case is satisfied, it only decides which
case has to be executed.
• Switch is more readable in comparison to if-else statements.
• It is also more manageable for having higher level of indentation than if.
(less brackets to worry about!)
Importance of Indentation
• Indentation is one of the most important stylistic aspect of programming.
• It makes the code easier to:
• Modify and improve.
• Maintain and track changes.
• Read and understand the flow.
• It saves lots of time when we are revisiting the code base.
• Check: https://www2.cs.arizona.edu/~mccann/indent_c.html
Next week
Loops

You might also like