Vous êtes sur la page 1sur 21

if (Condition)

{
statement(s);
}

if (Condition)
statement;

Condition contains Relational operators


as well as Logical Operators.

Example:

if (i > 0)
{
printf("i = %d “, i );
}

if (i > 1 || j!=5)
{
printf("i = %d “, i );
}

-1- By: Kute T. B. for FYIF (2007-08)


if statement flow-chart

true
Condition?

Statement-1 false

Statement-2

Statement-3

-2- By: Kute T. B. for FYIF (2007-08)


The if...else Statement

if (condition)
{
stmt(s) for true case;
}
else
{
stmt(s) for true case;
}

Or

if (condition)
stmt for true case;
else
stmt for true case;

-3- By: Kute T. B. for FYIF (2007-08)


if...else statement flow-chart

true
Condition?

Statement-1 false

Statement-2

Statement-3

-4- By: Kute T. B. for FYIF (2007-08)


Multiple Alternative if Statements

if (score >= 90)


grade = ‘A’;
else
if (score >= 80)
grade = ‘B’;
else
if (score >= 70)
grade = ‘C’;
else
if (score >= 60)
grade = ‘D’;
else
grade = ‘F’;

-5- By: Kute T. B. for FYIF (2007-08)


The switch-case Statement

switch (variable-name)
{
case value1:
Execute stmt1;
break;
case value2:
Execute stmt2;
break;

case valueN:
Execute stmtN;
break;
default:
Execute last;
}
-6- By: Kute T. B. for FYIF (2007-08)
switch-case flowchart

Variable
Value1 default
Value2

Stmt-1 Stmt-2 last

Next
Statement

-7- By: Kute T. B. for FYIF (2007-08)


switch-case example

int w = 20;
switch(w)
{
case 10: printf(“A”);
break;
case 20: printf(“B”);
break;
case 30: printf(“C”);
break;
default: printf(“D”);
}

If the value is character then


it must be enclosed in ‘ ’
e.g. case ‘q’:

-8- By: Kute T. B. for FYIF (2007-08)


Conditional / Ternary operator

(condition) ? exp1 : exp2

if (x > 0)
y = 1
else
y = -1;

is equivalent to -

y = (x > 0) ? 1 : -1;

-9- By: Kute T. B. for FYIF (2007-08)


goto statement
goto label;
- - - - - -
- - - - - - Forward
- - - - - - Jump
label:
statement;

label:
statement; Backward
- - - - - - Jump
- - - - - -
- - - - - -
goto label;

- 10 - By: Kute T. B. for FYIF (2007-08)


while loop
while (condition is true)
{
// loop-body;
}

e.g.
int a = 20;
while(a>10)
{
printf(“CPR”);
a--;
}

- 11 - By: Kute T. B. for FYIF (2007-08)


while loop flow-chart

false
Condition?

Statements true
inside the loop

Statement-2

Statement-3

- 12 - By: Kute T. B. for FYIF (2007-08)


do-while loop

do
{
// Loop body;
}
while (condition is true);
Example:

int a = 0;
do
{
printf(“%d”,a);
a++;
}while(a<50);
- 13 - By: Kute T. B. for FYIF (2007-08)
do-while flow-chart

Statements
inside the loop

true
Condition?

false

Statement-2

Statement-3

- 14 - By: Kute T. B. for FYIF (2007-08)


for loop
for(initialization;condition;incr/decrement)
{
// Loop body;
}

Example:

int a;
for(a=0;a<10;a++)
{
printf(“\n%d”,a);
}
Or
int a;
for(a=0;a<10;a++)
printf(“\n%d”,a);

- 15 - By: Kute T. B. for FYIF (2007-08)


for loop flow-chart

initialization

false
Condition?

Increment true
/decrement

Statements
inside the loop

Statement-2

Statement-3

- 16 - By: Kute T. B. for FYIF (2007-08)


Nesting of Loops
for(i=0;i<10;i++)
{
- - - - - -
- - - - - -
for(j=0;j<5;j++)
{
- - - - - -
- - - - - -
}
- - - - - -
- - - - - -
}

- 17 - By: Kute T. B. for FYIF (2007-08)


break Statement
Statement-1

false
Condition?

break true

Statements

Statement-2

Statement-3

- 18 - By: Kute T. B. for FYIF (2007-08)


Example: break statement

int a = 10;
while( a >= 0 )
{
printf(“\n a = %d”,a);
a--;
if(a==5)
break;
}

Output:
Value of a = 10
Value of a = 9
Value of a = 8
Value of a = 7
Value of a = 6

- 19 - By: Kute T. B. for FYIF (2007-08)


continue statement
Statement-1

false
Condition?

Statement-3 true

continue

Statement-2

Statement-4

Statement-5

- 20 - By: Kute T. B. for FYIF (2007-08)


Example: continue statement

int a = 6;
while( a >= 0 )
{
a--;
if(a==3)
continue;
printf(“\n a = %d”,a);
}

Output:

Value of a = 5
Value of a = 4
Value of a = 2
Value of a = 1
Value of a = 0

- 21 - By: Kute T. B. for FYIF (2007-08)

Vous aimerez peut-être aussi