Lesson 5-C Lang - Control Statements - Part 1
Lesson 5-C Lang - Control Statements - Part 1
C structure
language
C statem
languagent
e
• In C language, a statement is one or more lines of code ending
with a semi-colon (;).
printf(Hello, World!”);
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");
}
}