0% found this document useful (0 votes)
6 views40 pages

16 - (3-25) C++ Control Structures

The document outlines the syllabus and key concepts for EECS 348, focusing on C++ control structures including sequence, selection, and repetition. It provides syntax and examples for various control statements such as if, if-else, switch, while, do-while, and for loops, along with explanations of their usage and common pitfalls. Additionally, it includes reminders for assignment deadlines and an in-class problem rubric.

Uploaded by

sakthixs01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views40 pages

16 - (3-25) C++ Control Structures

The document outlines the syllabus and key concepts for EECS 348, focusing on C++ control structures including sequence, selection, and repetition. It provides syntax and examples for various control statements such as if, if-else, switch, while, do-while, and for loops, along with explanations of their usage and common pitfalls. Additionally, it includes reminders for assignment deadlines and an in-class problem rubric.

Uploaded by

sakthixs01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

EECS 348

Software Engineering I
David O. Johnson
Spring 2025

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 1


Reminders
• Assignment 5 due: 11:59 PM, Thursday, March 27
• Assignment 6 due: 11:59 PM, Thursday, April 10

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 2


Any Questions?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 3


In-Class Problem Solution
• 15-(3-13) In-Class Problem Solution.pptx

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 4


Any Questions?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 5


Structured Programming

• Structured programming produces programs that are easier


than unstructured programs to understand, test, debug,
modify, and even prove correct in a mathematical sense.
• Prior to C, languages were unstructured with a lot of “go to”
statements.
• After almost 50 years, structured programming is “just the way
you program” now!

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 6


Structured Programming
Control Structures
Sequence
Structured programming control structures, which
originated from C, are common in most modern-day Selection
programming languages. • if-then
• if-then-else
• switch
• Today we will learn the syntax for these control
structures in C++. Repetition
• Although we didn’t talk about them in C, they are • while
also in C, with the same syntax • do-while
• for loop

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 7


Structured Programming - Sequence

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 8


Structured Programming - Selection

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 9


Structured Programming - Repetition

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 10


Any Questions?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 11


Structured Programming - Selection

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 12


The if Statement (Same as C)
Syntax:
if (condition) statement;

• If the condition is true (not zero), the statement is executed.


• If the condition is false (zero), it is not executed.
• You can group multiple statements together with braces.

if (condition) {
statement 1;
statement 2;
statement 3;
}

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 13


The if-else Statement (Same as C)
Syntax:
if (condition) statement1; else statement2;

• If the condition is true, statement1 will be executed


• Otherwise (else), statement2 will be
• You can group multiple statements together with braces.
if (myVal < 3){
printf(“myVal is less than 3.\n”);
}
else {
printf(“myVal is >= to 3.\n”);
}
C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 14
An Empty Statement

if (x == 3)
; if (x != 3)
Same as
else print(“Test\n”);
print(“Test\n”);

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 15


The Dangling else Problem

if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";

Which if does the else go with?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 16


The Dangling else Solution
The compiler interprets the statement as:
if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";

To force the nested if…else statement to execute as intended, use {…}:


if ( x > 5 )
{
if ( y > 5 )
cout << "x and y are > 5";
}
else
cout << "x is <= 5";

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 17


The else if Statement (Same as C)
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

int time = 22;


if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 18


The switch Statement (Same as C)
Syntax:
switch(expression) {
//value of the expression is compared
//with the values of each case
case x: //execute code block if x = value of expression
// code block
break; //optional – break out of switch block
case y: //execute code block if y = value of expression
// code block
break; //optional – break out of switch block
default: //optional – execute code block if no other matches
// code block
}
C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 19
The switch Statement (Same as C)
Example:
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
default:
cout << "Weekend";
} // Outputs "Thursday" (day 4)

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 20


expression Must Evaluate to an Integer

• The value of the expression must be a


switch(expression) {
combination of character and integer
case x:
constants that evaluates to a constant
// code block
integer value.
break;
• The switch statement compares the case y:
value of the expression with each case // code block
label. break;
• For example: default:
– Does expression = x? // code block
– Does expression = y? }

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 21


No Braces {} Required
switch(expression) {
case x:
x = x+1;
y = y +2;
The switch selection statement does not break;
require braces {…} around multiple case y:
statements in each case. // code block
break;
default:
// code block
}

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 22


case With No break – Fall Through
If a case doesn't have an explicit break statement, execution will
continue to the next case in the list.

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
}

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 23


