[go: up one dir, main page]

0% found this document useful (0 votes)
17 views26 pages

Lecture 4

The document outlines the content of Lecture 4 for the Introduction to Programming (CS 101) course, covering topics such as while loops, break/continue statements, and scope. It includes examples of code snippets demonstrating logical operators, switch statements, and the use of NaN and infinity in programming. Additionally, it discusses the syntax and semantics of while and do-while loops, as well as the concept of variable scope and shadowing.

Uploaded by

gurvardaan01
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)
17 views26 pages

Lecture 4

The document outlines the content of Lecture 4 for the Introduction to Programming (CS 101) course, covering topics such as while loops, break/continue statements, and scope. It includes examples of code snippets demonstrating logical operators, switch statements, and the use of NaN and infinity in programming. Additionally, it discusses the syntax and semantics of while and do-while loops, as well as the concept of variable scope and shadowing.

Uploaded by

gurvardaan01
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/ 26

Introduction to Programming (CS 101)

Spring 2024

Lecture 4:
- while loops, break/continue, do while, scope
Instructor: Preethi Jyothi

- Based on material developed by Prof. Abhiram Ranade


Recap-I (if and logical operators)

What is the output from the following #include <simplecpp>


piece of code? main_program{

int i = 1, j = 1, k = -1;
A 1 1 -1
if(!i || j && k)
i += 1;
B 1 2 -1 else
j += 1;
C 2 1 -1
cout << i << " " << j << " " << k;
}
D 2 2 -1
Recap-II (switch statement)
#include <simplecpp>
main_program{

What is the output from the following int i = 0, j = 1, k = -1;


piece of code?
switch(!i * j - k) {
case 2:
A 1 1 0 case 1:
j += 1; k += 1;
case 0:
B 0 2 0 i += 1;
break;
C 1 2 1 default:
k += 1;
}
D 1 2 0
cout << i << " " << j << " " << k;
}
Recap-III (ternary operator)

What is the output from the following


piece of code?
#include <simplecpp>
main_program{
A 0
int i = 0, j = 0;
B 1
cout << (i > j ? i-1 : j+1) << endl;
}
C -1
An aside: nan and inf
CS 101, 2025
NaN (Not a Number) vs. inf (In nity)
• nan:Short for Not a Number; cannot be de ned or represented

• Examples where nan appears:


• Log of a non-positive number
• Square root of -1

• inf:Short for In nity; numbers that are too large (in absolute value)

• Examples where inf appears:


• Divide (non-zero) number by zero
• Over ow: When a number exceeds the maximum representable oating-point number

• Note both these quantities relate to oating point numbers


fl
fi
fl
fi
fi
fl
while statement
CS 101, 2025
Compute average of scores

• Requirement: Read as input a sequence of student's scores (0 to 100) and print its average
• Number of students is not known beforehand
• Assume that at least one positive score will be given
• Treat a negative number as a signal to end the sequence

• Example:
• Input: 80,20,-5 Output: 50

• Implement using repeat?


• repeat repeats xed number of times and we do not know the number of students

• New looping constructs (while, do while, for) that naturally support such requirements
fi
while statement
• Syntax: start of while

while(condition){
body
false
} condition

• Semantics: true
1. Evaluate condition
body
2. If condition evaluates to true, then body is executed
3. If condition evaluates to false, then skip the while block
and move to the statement following while
4. Go back to step 1 and repeat
next statement after while
while statement (I)
• What does this program do?

#include <simplecpp>
#include <simplecpp>
main_program {
equivalent to
int i = 0; main_program {
while(i <= 10) { int i = 0;
i = i + 2; while(i <= 10) {
cout << i << endl; cout << (i+=2) << endl;
} }
} 2
}
4
6
8
10
12
output
while statement (II)
• What does this program do?

#include <simplecpp>

main_program { What if this was true? In nite loop!

int i = 0;
while(false) { • The while condition
i = i + 2; must eventually
cout << i << endl; become false,
} otherwise the program
} Nothing will be printed will never halt.
output
fi
Code to average student's scores

