The loop Control Structure Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

3 Methods to loop:

A
  1. For statement
  2. While Statment
  3. do-while
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

while statement:

A

initialise loop counter (index variable);
while (Condition [to test loop counter or any Exp] )
{ body of while;
loop counter increment / decrement;
}

loop counter can be float as well, u can incre or decree them by a float value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Default scope of while

A

1 statement right after it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Output?
int i = 1;
while ( i<= 32767 )
{
   printf("hi");
   i++
}
A

Indefinite.
when is 32767 and reaches increment, it wants to become 32768 which falls out of int range so instead it becomes -32768 and it satisfies the while condition, this will keep on happening.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
OutPut?
int i = 1;
while ( i<= 10 );
{
   printf("hi");
   i++
}
A
Indefinite
int i = 1;
while ( i<= 10 )
         ;
{
   printf("hi");
   i++
}
No output but control keeps rotating as i is not incremented.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Compound Assignment Operator:

A

+= , -=, *=, /=, %=.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

While(i++ < 10)

A

post increment operator. Performs i < 10 and then i becomes i + 1, for the body of loop for tht cycle and next check.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

++i

A

First ‘i’ gets incremented and then its Exp is evaluated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The for loop:

A

for ( initialise counter ; test counter ; increment counter)
{
excute statements;
}

Initialization, testing & increment incorporated in for statement.
those three can be any expressions, remember the flow of control.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Working of For:

A

set counter value, test condition execute the body and then increment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Default Scope of For:

A

Immediately next statement after it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

is this Valid:
for ( ;i++ >10; ){
}

A

Note semicolons are necessary.

here the comparison is done then the value is incremented and then body of the loop is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Nesting for loops.

A

For each value of outer loop, inner loop will take all values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Multiple Initialisations in the for loop.

A

for ( i = 1 , j = 0 ; j < 2 ; i++, j++ ) {statement;}
test expression can be only one but u can use logical operators to link more conditions.
hence ; are used in for statement so , allow us to use multiple initialisation n increment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The odd Loop

A

When we unaware how many times to loop

use of do-while.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

break statement:

A

When ‘break’ is encountered the control is passed to the statement just after the loop its executing in.

17
Q

Find a prime number.

A

pg 118.

18
Q

The continue Statement:

A

On encounter of continue in a loop, the control is passed to the beginning of the loop, skipping the body after continue.

19
Q

The Do-While loop:

A
do
{
statement(s);
}
while( condition );

here body is necessarily executed at least once.

20
Q

break and continue in do while:

A

break will send u to after condition.

Continue will send u to end at the condition and execute it.