Sunteți pe pagina 1din 36

DR ATIF SHAHZAD

IE-322

Computer Applications
LECTURE #04 in Industrial Engg.-I
Computer Applications in
Industrial Engg.-I

IE322
Dr. Atif Shahzad

7/4/2018
…Recap
What we will see…
Do-while break continue
while loop for loop
loop statement statement
Problem (slide 17) for loop (slide 21) break statement (slide
• Problem (slide 18) • for loop: Example 30)
continue statement
while loop (slide 8) • break statement (slide 31) (slide 36)
• break statement (slide 32)

Short form break statement in


• Short form (slide 10) nested loops
• Short form (slide 11) for loop (slide 23) • break statement in nested
loops (slide 34)

Example Fill the blanks

Problem
• Problem (slide 14) for loop: with
• Problem (slide 15)
compound assignment
Dr. Atif Shahzad

for loop: equivalent of


while
• for loop: equivalent of while
(slide 27)

7/4/2018
while loop
A while loop int num = 1;
while(num < 6)
repeatedly {

executes a block Console.WriteLine(num);


num++;
of code as long }
/* Outputs
as a given 1
condition is 2
3
true. 4
Dr. Atif Shahzad

5
*/
7/4/2018 5
while loop checks the condition
declares a variable int num = 1;
while(num < 6)
{
Console.WriteLine(num);
num++;
}

if true, executes the statements


in its body

After the 5th iteration, num equals 6,


Dr. Atif Shahzad

the condition evaluates to false, and


the loop stops running.
7/4/2018 6
Short form /* Outputs
int num = 0; 1
while(++num < 6) 2
3
Console.WriteLine(num); 4
5
*/
Dr. Atif Shahzad

7/4/2018 7
Short form /* Outputs
int num = 0; 1
while(++num < 6) 2
3
Console.WriteLine(num); 4
5
*/
What do you think, is there a difference between
while(num++ < 6) and
while(++num < 6)?
Dr. Atif Shahzad

7/4/2018 8
Short form /* Outputs
int num = 0; 1 will execute 5 times,

while(++num < 6) 2 because pre-increment


increases the value of
3 x beforenum<6
checking the

Console.WriteLine(num); 4 condition

post-increment will check


the condition before
5 /* Outputs
increasing the value of */ 1
int num = 0;
num, making while(num
++ < 6) execute 6 times.
2
while(num++ < 6) 3
4
Console.WriteLine(num); 5
Dr. Atif Shahzad

6
7/4/2018 */ 9
Example /* Outputs
int num = 1; 1
while(num < 6) 3
5
{ */
Console.WriteLine(num);
num+=2;
} Without a statement that eventually
evaluates the loop condition to false, the
loop will continue indefinitely.
Dr. Atif Shahzad

7/4/2018 10
Problem
How many times will the following
loop execute?
int x = 1;
while (x++ < 5)
{
if (x % 2 == 0)
x += 2;
Dr. Atif Shahzad

}
7/4/2018 11
Problem
How many times will the following
loop execute? START

int x = 1;
int x=1

while (x++ < 5)


{ X++<5 ? True x % 2 == 0 ? True X+=2

if (x % 2 == 0) False

False

x += 2;
Dr. Atif Shahzad

STOP
}
7/4/2018 12
Problem
How many times will the while condition be
checked? START

int x = 1;
int x=1

while (x++ < 5)


{ X++<5 ? True x % 2 == 0 ? True X+=2

if (x % 2 == 0) False

False

x += 2;
Dr. Atif Shahzad

STOP
}
7/4/2018 13
Do-while loop
A do-while loop is similar to a while loop,
except that a do-while loop is guaranteed
to execute at least one time.
Dr. Atif Shahzad

