16 - (3-25) C++ Control Structures
16 - (3-25) C++ Control Structures
Software Engineering I
David O. Johnson
Spring 2025
if (condition) {
statement 1;
statement 2;
statement 3;
}
if (x == 3)
; if (x != 3)
Same as
else print(“Test\n”);
print(“Test\n”);
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
switch (number) {
case 1:
case 2:
case 3:
// This line will be executed for all cases 1, 2, and 3
printf("Number is small\n");
break;
// ... other cases
}
Syntax:
while (condition) statement;
while(condition){
//code to be executed
}
Syntax:
do statement; while (condition);
• Similar to the while statement
• The do-while statement tests condition after the statement
executes.
• The statement always executes at least once.
• You can group multiple statements together with braces.
do {
//code to be executed
} while(condition)
switch(expression) {
case x:
case y:
}
• Not exactly …
• Booleans, as a distinct type, cannot be directly used in switch
statements in C.
• The switch statement in C requires an integer or character type for
its expression.
• However, since C represents booleans as integers (0 for false and
non-zero for true), you can use boolean variables or expressions
within a switch statement by treating them as integers.
Grading Level
Exceeds
Question Points Meets Expectations Unsatisfactory
Expectations
(80-89%) (0-79%)
(90-100%)
Comments on most
lines that describe
Comments on each Few comments or
what the code is
line that describe most comments do
2 50 doing, or comments
what the code is not describe what
on each line, but not
doing. code is doing.
describing what
code is doing.