5. C_ControlStatements_Selection_1101

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Control Statements

Structured Programming Language (CSE-1271)


Course Instructor : Mohammed Mamun Hossain
Assistant Professor, Dept. of CSE, BAUST
Outline
1. Control Statements
2. if statements
3. if-else statements
4. Switch statements
5. Goto Statements
6. Conditional Operator (Statements)
Control Statements
Let see a C program

Selection OR

Many time
Repetition
Control Statements
Let see a C program
Control Statements
Let see a C program
Sequential statements

Selection statements

Loop statements
Control Statements
Control the flow of execution in a program or function.
There are three kinds of execution flow:

 Sequence: The execution of the program is sequential.

 Selection: A control structure which chooses alternative to execute.

 Repetition: A control structure which repeats a group of


statements.
if Statements
 One of C’s selection statement.
 Sometimes called conditional statements
 Itsoperation is governed by the outcome of a conditional test evaluates to
either true or false.
 Inits simplest form, the if statement allows our program to conditionally
execute a statement.
if Statements
Simplest form of if statement:
if(expression)
if(expression) {
statement; statement;
}

 For multiple statements:


if(expression)
{
statement 1;
statement 2;
……
statement n;
}
if Statements
 The expression may be any valid C expression.

 If the expression evaluated as true, the statement will be executed.

 If it does not - the statement is bypassed and the line of code following
the if is executed.
if Statements
 The expression may be any valid C expression.

 If the expression evaluated as true, the statement will be executed.

 If it does not - the statement is bypassed and the line of code following
the if is executed.
if Statements
In C, an expression is true if it evaluates to any nonzero values
(5,9,-4,100 etc).
If it evaluate to zero, it is false.
int a=10, b=20; Output
if(a<b)
printf(“This line will print.”); This line will print.

int a=10, b=20; Output


if(a>b)
printf(“This line will print.”);

int a=10, b=20; Output


if(0)
printf(“This line will print.”);
if Statements
if Output
int a=10, b=20;
printf(“%d”, a<=b); 1

int a=10, b=20; Output


printf(“%d”, a>b);
0
int a=10, b=20; Output
if(a)
This line will print.
printf(“This line will print.”);

int a=-10, b=20; Output


if(a)
printf(“This line will print.”); This line will print.
If-else Statements
We can add else statement to the if.
Then the if statement looks like this:

if(expression)
statement1;
else
statement2;
If-else Statements
Structure of simple if-else is:

True
if(expression)
{
statements
}
else OR
{
statements
}
If-else Statements
Structure of simple if-else is:

False
if(expression)
{
statements
}
else OR
{
statements
}
If-else Statements
Structure of nested if-else is:
if(expression)
{
statements
}
else if(expression) OR
{
statements
}
...
else OR
{
statements
}
Switch Statements
If is good for choosing between two alternatives
When several alternatives are needed we should use switch
statement.
switch is C’s multiple selection statement.
Use to select one of several alternative paths in program
execution
Switch Statements
switch
A value is successively tested
against a list of integer or
character constants.

When the match is found, the


statement sequence associated
with that match is executed.

Statement sequence are not


blocks, not use curly braces
Switch Statements
switch
Switch Statements
Nested switch:
If vs Switch

switch can only test for equality, where the if conditional


expression can be of any type
switch will work with only int or char types. We can’t use
float or others.
Goto Statement
When compiler encounters goto statement in a C program, the
control jumps to the corresponding label mentioned along with
Goto Statement
Goto example
Conditional Operator (Statements)
The conditional operator ? :
A conditional expression is written in the form

expression 1 ? expression 2 : expression 3

True or False? True False


Conditional Operator (Statements)
conditional operator

(a+b)>=13 ? a = 100 : a = 1000 Now a is 100

True or False? True


Conditional Operator (Statements)
conditional operator

i = (a+b)<13 ? 100 : 1000 Now i is 1000


False
True or False?
Thank You.
Questions and Answer
References
Books:
1. Programming With C. By Byron Gottfried
2. The Complete Reference C. By Herbert Shield
3. Programming in ANSI C By E. Balagurusamy
4. Teach yourself C. By Herbert Shield

Web:
1. www.wikbooks.org
and other slide, books and web search.

You might also like