Knowee
Questions
Features
Study Tools

Explain the various control structure with its syntax and example.a) If elseb) Switch casec) Break,continue and goto

Question

Explain the various control structure with its syntax and example.

a) If else
b) Switch case
c) Break, continue and goto

🧐 Not the exact question you are looking for?Go ask a question

Solution

a) If else: The if-else control structure is used to perform different actions based on different conditions.

Syntax:

if (condition) {
   // code to be executed if condition is true
} else {
   // code to be executed if condition is false
}

Example:

int a = 10;
if (a > 5) {
   printf("a is greater than 5");
} else {
   printf("a is not greater than 5");
}

b) Switch case: The switch case control structure is used to perform different actions based on the value of a variable or expression.

Syntax:

switch(expression) {
   case constant1:
      // code to be executed if expression equals constant1;
      break; 
   case constant2:
      // code to be executed if expression equals constant2;
      break;
   default: 
      // code to be executed if expression doesn't match any constants;
}

Example:

char grade = 'B';
switch(grade) {
   case 'A':
      printf("Excellent!");
      break;
   case 'B':
      printf("Good Job");
      break;
   default:
      printf("Invalid grade");
}

c) Break, continue and goto:

  • Break: It is used to terminate the loop or switch statement and transfer execution to the statement immediately following the loop or switch.

Syntax:

break;

Example:

for (int i = 0; i < 10; i++) {
   if (i == 5) {
      break;
   }
   printf("%d ", i);
}
  • Continue: It is used to skip the rest of the code inside the current loop iteration and immediately jump to the next iteration of the loop.

Syntax:

continue;

Example:

for (int i = 0; i < 10; i++) {
   if (i == 5) {
      continue;
   }
   printf("%d ", i);
}
  • Goto: It is used to transfer control to the labeled statement in the program.

Syntax:

goto label;
...
label: statement;

Example:

int a = 10;
if (a > 5) {
   goto jump;
}
printf("a is not greater than 5");
jump:
printf("a is greater than 5");

This problem has been solved

Similar Questions

Which of the following is not a valid control structure in C?if-elseforswitchselect

Which is the following is an example of multiple selection control structure in C Question 4Answera.SWITCH CASE b.IFc.FOR d.WHILE

In a switch statement, which keyword is used to execute a block of code if none of the cases match the specified value?Optionsdefaultelsebreakcontinue

Which of the following is used with the switch statement?Optionswhilecontinuebreakdo

which is used to encounter inside any loop, control automatically passes to the first statement after loop?Optionsbreakcontinuepassgoto

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.