Download presentation
Presentation is loading. Please wait.
1
CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 19: 11/5/2002 Lecture 19: 11/5/2002 CS149D Fall 2002
2
Outline Selection Statements cont. Switch statement Looping
While statement Lecture 19: 11/5/2002 CS149D Fall 2002
3
Selection Statements cont.
Switch statement switch (expression) { case value1: statement(s); break; case value2: statement(s); break; case valuen: statement(s); break; default: statement(s); } if ( i == 1) x = x +1; else if (i == 2) y = y +1; else if (i == 3) z = z+1; else if (i == 4) T = T +1; Is there a better way? switch(i) { case 1: x = x+1; break; case 2: y = y+1; break; case 3: z = z+1; break; case 4: T = T+1; } Can be rewritten as Lecture 19: 11/5/2002 CS149D Fall 2002
4
Loops Implement Repetitive structures 3 loop structures in C++
while loop, do/while loop, for loop General functionality Execute a certain block of statements until a condition is not true. Lecture 19: 11/5/2002 CS149D Fall 2002
5
The while loop Condition True ? Execute while Statement(s)
Continue to next statement No Yes while (condition) statement; or { statements; } Infinite loop If condition is always true Lecture 19: 11/5/2002 CS149D Fall 2002
6
The while loop int x = 0,z = 0, y = 5; while (x < y) { z++;
cout << z; x++; } What is the program output? Example to convert degrees to radians (page 73-74) Degrees range from 0 to 360 int degrees = 0; while (degrees <= 360) { radians = degrees * PI/180; degrees += 10; } Lecture 19: 11/5/2002 CS149D Fall 2002
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.