I have been programming for the last few weeks and I have tests coming up soon and theres a few things I want to get straight in my head so does anyone know were I could find some sourse files of a simple program that uses an if...else command and another with a do...while command? If someone even wrote an example in there answer, that would be great. This isnt homework or anything, I just need to get things straight in my mind. Any help would be great.
C++ Help!!!?
////////////////////////////////////////...
//////////////////////////////////////...
Warning
The first piece of article is written by myself.
//////////////////////////////////////...
I am a Chinese student and I would be glad to receive your letters and be your friend.
//////////////////////////////////////...
//////////////////////////////////////...
In my mind that "if..else.." statement is always used like this:
Selection Statement: If-Else
Key: ture or false?
if(iAmAStudent)
callSchool;
else
callOffice;
Is (iAmAStudent) true? (Am I a student?)
Yes? "callSchool;"
No? "callOffice;"
--------------------------------------...
"do..while.." Repetition Statement
Key WORD : repeat doing expression at least 1 time
do{
expression;
}while(condition)
This kind of Repetition Statement can do expression at least 1 time, and this Repetition will never finish until the condition turn to false.
//////////////////////////////////////...
I am a Chinese student and I would be glad to receive your letters and be your friend.
//////////////////////////////////////...
//////////////////////////////////////...
//////////////////////////////////////...
Contents extracted from:
C++ How to Program, Fifth Edition
//////////////////////////////////////...
//////////////////////////////////////...
Hope that helps you!
//////////////////////////////////////...
if...else Double-Selection Statement
The if single-selection statement performs an indicated action only when the condition is TRue; otherwise the action is skipped. The if...else double-selection statement allows the programmer to specify an action to perform when the condition is true and a different action to perform when the condition is false. For example, the pseudocode statement:
If student's grade is greater than or equal to 60
Print "Passed"
Else
Print "Failed"
--------------------------------
if ( grade %26gt;= 60 )
cout %26lt;%26lt; "Passed";
else
cout %26lt;%26lt; "Failed";
--------------------------------
--------------------------------
**********************
do...while Repetition Statement
The do...while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes. The do...while statement tests the loop-continuation condition after the loop body executes; therefore, the loop body always executes at least once. When a do...while terminates, execution continues with the statement after the while clause. Note that it is not necessary to use braces in the do...while statement if there is only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do...while statements. For example,
while ( condition )
normally is regarded as the header of a while statement. A do...while with no braces around the single statement body appears as
do
statement
while ( condition );
which can be confusing. The last linewhile( condition );might be misinterpreted by the reader as a while statement containing as its body an empty statement. Thus, the do...while with one statement is often written as follows to avoid confusion:
do
{
statement
} while ( condition );
@@@@@@@@@@@@@@@@@
1 // Fig. 5.7: fig05_07.cpp
2 // do...while repetition statement.
3 #include %26lt;iostream%26gt;
4 using std::cout;
5 using std::endl;
6
7 int main()
8 {
9 int counter = 1; // initialize counter
10
11 do
12 {
13 cout %26lt;%26lt; counter %26lt;%26lt; " "; // display counter
14 counter++; // increment counter
15 } while ( counter %26lt;= 10 ); // end do...while
16
17 cout %26lt;%26lt; endl; // output a newline
18 return 0; // indicate successful termination
19 } // end main
1 // Fig. 5.7: fig05_07.cpp
2 // do...while repetition statement.
3 #include %26lt;iostream%26gt;
4 using std::cout;
5 using std::endl;
6
7 int main()
8 {
9 int counter = 1; // initialize counter
10
11 do
12 {
13 cout %26lt;%26lt; counter %26lt;%26lt; " "; // display counter
14 counter++; // increment counter
15 } while ( counter %26lt;= 10 ); // end do...while
16
17 cout %26lt;%26lt; endl; // output a newline
18 return 0; // indicate successful termination
19 } // end main
@@@@@@@@@@@@@@@@@
//////////////////////////////////////...
I am a Chinese student and I would be glad to receive your letters and be your friend.
//////////////////////////////////////...
learn together, make progress everyday.
Reply:if/else psuedocode:
if (i am rich)
buy a car
else
get more money
do-while psuedocode:
do (give me money)
while (i have no car)
while psuedocode:
while (money %26lt; price of car)
{
get more money
}
for loop psuedocode:
for (money is 0, money %26lt; car; get more money)
count my money and complain
OK so here it is...
void main()
{
int rich = 0; // how much money i have
int car = 10000; //how much i need for car
if (rich = car);
cout %26lt;%26lt; " buy a car and pimp my ride";
else
{
rich++;
cout %26lt;%26lt; "I just earned 1 dollar";
}
}
void main()
{
int rich = 0;
int car = 1000;
do
{
rich++;
}
while (rich %26lt; car);
}
void main()
{
int rich = 0;
int car = 1000;
while ( rich %26lt; car )
{
rich++;
}
}
void main()
{
int rich;
int car = 1000;
for (rich = 0; rich %26lt; car; rich++)
{
cout %26lt;%26lt; "still need more money";
}
cout %26lt;%26lt; "now I can buy my car";
}
//good luck on your tests.
deliver flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment