Understanding control flow statements like conditional statements, loops, break/continue, and exception handling is essential for writing clean and efficient C# code. This guide will walk you through each concept.
if statement in C#?
if(condition) { //code }if { condition; }if (condition) { code } elseif: condition { code }int x = 5; if (x > 3) { Console.WriteLine("Greater"); } else { Console.WriteLine("Smaller"); }
if-else statement in C#?
if (condition) { //block } else { //block }if condition { //block } else { }if (condition) { } elseelse { } if (condition) { }switch statement do in C#?
switch statement, if no case matches, which statement is executed?
switch statement in C#?
case value: { //code }case value; { //code }case value() { //code }switch(value) { //cases }switch block early in C#?
else statements can you use in an if-else block?
default keyword in a switch statement?
switch statement?
break in a case blockif inside a case blockdefault caseforwhiledo-whileforeachforforeachwhiledo-whilefor loop in C#?
for (initialization; condition; increment) { }for (initialization) { condition } { increment }for (condition) { initialization; increment; }for { initialization; condition; increment }while loop in C# work?
truedo-while loop in C#?
for loop do?csharpCopy codefor (int i = 0; i < 5; i++) { Console.WriteLine(i); }
5 onlyforeach loop in C#?
forforeachrepeatwhileexitstopreturnbreakint x = 0; while (x < 3) { Console.WriteLine(x); x++; }
break statement in C#?
exitcontinuepausereturnbreak statement be used in a switch statement in C#?
return statement must be usedbreak is not allowed in switch statementscontinue statement is encountered in a for loop?
break and continue differ in their functionality in C#?
break exits the loop, while continue skips the current iterationbreak skips the iteration, while continue exits the loopbreak exits the program, while continue skips the loopbreak statement be used in?
continue inside the loopbreak after a certain number of iterationsreturn inside the loopfor (int i = 0; i < 5; i++) { if (i == 3) break; Console.WriteLine(i); }
continue?
continue if the number is evencontinue if the number is oddbreak for even numberscontinue for odd numbersbreak statement in a loop?
| Qno | Answer |
|---|---|
| 1 | A) if(condition) { //code } |
| 2 | A) Greater |
| 3 | A) if (condition) { //block } else { //block } |
| 4 | A) Executes one block of code out of multiple choices based on an expression |
| 5 | A) Default |
| 6 | A) case value: { //code } |
| 7 | C) Break |
| 8 | A) Only one |
| 9 | A) To handle unexpected input or cases |
| 10 | D) Using a non-constant value in a case |
| 11 | C) do-while |
| 12 | B) foreach |
| 13 | A) for (initialization; condition; increment) { } |
| 14 | A) It runs as long as the condition evaluates to true |
| 15 | B) The loop runs at least once regardless of the condition |
| 16 | C) Print numbers from 0 to 4 |
| 17 | A) It is used to loop through arrays or collections |
| 18 | C) repeat |
| 19 | C) break |
| 20 | B) 0, 1, 2 |
| 21 | A) Exits the loop or switch statement immediately |
| 22 | B) continue |
| 23 | A) Yes, to exit the switch case |
| 24 | B) The loop skips the current iteration and continues with the next one |
| 25 | A) break exits the loop, while continue skips the current iteration |
| 26 | C) Loops and switch statements |
| 27 | D) Both B and C |
| 28 | B) Prints 0, 1, 2 |
| 29 | A) Use continue if the number is even |
| 30 | B) When you want to exit the loop after a certain condition is met |