HD ES085 - Topic - 1 - C - Conditional - Control - Structures
HD ES085 - Topic - 1 - C - Conditional - Control - Structures
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
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
check the condition gender != 'M' inside the outer if block. If the condition is false, it
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
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
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
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
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
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
handle.
Syntax of a Switch Statement
The expression is evaluated, and its value is
the case constants, the code block within the default label is
IF – ELSE STATEMENTS
That’s All!
To be Continued…
Up Next
C Iterative Control Structures