0% found this document useful (0 votes)
126 views3 pages

Decision Making Statements

The document discusses different forms of decision making statements in programming using if statements, including: 1) Simple if statements that execute code if an expression is true or else skip it 2) if-else statements that execute one block of code if true and another block if false 3) Nested if-else statements that allow for additional conditional logic within other conditional statements 4) Switch statements that choose one block of code to execute based on an integral expression matching different case values The rules for switch statements require the expression to evaluate to an integer and case labels to be unique integers ending with a colon.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views3 pages

Decision Making Statements

The document discusses different forms of decision making statements in programming using if statements, including: 1) Simple if statements that execute code if an expression is true or else skip it 2) if-else statements that execute one block of code if true and another block if false 3) Nested if-else statements that allow for additional conditional logic within other conditional statements 4) Switch statements that choose one block of code to execute based on an integral expression matching different case values The rules for switch statements require the expression to evaluate to an integer and case labels to be unique integers ending with a colon.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Decision making statements with if statement

The if statement may be implemented in different forms depending on the complexity of conditions to
be tested. The different forms are,

1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. switch statement
1. Simple if statement

The general form of a simple if statement is,

if(expression)
{
statement inside;
}
statement outside;
If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is
skipped and only the statement-outside is executed

2. if...else statement

The general form of a simple if...else statement is,

if(expression)
{
statement block1;
}
else
{
statement block2;
}
If the expression is true, the statement-block1 is executed, else statement-block1 is skipped
and statement-block2 is executed.

3. Nested if....else statement

The general form of a nested if...else statement is,

if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
if expression is false then statement-block3 will be executed, otherwise the execution continues and
enters inside the first if to perform the check for the next if block, where if expression 1 is true
the statement-block1 is executed otherwise statement-block2 is executed.

4. Switch statement

Switch statement is a control statement that allows us to choose only one choice among the many given
choices. The expression in switch evaluates to return an integral value, which is then compared to the
values present in different cases. It executes that block of code which matches the case value. If there is
no match, then default block is executed(if present). The general form of switch statement is,

switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}

Rules for using switch statement

The expression (after switch keyword) must yield an integer value i.e the expression should be an
integer or a variable or an expression that evaluates to an integer.

The case label values must be unique.

The case label must end with a colon(:)

The next line, after the case statement, can be any valid C statement.

You might also like