0% found this document useful (0 votes)
36 views

Lecture 04

The document discusses if-else statements and switch statements in C programming. It covers the syntax of if-else statements, common errors like missing brackets and using else without if. It also discusses good programming practices for if-else statements. The document then explains multi-way decision making using nested if-else and switch statements with examples. It covers the syntax, flow, default case and break statement in switch. It concludes with discussion on assignment operators, increment/decrement operators and some common programming errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lecture 04

The document discusses if-else statements and switch statements in C programming. It covers the syntax of if-else statements, common errors like missing brackets and using else without if. It also discusses good programming practices for if-else statements. The document then explains multi-way decision making using nested if-else and switch statements with examples. It covers the syntax, flow, default case and break statement in switch. It concludes with discussion on assignment operators, increment/decrement operators and some common programming errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Lecture # 4

CSC103: Lecture#4
if-else statement (Revisited)
Syntax of if-else condition executing a set of
statements (i.e. block of statements)
If (condition)
{
statement-1;
statement-2;
……..
}
else
{
statement-1;
statement-2;
……..
}
Statements after if structure;
Common programming error
Missing brackets around a block: It is an
error to forget brackets around a block of if or
if-else statement
E.g. OR
If (condition) If (condition)
statement-1;
{
statement-2; statement-1;
statement-2;
Statement-2 is independent }
of the condition state (whether else
True or false). It will always statement-3;
execute. statement-4;

/* statement-4 is not attached to else


part, although it is indented. It will
always execute. */
If-else example (Missing brackets)
If-else example (Missing brackets)
If-else example (Missing brackets)
If-else example (Missing brackets)
If-else example (Missing brackets)

This program will not run!


Common programming error
Using else without an if: It is an error to use
else statement without corresponding if
statement
E.g. If (condition)
int x = 10; statement-1;
else // Error: else statement-2;
// without an if else
{ {
statement-1; statement-1;
statement-2; statement-2;
} }
Good Programming Practice
Always use if-else brackets: If the if structure
contains only one statement, then brackets are
not MUST
Similarly, if the else part of an if-else statement
contains only one statement, then brackets are
not MUST, E.g.
if(age>40)
printf(“Too old!”);
Else
printf(“You are still in!”);
Although, it is a good practice to always include
brackets (even in case of single statement)
Multi-way decision
if Statements vs if else
if ( grade ==‘A’ ) if ( grade ==‘A’ )
printf(“ Excellent ”); printf(“ Excellent ”);
if ( grade ==‘B’ ) else
printf(“ Very Good ”); if ( grade ==‘B’ )
if ( grade ==‘C’ ) printf(“ Very Good ”);
printf(“ Good ”); else
if ( grade ==‘D’ ) if ( grade ==‘C’ )
printf(“ Poor ”); printf(“ Good ”);
if ( grade ==‘F’ ) else
printf(“ Fail ”); if ( grade ==‘D’ )
printf(“ Poor ”);
else
Printf(“Fail”);
Multi if else statement
if ( grade ==‘A’ )
printf(“ Excellent ”);
else if ( grade ==‘B’ )
printf(“ Very Good ”);
else if ( grade ==‘C’ )
printf(“ Good ”);
else if ( grade ==‘D’ )
printf(“ Poor ”);
else
printf(“ Fail ”);
 Make a flowchart of Multi if-else structure…
Multi if else statement Example
switch statement
An alternate of “nested if-else”
• Provides a more convenient format of writing
multiple decisions with alternate actions
The decisions are based on a expression – say
the switch expression
• The switch expression evaluates to a value, which
is matched with different cases (choices) inside
the switch statement
• One of the case is executed depending on the
value
switch statements
switch ( variable name )
{
case 1 :
statements;
case 2 :
statements;
case 3 :
statements;

}
Flow Chart of switch statement
Switch Example-1
Switch Example-2
Switch Example-3

• At times we may want to


execute a common set of
statements (case blocks) for
multiple cases.
• Writing multiple cases for a
case block results is equivalent
to “Oring” the matches.
• i.e. for first case in our
example:
ch==‘a’ || ch==‘A’
Switch Example-4
Solution using switch cases (nested switch cases)

