27

1. Conditional Statements

if-else

  • Used to execute code based on conditions.
  • Provides alternative paths based on different conditions.
  • Example:
  • if (condition) { … } else if (anotherCondition) { … } else { … }

switch

  • Provides a way to execute different actions based on the value of a variable.
  • Offers a cleaner alternative to nested if-else statements for multiple conditions.
  • Example:
  • switch (variable) { case value1: … break; case value2: … break; default: … break; }

2. Loops

for Loop

  • Iterates a statement or a block of statements repeatedly until a specified condition is met.
  • Typically used when the number of iterations is known.
  • Example:
  • for (int i = 0; i < 5; i++) { … }

while Loop

  • Repeats a statement or a block of statements while a given condition is true.
  • Example:
  • while (condition) { … }

do-while Loop

  • Executes a block of statements repeatedly until a specified condition becomes false.
  • Example:
  • do { … } while (condition);

foreach Loop

  • Iterates over a collection of items such as arrays or collections.
  • Simplifies iteration without explicitly initializing a counter variable.
  • Example:
  • foreach (type variable in collection) { … }

Write A Comment