Demo in class and code shared on Moodle


break/continue statements
CS 101, 2025
break statement within while loop
• Syntax of break within a while loop:

while(condition){
body1
break;
body2

}

• Semantics:
• if condition is satis ed, body1 is executed and when control reaches break, the
execution of the while statement is terminated.
• That is, body2 is not executed if break appears right before it.
• Execution continues from the next statement following the while block.
fi
break in code to average student's scores
• Consider break in the following piece of code that implements averaging student's scores:

main_program {
float next, sum = 0;
int count = 0; if next < 0, then the while loop
execution terminates
while(true) {
cin >> next;
Execution continues from the statement
if(next < 0) break; after while, i.e., cout << ...
sum += next;
count += 1;
}
cout << sum/count << endl;
}
break in code to average student's scores
• Consider break in the following piece of code that implements averaging student's scores:

main_program {
float next, sum = 0;
int count = 0; Note how break is written. Since {} is
while(true) { omitted, the single statement after if
cin >> next; can appear on the same line.
if(next < 0) break;
sum += next;
count += 1;
}
cout << sum/count << endl;
}

• If break appears inside a while which is itself nested inside another while, then the
inner while statement is terminated
continue statement within while loop
• Syntax of continue within a while loop:

while(condition){
body1
continue;
body2
}

• Semantics:
• if condition is satis ed, body1 is executed and when control reaches continue, it
goes to the while loop for the next iteration.
• body2 i.e., statements from continue to the end of the loop are skipped.
fi
Averaging student's scores, with a constraint
• Ignore if a score > 100, and move on to the next score in the input

main_program {
float next, sum = 0;
int count = 0;
while(true) {
cin >> next;
if(next < 0) break;
sum += next;
count += 1;
}
cout << sum/count << endl;
}
Averaging student's scores, with a constraint
• Ignore if a score > 100, and move on to the next score in the input

main_program {
float next, sum = 0;
if next > 100, then control is transferred
int count = 0; to the beginning of the while loop
while(true) {
cin >> next;
if(next > 100) continue;
if(next < 0) break;
sum += next;
count += 1;
}
cout << sum/count << endl;
}
ddo while statement
CS 101, 2025
do while statement
• Syntax:
do{
body
}while(condition);

• Semantics: Equivalent to

{ body }
while(condition){
body
}
• Note: The above equivalence holds only when body does not contain a continue
statement. continue is only used within loop bodies.
• Compared to while, can avoid one condition evaluation, if it holds anyway in the beginning
• Compared to while, do while is less commonly used
Blocks and scope
CS 101, 2025
Blocks and scope
• Code inside {} is referred to as a block

• repeat, if, etc. typically consists of blocks; one could create them otherwise too by
just adding {}

• Variables can be declared inside a block

main_program {
int sum = 0;
repeat(10) {
int term;
cin >> term;
sum += term;
}
cout << sum << endl;
}
How de nitions in a block execute

• A variable is de ned/created every time control reaches the de nition.

• All variables de ned in a block are destroyed every time control reaches the end of the
block.

• “Creating” a variable is only notional; the compiler simply starts using that region of
memory from then on.

• Likewise “destroying” a variable is notional.


fi
fi
fi
fi
Scope and shadowing

• Variables de ned outside a block can be used inside


the block, if no variable of the same name is de ned
main_program {
inside the block.
int i = 3;
cout << i << endl;
• If a variable of the same name is de ned, then from
the point of de nition to the end of the block, the {
newly de ned variable gets used. cout << i << endl;
int i = 8;
• The new variable is said to “shadow” the old variable. cout << i << endl;
}
• The region of the program where a variable de ned in cout << i << endl;
a particular de nition can be used is said to be the } 3
3
"scope" of the de nition. 8
3
output
fi
fi
fi
fi
fi
fi
fi
fi
Next class: Looping construct "for"
CS 101, 2025

You might also like