Introduction to Programming (CS 101)
Spring 2024
Lecture 5:
- for loop, increment/decrement operators, constants
Instructor: Preethi Jyothi
- Based on material developed by Prof. Abhiram Ranade
Recap-I (nested if): Dangling else problem
What is the output from the following
piece of code? The formatting of the
statements may not correctly re ect the #include <simplecpp>
underlying behaviour.
main_program{
int n = -1;
if(n < 10)
A Number's more than 10 if(n > 0)
cout << "Positive number\n";
No output else
B
cout << "Number's more than 10\n";
C Positive number }
fl
Recap-II (while statement)
What does this program output (in words)?
For n = 44, what value of p is printed? #include <simplecpp>
main_program{
A 44 unsigned int n;
cin >> n;
unsigned int p = 1;
B 42
while(p * 2 <= n)
C 32 p *= 2;
cout << p;
D 64
}
Recap-III (while and break)
Demo in class and code (guess.cpp) shared on Moodle
Constants
CS 101, 2025
Constants
• A constant is a variable with a xed value de ned before the program runs
• We use the const keyword to de ne constants:
const int i = 1;
i is a constant 1 is a literal
• const is an annotation to any type that ensures that it will not be changed
• A statement i = 5; after the const de nition above will lead to a compiler error
• Why use constant variables instead of just literals themselves?
• Improved readability
• Modular if the value needs to be changed; can make the change in just one place
fi
fi
fi
fi
for statement
CS 101, 2025
Motivation: for statement
• Example: Write a program to print a table of cubes of numbers from 1 to 100
int i = 1;
repeat(100) {
cout << i << "|" << i*i*i << endl;
i+=1;
}
• The idiom above, which is, "do something for every number between x and y" occurs
very commonly
• The for loop, with its syntax, makes it easy to implement this idiom
previous statement before for
for syntax and semantics
initialization
• Syntax:
for(initialization; condition; update) {
body false
} condition
• Example:
true
for(int i = 1; i <= 100; i++) {
body
cout << i << " " << i*i << endl;
} update
• Semantics:
• Before the rst iteration of the loop, initialization is executed
• Within each iteration: next statement after for
• If condition evaluates to false, the loop terminates.
• If condition evaluates to true, body is executed, followed by update. Then, the next
iteration begins.
fi
while and for
initialization condition initialization condition update
int d = 2; for(int d = 2; x > 1; d+=1) {
while(x > 1) { while(x % d == 0) {
while(x % d == 0) { x /= d;
x /= d; cout << d << " ";
cout << d << " "; }
} }
update
d+=1;
}
• Note that a new variable d (int d) is de ned in the initialization. d is accessible within the
loop body, i.e. the scope of d is the for loop's body. d is accessible within "condition" and
"update" as well.
• Note that the scope of d in the code on the left extends beyond the outer while loop.
However, the scope of d in the code on the right is only within the for loop; d is not
accessible outside the for loop.
fi
for examples
• repeat(n) { ... } can be replaced with:
for(int i = 0; i < n; i+=1) { ... }
OR
for(int i = n; i > 0; i-=1) { ... }
• for( ; ;) is allowed. That is, empty condition, empty initialization and empty
update is allowed.
• This would be an in nite loop, like while(true)
• Would need a break statement to get out of the loop
fi
Increment, decrement operators
• for(int i = 0; i < n; i+=1) { ... } is more commonly written as
for(int i = 0; i < n; i++) { ... }
OR
for(int i = 0; i < n; ++i) { ... }
using the increment (++) operator.
• There is similarly a -- decrement operator (i--, --i)
• Both ++i and i++ in the for loop above work as shorthand for i = i + 1
... with an important caveat
Increment, decrement operators
• An important difference between the pre-increment (++i) and post-increment (i++) operators:
• ++i will rst increment i and evaluate to the incremented value
• i++ will evaluate to the original value of i prior to incrementing
• Similarly, the pre-decrement (--i) and post-decrement (i--) operators
output
for(int i = 0; i < 10;) { This would output 1 to 10
cout << ++i << "\n";
}
output
for(int i = 0; i < 10;) {
This would output 0 to 9
cout << i++ << "\n";
}
fi
Increment, decrement operators
• What is the output of this program?
int x = 0, y = 0;
for(int i = 0; i < 3; i++) {
x += i++;
}
for(int i = 0; i < 3; ++i) {
y += ++i;
} output
cout << x << "\n"; 2
cout << y << "\n";
4
output
nested for example
• What does this program do?
#include <simplecpp>
main_program {
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; j++) cout << j;
cout << "\n";
}
} 012
012
012
output
Another for example
• What does this program do?
Note the empty body
char c; of this for loop.
unsigned int n;
for(cin >> c; c<'0' || c>'9'; cin >> c);
for(n=0; c>='0' && c<='9'; n=n*10+(c-'0'), cin>>c);
output
The rst for loop will Note the , (comma) operator. Different
ignore non-digit letters till
the rst digit is from , used as a separator (E.g., int
encountered. Then, a i, j;). Expressions separated by a
number is accumulated comma are evaluated left-to-right.
from the digits.
fi
fi
break across different loop structures
• Note consistent behaviour of break across while,
while(condition) {
do-while and for loop structures:
// ...
break; • Causes a jump to the statement immediately
body following the enclosing loop
}
do { • break can appear only within a loop body or
// ... within switch (case) blocks
break; • break within nested loops will terminate only the
body enclosing loop (and not break out of all nested
}while(condition) loops)
for(initialization;condition;update) {
// ...
break;
body
}
continue across different loop structures
while(condition) {
// ...
continue;
body
}
• Note consistent behaviour of continue across while,
do {
// ... do-while and for loop structures:
continue; • Causes a jump to the end of the loop body
body
}while(condition)
for(initialization;condition;update) {
// ...
continue;
body
}
continue in a nested loop
• What is the output of this program?
main_program {
for(int i = 0; i < 3; i++) { 0, 1
for(int j = 0; j < 3; j++) { 0, 2
1, 1
if(j == 0) 1, 2
continue; 2, 1
2, 2
cout << i << "," << j << endl;
} output
}
}
Next class: Internal Representations of data types
CS 101, 2025