7/4/2018 14
Problem
int a = 0;
do {
/* Outputs
Console.WriteLine(a); 0
a++; 1
2
} while(a < 5);
3
4
*/
Dr. Atif Shahzad

Note the semicolon after the while statement.

7/4/2018 15
Problem
What is the output of this code?
int a = 2;
do {
a+=3;
} while(a < 4);
Console.Write(a);
Dr. Atif Shahzad

7/4/2018 16
Computer Applications in
Industrial Engg.-I

IE322
for loop
A for loop
repeatedly
executes a block
of code a
specific number
of times.
for (init; condition; increment )
{
Dr. Atif Shahzad

statement(s);
}
7/4/2018 18
for loop

for (init; condition; increment )


{
Dr. Atif Shahzad

statement(s);
}
7/4/2018 19
for loop: Example
for (int x = 10; x < 15; x++)
{
Console.WriteLine("Value of x: {0}", x);
}
/*
Value of x: 10
Value of x: 11
Value of x: 12
Value of x: 13
Dr. Atif Shahzad

Value of x: 14
*/
7/4/2018 20
for loop
A counter is declared once in init.
Next, the condition evaluates the value of the counter and the
body of the loop is executed if the condition is true.
After loop execution, the increment statement updates the
counter, also called the loop control variable.
The condition is again evaluated, and the loop body repeats,
only stopping when the condition becomes false.

for (init; condition; increment )


{
Dr. Atif Shahzad

statement(s);
}
7/4/2018 21
Fill the blanks
for ( ; ; )
{ for
Console.WriteLine(x); x++
}
X<10
While
int x=5
Dr. Atif Shahzad

7/4/2018 22
for loop: with compound assignment
for (int x = 0; x < 10; x+=3)
{
Console.WriteLine(x);
}
/* Outputs
0
3
6
9
*/
Dr. Atif Shahzad

7/4/2018 23
for loop: equivalent of while
int x = 10;
for ( ; x > 0 ; )
{
Console.WriteLine(x);
x -= 3;
} You can skip init and increment but
; are mandatory
Dr. Atif Shahzad

7/4/2018 24
for loop: equivalent of while
How many times will this loop run?
int x = 1;
for ( ; x < 9; )
{
x+=2;
}
Dr. Atif Shahzad

7/4/2018 25
Computer Applications in
Industrial Engg.-I

IE322
break statement
int num = 0;
while (num < 20)
{
if (num == 5)
break;
Console.WriteLine(num);
num++;
}
Dr. Atif Shahzad

7/4/2018 27
break statement
When the break statement
int num = 0; is encountered inside a loop,
while (num < 20) the loop is immediately
terminated and the program
{
execution moves on to the
if (num == 5) next statement following the
break; loop body.
Console.WriteLine(num);
num++;
}
Dr. Atif Shahzad

7/4/2018 28
break statement
When the break statement
int num = 0; is encountered inside a loop,
while (num < 20) the loop is immediately
terminated and the program
{
execution moves on to the
if (num == 5) next statement following the
break; loop body.
Console.WriteLine(num);
num++;
}
Dr. Atif Shahzad

7/4/2018 29
break statement
/* Outputs:
int num = 0; 0
while (num < 20) 1
{ 2
if (num == 5) 3
break; 4
Console.WriteLine(num); */
num++;
}
Dr. Atif Shahzad

7/4/2018 30
break statement in nested loops
If you are using nested loops (i.e., one loop
inside another loop),
the break statement will stop the execution
of the innermost loop and start executing the
next line of code after the block.
Dr. Atif Shahzad

7/4/2018 31
break statement in nested loops
What is the largest number that will be printed
by this code?

for (int x = 1; x < 8; x++) {


if (x > 5)
break;
Console.WriteLine(x);
}
Dr. Atif Shahzad

7/4/2018 32
continue statement
for (int i = 0; i < 10; i++)
{
skips the current iteration of
if (i == 5) the loop and continues with
continue; the next iteration.

Console.WriteLine(i);
}
Dr. Atif Shahzad

7/4/2018 33
continue statement /* Outputs:
for (int i = 0; i < 10; i++) 0
1
{
2
if (i == 5) 3
continue; 4
Console.WriteLine(i); 6
7
}
8
9
Dr. Atif Shahzad

7/4/2018
*/ 34
NEVER hesitate to
contact should you
have any question
Dr. Atif Shahzad
Dr. Atif Shahzad

7/4/2018 36

S-ar putea să vă placă și