0% found this document useful (0 votes)
2 views16 pages

Control Statements, Conditional Operator

Chapter 2 covers control statements in C programming, focusing on if-else statements, nested if-else, and the else-if ladder for handling multiple conditions. It explains the syntax and execution flow of these statements, along with the use of relational and Boolean operators. Additionally, the chapter introduces the conditional operator as a shorthand for simple if-else conditions.

Uploaded by

sanjamsiam2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
2 views16 pages

Control Statements, Conditional Operator

Chapter 2 covers control statements in C programming, focusing on if-else statements, nested if-else, and the else-if ladder for handling multiple conditions. It explains the syntax and execution flow of these statements, along with the use of relational and Boolean operators. Additionally, the chapter introduces the conditional operator as a shorthand for simple if-else conditions.

Uploaded by

sanjamsiam2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 16

Chapter 2

Control Statements, Conditional Operator


Control Statements:
The if-else Statement,
if ladder, nested if-else
Basic syntax of if-else statement Relational operators

If (condition)
{
Statements;
}else{
Statements;
}

condition is a Boolean expression written with


only relational operators or Boolean operator or
combination of both. Booleanoperators
C – If statement
● The statements inside the body of “if” only execute -> if the given condition
returns true.
● If the condition returns false then the statements inside “if” are skipped.

if(condition)
{
// Statements to execute if
// condition is true
}
C If else statement
● We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
1. #include <stdio.h>
2. int main()
3. {
4. int age;
5. printf("Enter your age?");
6. scanf("%d",&age);
7. if(age>=18)
8. {
9. printf("You are eligible to
vote...");
10. }
11. else
12. {
13. printf("Sorry ... you can't
vote");
14. }
15. }
C Nested If..else statement
When an if else statement is present inside the body of another “if” or “else” then this is
called nested if else.

if(condition) {
//Nested if else inside the
body of "if"
if(condition2) {
//Statements inside the
body of nested "if"
}
else {
//Statements inside the
body of nested "else"
}
}
else {
//Statements inside the body
of "else"
}
C – else..if statement /if-else-if ladder
The else..if statement is useful when you need to check multiple conditions within the
program.
● As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the C else-if ladder is bypassed.
● If none of the conditions are true, then the final else statement will be executed.

Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Conditional Operator
conditional statements are the decision-making statements which
depends upon the output of the expression.

● It is the one and only ternary operator in the C programming


language
● It can be used as an alternative for if-else condition if the 'if
else' has only one statement each
The conditional operator takes an expression and executes the first
statement if the expression evaluates to be true, and the second
statement if the expression evaluates to be false.

expression ? statement1 : statement2


expression ? statement1 : statement2

● The expression will be treated as a logical condition

● any non 0 value will be considered as true, and 0 as false.

● The statement1 and statement2 can be a statement, expression, variable,


or a constant.
● One of the statements will get executed based on the result obtained from
the evaluation of the given expression.
The condition is evaluated first and the result of the condition is implicitly converted to boolean.

Another form:
variable = condition ? value1: value2

You might also like