simple control structures if, if-else statements in c

10
Simple Control Simple Control Structures Structures IF, IF-ELSE statements in IF, IF-ELSE statements in C C

Upload: arlene-may

Post on 03-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simple Control Structures IF, IF-ELSE statements in C

Simple Control Simple Control StructuresStructures

IF, IF-ELSE statements in CIF, IF-ELSE statements in C

Page 2: Simple Control Structures IF, IF-ELSE statements in C

The The ifif statement statement The The ifif statement: statement:

if (expression){if (expression){statement;statement;

}} How it works:How it works:

The expression is evaluatedThe expression is evaluated If the expression is TRUE the statement If the expression is TRUE the statement

are executed and program continues to are executed and program continues to the next statement after IFthe next statement after IF

If the expression is FALSE, program If the expression is FALSE, program continues to the next statement after IF continues to the next statement after IF

Page 3: Simple Control Structures IF, IF-ELSE statements in C

Flowchart for the Flowchart for the if if statementstatement

expr statementtrue

false

Page 4: Simple Control Structures IF, IF-ELSE statements in C

The The if-elseif-else statement statement

The The if-else if-else statement:statement:if (expression) {if (expression) {

statement1;statement1;}}else {else {

statement2;statement2;}}

Page 5: Simple Control Structures IF, IF-ELSE statements in C

IF-ELSE works as followsIF-ELSE works as follows

Expression is evaluatedExpression is evaluated If the expression is TRUE, If the expression is TRUE,

statement1 is executed and program statement1 is executed and program continues to the next statement after continues to the next statement after if-elseif-else

If the expression is FALSE, If the expression is FALSE, statement2 is executes and program statement2 is executes and program continues to the next statement after continues to the next statement after if-elseif-else

Page 6: Simple Control Structures IF, IF-ELSE statements in C

Flowchart for the if-else Flowchart for the if-else statementstatement

exprtrue

statement1 statement2

false

Page 7: Simple Control Structures IF, IF-ELSE statements in C

SYNTAX ROOLES and SYNTAX ROOLES and INDENTATIONINDENTATION

If the statement in IF statement is a If the statement in IF statement is a single statement, the curly braces single statement, the curly braces could be omittedcould be omitted

If the statement1 or/and statement2 If the statement1 or/and statement2 is/are singe statements, the curly is/are singe statements, the curly braces could be omittedbraces could be omitted

INDENTATIONINDENTATION The statement part of the if, if-else The statement part of the if, if-else

statement should be shiftedstatement should be shifted

Page 8: Simple Control Structures IF, IF-ELSE statements in C

EXAMPLESEXAMPLES What would be the output of the following What would be the output of the following

programming fragment?programming fragment? int a = 10;int a = 10; if (a >= 10){if (a >= 10){

a = a + 1;a = a + 1;printf(“%d\n”, a);printf(“%d\n”, a);

}}a = 0;a = 0;

printf(“%d\n”, a);printf(“%d\n”, a);

Page 9: Simple Control Structures IF, IF-ELSE statements in C

EXAMPLESEXAMPLES What would be the output of the following What would be the output of the following

programming fragment?programming fragment? int a = 9;int a = 9; if (a >= 10){if (a >= 10){

a = a + 1;a = a + 1;printf(“%d\n”, a);printf(“%d\n”, a);

}}a = 0;a = 0;

printf(“%d\n”, a);printf(“%d\n”, a);

Page 10: Simple Control Structures IF, IF-ELSE statements in C

EXAMPLESEXAMPLES

What would be the output of the What would be the output of the following programming fragment?following programming fragment? int a = 10;int a = 10; if (a >= 10)if (a >= 10)

a = a + 1;a = a + 1; else else

a = 0;a = 0; printf(“%d\n”, a);printf(“%d\n”, a);