Lecture 5
Lecture 5
Lecture 5
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:
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
payment ←50
payment ←40
payment ←30
payment ←20
else
payment ← 10
endif
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
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