A company insures its drivers in the following cases:


• If the driver is married.
• If the driver is unmarried, male & above 30 years of
age.
• If the driver is unmarried, female & above 25 years of
age.

In all other cases the driver is not insured. If the marital


status, gender and age of the driver are the inputs, write
a program to determine whether the driver is to be
insured or not.
Switch Example-4
Switch Example-4
Note: The usage of if-else
statement for age range
comparison. Switch is not
suitable for such cases.
default :
default :
printf(“Invalid Choice“) ;

A default statement executes if none of the cases could


be matched with the expression
• Can be considered equivalent to an else part of a nested if-else
statement
• No break is needed in default case; it will exit the switch
statement anyway
break;
Notes about the switch statement
Note the break statement after each case
• Denotes the end of a certain case statement. Results in immediate
exit from the switch statement body
• If not used, all “subsequent” cases will also be executed
 The expression and matching constants can
have only integer or character types
 The switch statement can only check for
expression matching, not range comparison
• You have to resort back to nested if-else for decisions involving range
comparison
Expression in a switch statement
We can check the value of any expression in a
switch. Thus the following switch
statements are legal.
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
Expressions can also be used in cases provided
they are constant expressions. Thus, case 3 + 7
is correct, however, case a + b is incorrect.
Common Programming Error

It is a syntax error to provide a case in


switch statement, which involves variables
The nested if statement & switch
statement
Nested if-else statement Switch statement
1) It becomes complicated for 1) It is easy to understand for
multiple choices multiple selections
2) It uses an independent 2) It uses single expression for
expression for each case all cases, but each case
3) The tested condition can be must have a constant value
given in a specific range of of integer type or character
values. If the given condition type
matches then the statement 3) Only a single expression is
under it will be executed given in the switch
statement which returns a
single value. The test
condition cannot be given in
a specific range. It is the
major drawback of the
switch statement
Common Programming Error
It is a syntax error to specify a float type expression
in a switch statement
It is a logical error to forget a break statement in
some matching case
Assignment Operators in C
Assignment operators abbreviate assignment
expressions
c = c + 3;
• can be abbreviated as c += 3; using the addition assignment
operator
Statements of the form
variable = variable operator expression;
can be rewritten as
• variable operator= expression;
Examples of other assignment operators:
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
Assignment Operators in C
Increment and Decrement Operators
Increment operator (++)
• Can be used instead of c+=1
Decrement operator (--)
• Can be used instead of c-=1
Pre-increment
• Operator is used before the variable (++c or --c)
• Variable is changed before the expression it is in is evaluated
Post-increment
• Operator is used after the variable (c++ or c--)
• Expression executes before the variable is changed
Example: Increment & Decrement operators
Example: Pre-Post-Increment & Decrement
Increment and Decrement Operators

When variable not in an expression


Pre-incrementing and post-incrementing have the same effect
++value;
printf( “%d”, value );
Has the same effect as
value++;
printf( “%d”, value );
Increment and Decrement Operators
Good Programming Practice

Unary operators should be placed directly next


to their operands with no intervening spaces.
Common Programming Error

Attempting to use the increment or decrement


operator on an expression other than a simple
variable name is a syntax error, e.g., writing ++
(x + 1).
Error-Prevention Tip
C generally does not specify the order in which an
operator’s operands will be evaluated (although
we will see exceptions to this for a few operators
in Chapter 4).
Therefore you should avoid using statements with
increment or decrement operators in which a
particular variable being incremented or
decremented appears more than once.
Programming Problems
• Write a program which allows the user to enter a character and the
program outputs whether it is a vowel or consonant. If the character
entered is non-alphabet than the program should exit by showing an
error message “Invalid character entered!”.
• A year is called leap year if it is divisible by 4. However, according to the
Gregorian calendar if a year is divisible by 100 than it will not a leap year
(for example 1900). Moreover, a year is leap year if it is divisible by 400
(for example 2000). Write a program that enters a year and find that the
entered year is a leap year or not according to the above statement.

You might also like