Document 7857484

Download Report

Transcript Document 7857484

Chapter 3
Control Structures
Branching
Decision Making
Changing the order of execution
 Thus far, each line is done in order
 What if you do NOT want that order?
 In C# you may use IF statements
IF (expression)

Statement 1;

If expression is TRUE, then statement 1 will
run
Logical Conditions
Symbol
<
<=
>
>=
==
!=
Meaning
less than
less than or equal
greater than
greater than or equal
equal
not equal
Example
31 < 25
464 <= 7213
-98 > -12
9 >= 99
9 = 12 + 12
292 != 377
FIGURE 3.1 C# relational and equality operators
false
true
false
false
false
true
.
3
Salary=20000
yes
Salary > 15000?
Compute tax at 10%
no
Compute tax at 5%
4
Example of IF
• If year is greater than 2001 add 2 to age
int age = 20; // assign value 20 to age
int year = 2002;
if (year > 2001) // note: NO colon!
age = age+2; // the statement ends here (;)
• Result …
• Since year IS > 2001, 2 will be added to
age, otherwise execution continues to next
statement
Printing (it never ends!)
If you want to output in columns…
 Console.WriteLine(“{0,-10} {1,10}”,”Name”,
“Salary”);
 Console.WriteLine(“{0,-10} {1,10}”,person1,
salary1);
 Output sample:
Name
Salary
John Jones
190.00

6
Example continued
int age = 20;
int year = 2000;
if (year > 2001)
age = age+2;
• Result …
• Since year IS NOT > 2001, next statement
is ignored (age=age+2)
7
One more example
To check equality…
if (age = = 21)


….. As before

Notice the DOUBLE =?
What about compound conditions?
 If age < 21 AND year > 2000
if ((age < 21) && (year > 2000))
 || is the symbol for OR

8
Variation on IF (Else)
double gpa=3.7;
if (gpa > 3.5)
Console.WriteLine(“On Dean’s List”);
else
Console.WriteLine(“Not on List!”);
• What if gpa = 3.5?
9
Multiple statements (blocks)
if (expression)
{
Line1;
Line2;
Linen;
}// end if
ALL statements within braces are executed if
expression is TRUE
10
Chapter 3
LOOPs
11
Loops
Loops repeat a group of statements
while (expression)
{

Statement1;
Statement2;
StatementN;
}// end while
Next line;
 If expr is true, statementN returns to statement1
 When expr is false, Next line is executed.
12
GPA=3.6
Add to Dean’s List
GPA>=3.5?
yes
no
13
GPA=3.6
Add to Dean’s List
Get new GPA
GPA>=3.5?
yes
no
14
Examples of LOOPs
only display when GPA greater than or equal to 3.5:
double gpa;
input gpa // input the value the first time outside loop
while (gpa>=3.5)
{
display “on deans list”;
input gpa // notice age changes inside the loop?
}// end while
Continue when gpa less than 3.5

15
The FOR loop
This type of loop starts and stops the loop under
control:
int numemp;
initialize
test condtn increment
for (numemp=1; numemp<10; numemp++)
{
steps to execute
}// end for
statement after for

16
Example
int looper, Numemp; double salary, deducts;
Console.Write(“How many employees? ”);
Numemp=int.Parse(Console.ReadLine());
for (looper=0; looper<Numemp; looper++)
{
Console.Write(“Please enter salary: ”);
salary=double.Parse(Console.ReadLine());
Console.Write(“Please enter deductions: ”);
deducts=double.Parse(Console.ReadLine());
} // end for loop
17
Where does it fit?
// declare section (as usual)
// input section
Console.Write(“How many employees? ”);
Numemp=int.Parse(Console.ReadLine());
for (looper=0; looper<Numemp; looper++)
{
Console.Write(“Please enter salary: ”);
salary=double.Parse(Console.ReadLine());
Console.Write(“Please enter deductions: ”);
deducts=double.Parse(Console.ReadLine());
} // end for loop
18
The foreach loop



Makes loops in a string very easy
foreach (string word in sentence.Split())
Check the following program segment
19
string sentence;
sentence=Console.ReadLine();
foreach (string word in sentence.Split())
{
Console.WriteLine(word);
}// end foreach
Console.ReadLine();
20
21