0% found this document useful (0 votes)
9 views

05 Control Statements in C

Control statements in C are used to write powerful programs by selecting between optional sections using selection statements like if/else and switch statements, and repeating important sections using iteration statements like while, do-while, and for loops. The if/else statement and nested else-if structures allow choosing between alternative courses of action based on conditions. The switch statement provides a multi-way decision structure to select execution paths based on case values. Loops allow repeating sections of code until a condition is met.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

05 Control Statements in C

Control statements in C are used to write powerful programs by selecting between optional sections using selection statements like if/else and switch statements, and repeating important sections using iteration statements like while, do-while, and for loops. The if/else statement and nested else-if structures allow choosing between alternative courses of action based on conditions. The switch statement provides a multi-way decision structure to select execution paths based on case values. Loops allow repeating sections of code until a condition is met.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Control Statements in C

Selection statement and Iteration Statements


Control Structures/Statements
• A program consists of a number of statements which are usually
executed in sequence. Programs can be much more powerful if we
can control the order in which statements are run.

• Statements fall into three general types;


1. Assignment, where values, usually the results of calculations, are stored in
variables.
2. Input / Output, data is read in or printed out.
3. Control, the program makes a decision about what to do next.
Control Statement Continued…
Control statements in C are used Control
to write powerful programs by; statements

1. Selecting between optional Selection Iteration


sections of a program. Statements statements
2. Repeating important sections of
the program. The if else The while loop &
statement Do while loop

The switch
The for loop
statements
If Selection Statement
• Selection structure
• Choose among alternative courses of action Syntax:
if ( condition )
• Pseudocode example: {
If student’s grade is greater than or equal to 60 statement ;
}
Printf “Passed”
• If the condition is true
• Print statement executed, program continues to next statement
• If the condition is false
• Print statement ignored, program continues
If Structure
• Translation into C
If student’s grade is greater than or equal to 60 if ( grade >= 60 ) {
Print “Passed” printf("Passed“);}

• if structure
• Single-entry/single-exit A decision can be made on
any expression.
zero - false
true nonzero - true
grade >= 60 print “Passed”
Example:
3 - 4 is true

false
If-Else Structure FLOW Chart

• if
• Performs action if condition true False True
Test Condition
• if/else
• Different actions if conditions true or false
• Pseudocode Executable Y - Statement Executable X - Statement

if student’s grade is greater than or equal to 60


print “Passed”
else
print “Failed”
Syntax:
• C code if ( condition )
{
if ( grade >= 60 ) }
statement 1 ; (if the condition is true this statement will be executed)

printf("Passed“); else
{
else statement 2 ; (if the condition is false this statement will be executed)
printf("Failed“); }
Else-If Structure
• Nested if/else structures • Example
One inside another, test for multiple cases
Once condition met, other statements skipped
if ( grade >= 90 ) // 90 and above
if student’s grade is greater than or equal to 90
Print “A” printf("A“);
else else if ( grade >= 80 ) // 80-89
if student’s grade is greater than or equal to 80 pritnf("B“);
Print “B”
else
else if ( grade >= 70 ) // 70-79
if student’s grade is greater than or equal to 70 printf("C");
Print “C” else if ( grade >= 60 ) // 60-69
else
if student’s grade is greater than or equal to 60 printf("D“);
Print “D” else // less than 60
else printf("F“);
Print “F”
FALSE TRUE
Test Condition_1 Exec. Stat_1

FALSE
TRUE

Control Flow of else-if Test Condition_2 Exec. Stat_2

FALSE TRUE
Test Condition_3 Exec. Stat_3

FALSE TRUE
Test Condition_n

Exec. Stat_X Exec. Stat_n


Compound statement
• Set of statements within a pair of braces
if ( grade >= 60 )
printf("Passed\n“);
else {
printf("Failed\n“);
printf("You must take this course again\n“);
}
• Without braces,
printf("You must take this course again\n“);
always executed
• Block
Set of statements within braces
Switch Statement

• The control statements which allow us to make a decision from the


number of choices is called switch (or) Switch-case statement.

• It is a multi way decision statement, it test the given variable (or)


expression against a list of case value.

• The switch statement is very useful while writing menu driven


programs.
TRUE
EXPR==VALUE_1 Starts execution from this case

Switch Case Statement

FALSE
Syntax and Flow Chart
TRUE
EXPR==VALUE_2 Starts execution from this case

Syntax

FALSE
Switch ( expression )
{
TRUE
EXPR==VALUE_3 Starts execution from this case
case value_1: set of statements;
case value_2: Setof Statements;

FALSE
case value_3: Set of Statements;
default: Set of Statements; Flow Chart
} EXPR==VALUE_1

Starts execution from default case


Rules for Switch Statement
The expression in the switch statement must be an integer or character constant.
No real numbers are used in an expression.
The default is optional and can be placed anywhere, but usually placed at end.
The case keyword must be terminated with colon (:);
No two case constant are identical.
The values of switch expression is compared with case constant in the order
specified i.e from top to bottom.
The compound statements are no need to enclose within pair of braces.
Integer Expression used in different case statements can be specified in any order.

You might also like