+ All Categories
Home > Documents > Lecture 9: Making Decisions Final Section

Lecture 9: Making Decisions Final Section

Date post: 01-Jan-2016
Category:
Upload: carol-yates
View: 28 times
Download: 2 times
Share this document with a friend
Description:
Lecture 9: Making Decisions Final Section. Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220. Increment and Decrement. ++ and – are operators that add and subtract 1 from their operands Increment Decrement But, C++ has a special set, just for counting by 1’s ++ --. - PowerPoint PPT Presentation
Popular Tags:
19
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220
Transcript

Lecture 9: Making Decisions Final Section

Professor: Dr. Miguel Alonso Jr.

Fall 2008

CGS2423/COP1220

Increment and Decrement

++ and – are operators that add and subtract 1 from their operands

Increment Decrement

But, C++ has a special set, just for counting by 1’s ++ --

num = num + 1;num += 1;

num = num - 1;num -= 1;

POSTFIXnum ++;num --;

PREFIX++num;--num;

// This program demonstrates the prefix and postfix// modes of the increment and decrement operators.#include <iostream>using namespace std;

int main(){ int num = 4; cout << num << endl; // Displays 4 cout << num++ << endl; // Displays 4, then adds 1 to num cout << num << endl; // Displays 5 cout << ++num << endl; // Adds 1 to num, then displays 6 cout << endl; // Displays a blank line cout << num << endl; // Displays 6 cout << num-- << endl; // Displays 6, then subtracts 1 from num cout << num << endl; // Displays 5 cout << --num << endl; // Subtracts 1 from num, then displays 4 return 0;}

Using ++ and – in Math and Relations

c = a * b++; c = a * ++b; c = ++(a * b);

if(x++ > 10){…} if(++x > 10) {…}

The WHILE Loop

Loop: A part of the program that repeats while is a pre-test loop

while(expression)statement;

while(expression){

statement;statement;// as many as// you need

}

// This program demonstrates a simple while loop.#include <iostream>using namespace std;

int main(){ int number = 1;

while (number <= 5) { cout << "Hello\n"; number++; } cout << "That's all!\n"; return 0;}

Tips for while loops

Infinite loops Loops should contain a way to terminate themselves If not, the run on forever

Don’t forget braces { } with blocks of statements

Programming style Similar to if

Statements on the following line Indented and within braces

In the Spotlight

Pg. 259

Using the while loop for input validation

The while loop can be used to create input routines that repeat until acceptable data is entered

Counters

A counter is a variable that is regularly incremented or decremented each time a loop iterates

Some times you may need to keep track of something every time the loop is performed

// This program displays the numbers 1 through 10 and// their squares.#include <iostream>using namespace std;

int main(){ int num = 1; //Initialize the counter.

cout << "Number Number Squared\n"; cout << "-------------------------\n"; while (num <= 10) { cout << num << "\t\t" << (num * num) << endl; num++; //Increment the counter. } return 0;}

The do-while loop

The do-while is a posttest loop, which means that its expression is tested after each iteration

dostatement;

while (expression);

do{

statement;statement;statement;// as many as you// need

} while (expression);

Using do-while with menus

The for loop

The for loop is perfect for performing a known number of iterations

Two types: Conditional

while, do-while

Count-controlled For

Count controlled posses three elements It must initialize a counter variable It must test the counter by comparing it to a max for termination It must update the counter during each iteration

for(initialization; test; update)statement;

for(initialization; test; update){

statement;statement;statement;// as many as you //need

}


Recommended