C Lang - Control Statements
C Lang - Control Statements
C structure
language
C statem
languag ent
e
• In C language, a statement is one or more lines of code ending
with a semi-colon (;).
printf(Hello, World!”);
int main() {
printf(“Hello, ”);
printf(“World!”);
}
Contr struct
ol ure
• Control structures allow to change the ordering of
how the statements in a program is executed
• Types of control structures:
• Decision control structures
• allow to select specific sections of code to be executed
• Repetition control structures
• allow to execute specific sections of the code a number of times
Contr struct
ol ure
• C language statements that allows a programmer to select and execute
specific blocks of code while skipping other sections.
if (expression)
{
statement1;
statement2;
…
}
structu
if
re
Every time C language encounters an if statement,
NO Boolean YES
expression
structu
if
re
If Structure: Example 1
• Suppose that the passing grade on an examination is 75 (out of 100). Then the if
statement may be written in Java as:
if (expression)
statement1;
else
statement2;
or
if (expression) statement1;
else statement2;
else
if
structure
• As in the if statement, compound statements may also be used in if-else statements (provided they
are grouped together by braces).
if (expression)
{
statement1;
statement2;
…
}
else
{
statement3;
statement4;
…
}
else
if
structure
• Making a decision involves choosing between two alternative courses of action based on some value
within a program.
NO Boolean YES
expression
else
if
structure
If-Else Structure: Example 1
int grade = 68;
if (grade > 60)
printf(“Congratulations!”);
else
printf(“Sorry you failed!”);
else
if
structure
If-Else Structure: Example 2
int grade = 68;
if (grade > 60) {
printf(“Congratulations!”);
printf(“You Passed!”);
}
else {
printf(“Sorry you failed!”);
}
else
if
structure
If-Else Structure: Example 3
int choice = 0;
if (choice == 1)
{
num = 1;
num2 = 10;
}
else
{
num = 2;
num2 = 20;
}
ercises
Ex
:
1. Test if the value of the variable number is an odd number. If it is, print “This number
is not an even number” otherwise, print “This number is an odd number.”
neste if
d structure
neste if
d structure
• A nested if is an if statement that is the target of another if or else. Nested if
statements means an if statement inside an if statement. Yes, C language allows us
to nest if statements within if statements. i.e, we can place an if statement inside
another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
neste if
d structure
#include<stdio.h>
Int main()
{
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf ("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
printf("i is smaller than 12 too");
else
printf ("i is greater than 15");
}
}
}
else- if
if
structure
• user can decide among multiple
options. The if statements are
executed from the top down. As soon
as one of the conditions controlling
the if is true, the statement associated
with that if is executed, and the rest of
the ladder is bypassed. If none of the
conditions is true, then the final else
statement will be executed.
else- if
if
structure
if (condition)
statement;
else if
(condition)
statement; . .
else statement;
else- if
if
structure
#include<stdio.h>
Int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf(("i is not present");
}
}
Switc
Case structure
h
• The switch statement is a
multiway branch statement. It
provides an easy way to
dispatch execution to different
parts of code based on the value
of the expression.
Switc
Case structure
h • Unlike with the if statement, the multiple
• Every time C language statements are executed in the switch
encounters a switch statement statement without needing the curly
1. It evaluates the switch expression, and braces.
jumps to the case whose selector
matches the value of the expression. • When a case in a switch statement has
been matched, all the statements
2. The program executes the statements associated with that case are executed.
in order from that point on until a Not only that, the statements associated
break statement is encountered, with the succeeding cases are also
skipping then to the first statement executed.
after the end of the switch structure.
• To prevent the program from executing
3. If none of the cases are satisfied, the statements in the subsequent cases, use a
default block is executed. However, break statement as the last statement.
note that the default part is optional.
Switc
Case structure
h #include <stdio.h
int main()
• Expression can be of type byte, short, {
int char or an enumeration. int i = 9;
switch (i)
• Duplicate case values are not allowed. {
• The default statement is optional. case 0:
printf("i is zero.");
• The break statement is used inside the break;
switch to terminate a statement case 1:
printf("i is one.");
sequence. break;
• The break statement is optional. If case 2:
printf ("i is two.");
omitted, execution will continue on break;
into the next case. default:
printf ("i is greater than
2.");
}
}
}
Switc
Case structure
h
int grade = 92; #include<stdio.h>
int main()
switch (grade) { {
case 100: int i = 9;
switch (i)
printf(“Excellent!”);
{
break; case 0:
case 90: printf("i is zero.");
printf(“Good!”); break;
break; case 1:
case 80: printf("i is one.");
printf(“Study Harder!”); break;
break; case 2:
default: printf ("i is two.");
printf(“You failed!”); break;
} default:
printf("i is greater than
2.");
}
}
}