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 elseb) Switch casec) Break,continue and goto
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");
Similar Questions
Which is the following is an example of multiple selection control structure in C Question 4Answera.SWITCH CASE b.IFc.FOR d.WHILE
when can we use switch case and if else if statements
Which of the following is true for switch statement in C++?Select one:We need to put break statement at the end of the group of statements of a conditionWe can put a range for cases such as case 1..3None of the theseIt uses labels instead of blocks
In the context of "break" and "continue" statements in C, pick the best statement.a.“break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can be used in “switch” body.b.“break” and “continue” can be used in “for”, “while”, “do-while” loop body and “switch” body.c.“continue” can be used in “for”, “while” and “do-while” loop body.d.“break” and “continue” can be used in “for”, “while” and “do-while” loop body. Besides, “continue” and “break” can be used in “switch” and “if-else” body.
Which of the following is not a valid control structure in C?if-elseforswitchselect
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.