Conditional Statements - Java

Conditional Statements


  • If
    •   if (condition) {statement;}
      
  • If Else
    •   if (condition) {statement;}
        else {statement;}
      
  • If Else if Else
    •   if (condition) {statement;}
        else if (condition) {statement;}
        else {statement;}
      
  • Switch => Expression can only be a Constant
    •   switch (val) {
          case 1:
            statement;
            break;
      
          case 2:
            statement;
            break;
      
          default:
            statement;
        }
      
  • Break => Gets you out of current loop
    • break
  • Continue => Skips to next Iteration
    • continue

Loops


  • For
    •   for (int i = 0; i < 100; i++) {
          statement;
        }
      
    • Loop through an Array var1
        for (int var : var1) {
          statement;
        }
      
    • for (initialization; condition; updating) {}
    • All of the above 3 are optional, Also there can be multiple Variables in each
    • Flow
      • Goes Initialization
      • Check Condition, If True then go to Body
      • Goes to updating then again does previous step, This continues till Condition becomes false
  • While
    • while (condition) {statement;}
  • Do While
    •   do {statement;} while (condition);
      
Share: