You are on page 1of 4

ES084: C CONDITIONAL CONTROL STRUCTURES SECOND SEM AY 23-24

C CONDITIONAL CONTROL STRUCTURES IF…ELSE STATEMENTS

IF STATEMENTS If…else Statements


 A powerful control
If Statements structure in C
 Is a fundamental control programming that allows
structure in programming that you to execute different
allows you to control the flow of code blocks based on
your program based on specified conditions.
specified conditions  It provides a way to
 Provides a way to execute control the flow of your
certain blocks of code only when program by providing
a given condition is true alternative paths of execution.

 Syntax of an If Statement  Syntax of an If…else Statement

 The condition is an expression that


 The condition is an expression that
evaluates to either true or false.
evaluates to either true or false.
 If the condition is true, the code block
 If the condition is true, the code block
enclosed within the curly braces {} following
enclosed within the first set of curly braces
the if statement will be executed.
{} after the if statement will be executed.
 If the condition is false, the code block will
 If the condition is false, the code block
be skipped, and the program will continue
enclosed within the second set of curly
to the next statement after the if block.
braces {} after the else statement will be
executed.
 Example of an If Statement
 Example of an If – else Statement

 In this example, the condition age >= 18 is


evaluated.
 If the value of age is greater than or equal  In this example, the condition num % 2 == 0
to 18, the message "You are an adult" will is evaluated.
be printed to the console.  If the value of num is divisible by 2 and has
a remainder of 0, the message "The
Nested If Statements number is even" will be printed to the
 You can also nest if statements within other if console.
statements to create more complex decision-  Otherwise, the message "The number is
making structures. odd" will be printed.
 This allows you to test multiple conditions and
execute different blocks of code based on those Else if Statements
conditions.  Can be extended to include multiple conditions
using the else if clause
 This allows you to test additional conditions and
execute different code blocks accordingly.

 In this example, instead of using the else keyword,


we use an additional if statement to check the
condition gender != 'M' inside the outer if block.
 If the condition is false, it prints the message "You
are a female adult."
 Example of Else if Statements  Syntax of an If…else if…else Statement

 In this example, the program checks the  Each condition is an expression that
value of num. evaluates to either true or false.
 If num is greater than 0, the message "The  The if clause checks the first condition, and
number is positive" will be printed. if it is true, the corresponding code block is
 If num is less than 0, the message "The executed.
number is negative" will be printed.  If the first condition is false, the program
 Otherwise, if both conditions are false, the moves to the next else if clause and
message "The number is zero" will be evaluates its condition.
printed.  This process continues until a condition is
found to be true, in which case the
Nested If…else Statements corresponding code block is executed.
 You can also nest if...else statements within other  If none of the conditions are true, the code
if...else statements to create more complex block within the else clause is executed.
decision-making structures.
 This allows you to test multiple conditions and  Example of an If…else if…else Statement
execute different code blocks based on those
conditions.

 In this example, the program checks the


value of num and executes the
corresponding code block based on the
 In this example, the inner if...else statement is condition.
nested within the outer if statement.  If num is greater than 0, the message "The
 If num is greater than 0, the program checks number is positive" will be printed.
whether it is divisible by 2 to determine if it is even  If num is less than 0, the message "The
or odd. number is negative" will be printed.
 If num is less than or equal to 0, the message  If num is equal to 0, the message "The
"The number is non-positive" will be printed. number is zero" will be printed.
 If none of these conditions are met, the
message "Invalid number" will be printed.
IF…ELSE IF…ELSE STATEMENTS
If…else if…else Statements Nested If…else if…else Statements
 It is a powerful control  The if...else if...else statement can also be nested
structure in C within other if or else clauses to create nested
programming that decision-making structures.
allows you to test  This allows you to test multiple conditions and
multiple conditions execute different code blocks based on those
and execute different conditions within each level of nesting.
code blocks based on
those conditions.
 It provides a way to
create complex
decision-making
structures by
combining multiple if
and else if clauses.  In this example, the outer if statement checks if
num is greater than 0.
 If it is, the program enters the nested if...else
statement to check whether num is even or odd.
 If num is divisible by 2, it is even; otherwise, it is
odd.
 If num is less than 0, the message "The number
is negative" will be printed.
 If num is equal to 0, the message "The number is
zero" will be printed.

NESTED DECISIONS
Nested Decisions
 The practice of placing one or more if...else
statements inside another if or else block.
 Syntax of a Switch Statement
 This technique enables you to handle complex
 The expression is evaluated, and its value
decision scenarios where multiple conditions
is compared to the constants specified in
need to be evaluated sequentially.
each case label.
 These provide greater flexibility and granularity
 If the value matches a case constant, the
when designing your program's logic.
corresponding code block is executed.
 The break statement is used to exit the
switch statement after executing the
corresponding code block.
 If the value doesn't match any case
constant, the code block within the default
label is executed (optional).

 Syntax of a Nested Decision

 Example of a Switch Statement

 Examples of a Nested Decision

 In this example, the day variable is


evaluated, and the corresponding code
SWITCH STATEMENTS block is executed based on its value.
Switch Statements  If day is 1, the message "Monday" will be
 A control structure that allows you to execute printed.
different code blocks based on the value of a  If day is 2, the message "Tuesday" will be
variable or expression. printed, and so on.
 It provides an alternative to using multiple if...else  If day doesn't match any of the case
if...else statements when you have a large number constants, the code block within the default
of possible cases to handle.
label is executed, and the message Subject Grade Assessment
"Invalid day" will be printed.  You are to check if the student got A, B, C, or Fail
in his/her subject given a grade input.
IF VS. SWITCH  IF – ELSE STATEMENTS

IF SWITCH

 Addresses multiple  Addresses multiple


conditions values
 One Condition,
 One Value,
Multiple Conditions Multiple Values

Example 1
 You are to guess my favorite number which lies
between the values 1 – 5.
o Note: My favorite number is 3.
 IF–ELSE – Evaluates my guess if it is equal to 3.
 SWITCH – Evaluates my guess for each values of
1 – 5; and returns true if it is equal to 3.

Example 2
 You are to check if the input field for birth gender is
Male or Female
 IF–ELSE – Evaluates my input if it is Male or
Female.
 SWITCH – Checks for value of gender.

If-else Statement Switch Statement


 Best used when
 Best used when scenarios are discrete
scenarios are indistinct o Ex. Checking whether a
o Ex. Checking the value person is Male or
of x from 1 - 100 Female

You might also like