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

HD ES085 - Topic - 1 - C - Conditional - Control - Structures

This document discusses various conditional control structures in C programming including if statements, if-else statements, nested if statements, else if statements, switch statements, and nested decisions. It provides syntax examples and explanations of how each structure allows programmers to control program flow based on different conditions. Key points covered include using if statements to execute code blocks conditionally, if-else and else if statements to provide alternative code paths, switch statements to handle multiple values in a readable way, and nesting control structures to create complex decision-making logic.

Uploaded by

lalla.lilli026
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

HD ES085 - Topic - 1 - C - Conditional - Control - Structures

This document discusses various conditional control structures in C programming including if statements, if-else statements, nested if statements, else if statements, switch statements, and nested decisions. It provides syntax examples and explanations of how each structure allows programmers to control program flow based on different conditions. Key points covered include using if statements to execute code blocks conditionally, if-else and else if statements to provide alternative code paths, switch statements to handle multiple values in a readable way, and nesting control structures to create complex decision-making logic.

Uploaded by

lalla.lilli026
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

ES085

FIRST SEMESTER SY 2023 – 2024

Topic 1: C Conditional Control Structures


If Statements
If Statements

Is a fundamental control structure in programming that allows

you to control the flow of your program based on specified

conditions. It provides a way to execute certain blocks of code

only when a given condition is true.


Syntax of an If Statement

The condition is an expression that evaluates to either true or false. If the condition

is true, the code block enclosed within the curly braces {} following the if statement

will be executed. If the condition is false, the code block will be skipped, and the

program will continue to the next statement after the if block.


Example of an If Statement

In this example, the condition age >= 18 is evaluated. If the value of age is greater

than or equal to 18, the message "You are an adult" will be printed to the console.
Nested If Statements

You can also nest if statements within other if statements to

create more complex decision-making structures. This allows

you to test multiple conditions and execute different blocks of

code based on those conditions.


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."


If…else Statements
If…else Statements

The if...else statement is a powerful control structure in C

programming that allows you to execute different code

blocks based on specified conditions. It provides a way to

control the flow of your program by providing alternative

paths of execution.
Syntax of an If…else Statement

The condition is an expression that evaluates to either true or false. If the condition

is true, the code block enclosed within the first set of curly braces {} after the if

statement will be executed. If the condition is false, the code block enclosed within

the second set of curly braces {} after the else statement will be executed.
Example of an If – else Statement

In this example, the condition num % 2 == 0 is evaluated. If the value of num is

divisible by 2 and has a remainder of 0, the message "The number is even" will be

printed to the console. Otherwise, the message "The number is odd" will be printed.
Else if Statements

The if...else statement 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.


Example of Else if Statements

In this example, the program checks the value of num. If num is greater than 0, the message

"The number is positive" will be printed. If num is less than 0, the message "The number is

negative" will be printed. Otherwise, if both conditions are false, the message "The number

is zero" will be printed.


Nested If…else Statements

You can also nest if...else statements within other if...else

statements to create more complex decision-making

structures. This allows you to test multiple conditions and

execute different code blocks based on those conditions.


In this example, the inner if...else statement is nested within the outer if statement. If

num is greater than 0, the program checks whether it is divisible by 2 to determine if it is

even or odd. If num is less than or equal to 0, the message "The number is non-positive"

will be printed.
If…else if…else
Statements
If…else if…else Statements

The if...else if...else statement is a powerful control structure

in C programming that allows you to test multiple

conditions and execute different code blocks based on

those conditions. It provides a way to create complex

decision-making structures by combining multiple if and

else if clauses.
Syntax of an If…else if…else Statement

Each condition is an expression that evaluates to either true or false. The if clause checks the first

condition, and if it is true, the corresponding code block is executed. If the first condition is false, the

program moves to the next else if clause and evaluates its condition. This process continues until a

condition is found to be true, in which case the corresponding code block is executed. If none of the

conditions are true, the code block within the else clause is executed.
Example of an If…else if…else Statement

In this example, the program checks the value of num and executes the corresponding code block

based on the condition. If num is greater than 0, the message "The number is positive" will be printed.

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. If none of these conditions are met, the message

"Invalid number" will be printed.


Nested If…else if…else Statements

The if...else if...else statement can also be nested within other if

or else clauses to create nested decision-making structures.

This allows you to test multiple conditions and execute

different code blocks based on those conditions within each

level of nesting.
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. This technique enables you to

handle complex decision scenarios where multiple

conditions need to be evaluated sequentially. Nested

decisions provide greater flexibility and granularity when

designing your program's logic.


Syntax of a Nested Decision
Examples of a Nested Decision
Switch Statements
Switch Statements

A control structure that allows you to execute different code

blocks based on the value of a variable or expression. It

provides an alternative to using multiple if...else if...else

statements when you have a large number of possible cases to

handle.
Syntax of a Switch Statement
The expression is evaluated, and its value is

compared to the constants specified in each case

label. If the value matches a case constant, the

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).


Example of a Switch Statement

In this example, the day variable is evaluated, and the

corresponding code block is executed based on its value. If day is 1,

the message "Monday" will be printed. If day is 2, the message

"Tuesday" will be printed, and so on. If day doesn't match any of

the case constants, the code block within the default label is

executed, and the message "Invalid day" will be printed.


An IF addresses multiple conditions
A SWITCH addresses multiple values

IF – One value, Multiple Conditions

SWITCH – One Condition, Multiple Values


Example 1
You are to guess my favorite number which lies between the values 1 – 5.

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 are best used when scenarios are indistinct.
Example: Checking the value of x from 1 - 100.

Switch statements are best used when scenarios are discrete.


Example: Checking whether a person is Male or Female.
Knowledge Check!
Identify whether it is best to have it on
if-else statement of switch-case.
Subject Grade Assessment
You are to check if the student got A, B, C, or Fail in his/her subject given a grade input.

IF – ELSE STATEMENTS
That’s All!
To be Continued…

Up Next
C Iterative Control Structures

You might also like