Break and Continue Statements

  • Continue Statement:
  •  The continue statement in the body of the loop does this
  •  All you need to do is include the keyword “continue” in the body of the loop.
  •  An advantage of the continue statement is it eliminates nesting or additional blocks of code.
  •  Can enhance the readability when the code is very long or deeply nested inside already.
  • Break Statement:
  • Break statement will help in jump out of the loop.
  •  This can be done by just mentioning the word break with a semicolon at the end.
  • Break is often used to leave the loop for the following main reasons:
  •  Break will switch the statements.
  •  Break will help in jumping out of the loop.
  •  Example:
  • while ( p > 0)
  • {
  • printf(“%d\n”, p);
  • scanf(“%d\n”, &p);
  • while ( q > o);
  • {
  • printf(“%d\n”, p*q);
  • if ( q > 0)
  • break;     \\ break the inner loop
  • scanf(“%d\n”, &q);
  • }
  • if ( q > 100)
  • break;  \\ break the outer loop
  • scanf(“%d\n”, &p);
  • }
  • #include<time.h>

Switch Statement:

  • However, many times a program needs to choose one of the several alternatives.
  •  You can do this by using if else if..else.
  • Tedious, prone to errors.
  • When the value of the expression is repeatedly used then we can use switch statement.
  • More convenient and efficient.
  • The expression which is enclosed within parenthesis is successively compared against the values: value1, value2, …., valuen.
  • Cases must be simple constants or constant expressions.
  • If a case is found whose value is equal to the value of expression, then the statement that follow the cases are executed.
  • When more than one statement is included, they do not have to be enclosed within parenthesis.
  • The break statement signals the end of a case and causes execution of the switch statement to be terminated.
  • Include the break statement at the end of every case.
  • Forgetting to do so for a case causes program execution to continue into the next case.
  • The special optional case called default is executed if the value of expression does not match any of the case values.
  • Same as “fall through” else.
  • Switch statement example:
#include<stdio.h>
int main()
{
    enum Weekdays{Monday, Tuesday, Wednesday};
    enum Weekdays today = Tuesday;
    switch(today)
    {
        case Monday:
        printf("Today as Sunday\n");
        break;
        case Tuesday:
        printf("Today as Friday\n");
        break;
        case Wednesday:
        printf("Today as anyday\n");
        break;
        
    }
    return 0;
}

In C programming language timeh (used as ctime in C++) is a header file defined in the C Standard Library that contains time and date function declarations to provide standardized access to time/date manipulation and formatting.

Scroll to top