Any Questions?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 24


Structured Programming - Repetition

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 25


The while Statement (Same as C)

Syntax:
while (condition) statement;

• while the condition is true, the body of loop (statement) will


be executed.
• You can group multiple statements together with braces.

while(condition){
//code to be executed
}

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 26


The do-while Statement (Same as C)

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)

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 27


The for Statement (Same as C)
Syntax:
for (expr1; expr2; expr3) statement;

• The for loop will first perform the expr1 (initialization).


• Then, as long as expr2 (test) is true, it will execute statement.
• After each execution, it will execute expr3 (typically an
increment)
• You can group multiple statements together with braces.

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


printf(“Counter = %d\n”, i);
}
C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 28
More About the for Statement
The three expressions in the for statement header are optional (but
the two semicolon separators are required).

• One might omit the initialization expression


if the control variable is initialized earlier in
the program.
• If the loop condition is omitted, C++ assumes
that the condition is true, thus creating an
infinite loop.
• One might omit the increment expression if
the increment is calculated by statements in
the body of the for or if no increment is
needed.

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 29


The break Statement (Same as C)
• You have already seen the break statement used to "jump
out" of a switch statement.
• The break statement can also be used to jump out of a loop.
• This example jumps out of the loop when i is equal to 4:

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


if (i == 4) {
break;
}
cout << i << "\n";
} //output = 0 1 2 3

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 30


The continue Statement (Same as C)

• The continue statement breaks out of one iteration of a loop


and continues with the next iteration of the loop.
• This example skips the value of 4:

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


if (i == 4) {
continue;
}
cout << i << "\n";
} //output = 0 1 2 3 5 6 7 8 9

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 31


Any Questions?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 32


Evaluating conditions in C++
if (condition) statement;
if (condition) statement1; else
statement2;
else if (condition) statement;
while (condition) statement;
do statement; while (condition);
for (expr1; expr2; expr3) statement;
• A condition can be based on any expression.
• If the expression evaluates to zero, it’s treated as false.
• If the expression evaluates to nonzero, it’s treated as true.
• C++ also provides the data type bool for variables that can hold only
the values true and false.
• Each of these is a C++ keyword.
• The condition can also be determined with bool variables.
C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 33
Evaluating conditions in C
if (condition) statement;
if (condition) statement1; else
statement2;
else if (condition) statement;
while (condition) statement;
do statement; while (condition);
for (expr1; expr2; expr3) statement;
• conditions in C can use boolean values, too.
• Prior to C99, C didn't have a built-in boolean type, so integers were
used instead, where 0 represented false and any non-zero value
represented true.
• With the introduction of the stdbool.h header file in C99, the bool
type, along with the constants true and false, became available.
• This allows for more explicit and readable boolean logic in C code.
C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 34
What About expressions in switch-case?

switch(expression) {
case x:
case y:
}

• Boolean values can be used in C++ switch statements.


• Since boolean values are implicitly converted to integers (true to 1
and false to 0), they satisfy the requirement that the switch
expression must evaluate to an integer.
• Therefore, you can use true and false as case labels within a switch
statement.

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 35


What About in C?
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.

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 36


What About in C? – Well, Sort Of …

Here's how you can use


booleans in a switch
statement in C:

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 37


Any Questions?

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 38


In-Class Problem Rubric

Grading Level
Exceeds
Question Points Meets Expectations Unsatisfactory
Expectations
(80-89%) (0-79%)
(90-100%)

1 50 10 points per control structure

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.

C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 39


#include <iostream>

In-Class Problem int main() {


int age = 20;
if (age >= 18) {
std::cout << "Adult" << std::endl;
} else {
std::cout << "Minor" << std::endl;
}
int day = 3;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std.cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
1. Add a comment to identify each break;
control structure in this C++ code. default:
std::cout << "Other day" << std::endl;
2. Add a comment on every line }
for (int i = 0; i < 5; ++i) {
explaining what the C++ code is }
std::cout << "Iteration: " << i << std::endl;
doing. int counter = 0;
while (counter < 3) {
std::cout << "Counter: " << counter << std::endl;
counter++;
}
int num = 0;
do {
std::cout << "Number: " << num << std::endl;
num++;
} while (num < 2);
return 0;
}
C++ Control Structures David O. Johnson EECS 348 (Spring 2025) 40

You might also like