Class 6 Exam  >  Class 6 Notes  >  C Programming for Beginners  >  C Switch & While Loop & For Loop

C Switch & While Loop & For Loop | C Programming for Beginners - Class 6 PDF Download

Switch Statement

  • Instead of writing many if..else statements, you can use the switch statement.
  • The switch statement selects one of many code blocks to be executed:

Syntax

switch(expression) {

  case x:

    // code block

    break;

  case y:

    // code block

    break;

  default:

    // code block

}

This is how it works:

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break statement breaks out of the switch block and stops the execution
  • The default statement is optional, and specifies some code to run if there is no case match

The example below uses the weekday number to calculate the weekday name:

Example

int day = 4;


switch (day) {

  case 1:

    printf("Monday");

    break;

  case 2:

    printf("Tuesday");

    break;

  case 3:

    printf("Wednesday");

    break;

  case 4:

    printf("Thursday");

    break;

  case 5:

    printf("Friday");

    break;

  case 6:

    printf("Saturday");

    break;

  case 7:

    printf("Sunday");

    break;

}


// Outputs "Thursday" (day 4)

The break Keyword

  • When C reaches a break keyword, it breaks out of the switch block.
  • This will stop the execution of more code and case testing inside the block.
  • When a match is found, and the job is done, it's time for a break. There is no need for more testing.

A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.

The default Keyword

The default keyword specifies some code to run if there is no case match:

Example

int day = 4;


switch (day) {

  case 6:

    printf("Today is Saturday");

    break;

  case 7:

    printf("Today is Sunday");

    break;

  default:

    printf("Looking forward to the Weekend");

}


// Outputs "Looking forward to the Weekend"

Note: The default keyword must be used as the last statement in the switch, and it does not need a break.

C While Loop

Loops

  • Loops can execute a block of code as long as a specified condition is reached.
  • Loops are handy because they save time, reduce errors, and they make code more readable.

While Loop

The while loop loops through a block of code as long as a specified condition is true:

Syntax

while (condition) {

  // code block to be executed

}

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

Example

int i = 0;


while (i < 5) {

  printf("%d\n", i);

  i++;

}

Note: Do not forget to increase the variable used in the condition (i++), otherwise the loop will never end!

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

do {

  // code block to be executed

}

while (condition);

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Example

int i = 0;


do {

  printf("%d\n", i);

  i++;

}

while (i < 5);

Do not forget to increase the variable used in the condition, otherwise the loop will never end!

C For Loop

For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) {

  // code block to be executed

}

  • Statement 1 is executed (one time) before the execution of the code block.
  • Statement 2 defines the condition for executing the code block.
  • Statement 3 is executed (every time) after the code block has been executed.

The example below will print the numbers 0 to 4:

Example

int i;


for (i = 0; i < 5; i++) {

  printf("%d\n", i);

}

Example explained

  • Statement 1 sets a variable before the loop starts (int i = 0).
  • Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.
  • Statement 3 increases a value (i++) each time the code block in the loop has been executed.

Another Example

This example will only print even values between 0 and 10:

Example

for (i = 0; i <= 10; i = i + 2) {

  printf("%d\n", i);

}

The document C Switch & While Loop & For Loop | C Programming for Beginners - Class 6 is a part of the Class 6 Course C Programming for Beginners.
All you need of Class 6 at this link: Class 6
10 videos|13 docs|15 tests

Top Courses for Class 6

10 videos|13 docs|15 tests
Download as PDF
Explore Courses for Class 6 exam

Top Courses for Class 6

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

shortcuts and tricks

,

C Switch & While Loop & For Loop | C Programming for Beginners - Class 6

,

Important questions

,

MCQs

,

Sample Paper

,

study material

,

Objective type Questions

,

Free

,

C Switch & While Loop & For Loop | C Programming for Beginners - Class 6

,

Previous Year Questions with Solutions

,

Extra Questions

,

pdf

,

practice quizzes

,

Semester Notes

,

Viva Questions

,

Summary

,

past year papers

,

ppt

,

Exam

,

mock tests for examination

,

video lectures

,

C Switch & While Loop & For Loop | C Programming for Beginners - Class 6

;