Introduction to Conditional Statements
• Conditional statements control the flow of a
program based on conditions.
What are Conditional Statements?
• They execute code blocks only if certain
conditions are met.
Types of Conditional Statements
• 1. if Statement; 2. if-else Statement; 3. Nested
if; 4. switch Statement
The if Statement
• The simplest form of conditional statement.
Syntax of if Statement
• Syntax: if (condition) { // code }
Example of if Statement
• Example: if (a > b) { printf("A is greater"); }
The if-else Statement
• Used when there are two possible outcomes.
Syntax of if-else Statement
• Syntax: if (condition) { // code if true } else { //
code if false }
Example of if-else Statement
• Example: if (a > b) { printf("A is greater"); } else
{ printf("B is greater or equal"); }
The Nested if Statement
• An if statement within another if statement.
The switch Statement
• Used for multiple conditions with constant
values.
Syntax of switch Statement
• Syntax: switch (variable) { case value1: // code
break; case value2: // code break; default: //
code }
Example of switch Statement
• Example: switch (choice) { case 1:
printf("Option 1"); break; case 2:
printf("Option 2"); break; default:
printf("Invalid choice"); }
Summary and Tips
• 1. Always use braces for clarity; 2. Use switch
for multiple constant conditions; 3. Avoid
deeply nested if statements.

Introduction to Conditional Statements.pptx

  • 1.
    Introduction to ConditionalStatements • Conditional statements control the flow of a program based on conditions.
  • 2.
    What are ConditionalStatements? • They execute code blocks only if certain conditions are met.
  • 3.
    Types of ConditionalStatements • 1. if Statement; 2. if-else Statement; 3. Nested if; 4. switch Statement
  • 4.
    The if Statement •The simplest form of conditional statement.
  • 5.
    Syntax of ifStatement • Syntax: if (condition) { // code }
  • 6.
    Example of ifStatement • Example: if (a > b) { printf("A is greater"); }
  • 7.
    The if-else Statement •Used when there are two possible outcomes.
  • 8.
    Syntax of if-elseStatement • Syntax: if (condition) { // code if true } else { // code if false }
  • 9.
    Example of if-elseStatement • Example: if (a > b) { printf("A is greater"); } else { printf("B is greater or equal"); }
  • 10.
    The Nested ifStatement • An if statement within another if statement.
  • 11.
    The switch Statement •Used for multiple conditions with constant values.
  • 12.
    Syntax of switchStatement • Syntax: switch (variable) { case value1: // code break; case value2: // code break; default: // code }
  • 13.
    Example of switchStatement • Example: switch (choice) { case 1: printf("Option 1"); break; case 2: printf("Option 2"); break; default: printf("Invalid choice"); }
  • 14.
    Summary and Tips •1. Always use braces for clarity; 2. Use switch for multiple constant conditions; 3. Avoid deeply nested if statements.