0% found this document useful (0 votes)
21 views51 pages

Conditional Statements

Here is the flowchart and code to take input of n and print all numbers from 1 to n: [FLOWCHART] Start Declare integer variable n Input n Initialize counter variable i = 1 Check if i <= n Print i Increment i by 1 Increment i Check if i <= n Stop [CODE] #include <stdio.h> int main() { int n, i; printf("Enter a number: "); scanf("%d", &n); for(i=1; i<=n; i++) { printf("%d ", i); } return 0; }

Uploaded by

mahdialhasan170
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)
21 views51 pages

Conditional Statements

Here is the flowchart and code to take input of n and print all numbers from 1 to n: [FLOWCHART] Start Declare integer variable n Input n Initialize counter variable i = 1 Check if i <= n Print i Increment i by 1 Increment i Check if i <= n Stop [CODE] #include <stdio.h> int main() { int n, i; printf("Enter a number: "); scanf("%d", &n); for(i=1; i<=n; i++) { printf("%d ", i); } return 0; }

Uploaded by

mahdialhasan170
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/ 51

Conditional Statement

CSE 1111: Structured Programming Language


Charles Aunkan Gomes
Lecturer
Dept of CSE, UIU

1
Control Statement
 Control statements enable us to specify the flow of program
control; ie, the order in which the instructions in a program must
be executed.
 There are four types of control statements in C:
 Decision making statements (example – if-else statement )
 Selection statements (example – switch-case statement )
 Iteration statements (example – loop statement )
 Jump statements (example – goto, break, continue statement )
If-else Statement
 The if statement may be implemented in different forms.
1. Simple if statement
2. if…..else statement
3. elseif ladder
4. Nested if…..else statement
Flow Chart Symbols
Simple If Statement
 Syntax:  The if statement evaluates the
if(condition){ condition inside the parenthesis ().
statement 1;  If the condition is true, statements
statement 2; inside the body of if are executed.
}  If the condition is false, statements
inside the body of if are not
executed.
Simple If Statement Example
Simple If Statement Example using Flow Chart
If-else Statement
 Syntax:  The if statement evaluates the
if(condition){ condition inside the parenthesis ().
statement 1;  If the condition is true, statements
statement 2; inside the body of if are executed and
} statement inside the body of else is
skipped.
else{
statement 1;  If the condition is false, statements
statement 2; inside the body of else are executed
and statement inside the body of if is
}
skipped.
If-else Statement using Flow Chart
 The if statement evaluates the
condition inside the parenthesis ().
 If the condition is true, statements
inside the body of if are executed and
statement inside the body of else is
skipped.
 If the condition is false, statements
inside the body of else are executed
and statement inside the body of if is
skipped.
If-else Statement Example 1
 Question: Take input of a  Steps:
number and print its 1. First we have to declare a variable x
absolute value. Solve the
2. Take input of that variable x
problem using Flow Chart
first. 3. Check if it is grater than zero
4. If yes, then do nothing, just print the
value
5. If no, then multiply it by -1 and print
the value
If-else Statement Example 1 cntd.
 Steps:
1. First we have to declare a variable x
2. Take input of that variable x
3. Check if it is grater than zero
4. If yes, then do nothing, just print the
value
5. If no, then multiply it by -1 and print
the value
If-else Statement Example 1 cntd.
 Code Representation:
#include<stdio.h>
int main() {
int x;
printf("Enter an integer \n");
scanf("%d",&x);
if(x>0){
printf("Absolute value is: %d\n",x);
}
else{
x= x*(-1);
printf("Absolute value is: %d",x);
}
L
If-else Statement Example 2
 Question: Take input of  Steps:
your age and test 1. First we have to declare a variable age
whether you are eligible
2. Take input of age
for voting or not eligible.
Solve the problem using 3. Check if age is grater than equal 18
Flow Chart first. 4. If yes, then you are eligible
 Note: to be eligible for voting your 5. If no, then you are not eligible
age has to be atleast 18
If-else Statement Example 2 cntd.
 Steps:
1. First we have to declare a variable age
2. Take input of age
3. Check if age is grater than equal 18
4. If yes, then you are eligible
5. If no, then you are not eligible
If-else Statement Example 2 cntd.
 Code Representation:
#include<stdio.h>
int main() {
int age;
printf("Enter your age \n");
scanf("%d",&age);
if(age>=18){
printf("You are eligible for voting\n");
}
else{
printf("You are not eligible for voting");
}
}
elseif ladder statement
if (condition1) {  The else..if statement is
// If condition 1 is true then
// execute this and skip other if
useful when you need
} to check multiple
else if (condition2) { conditions within the
// If condition 1 is false and program.
// condition 2 is true then
// execute this and skip other if
}
else {
// If no expressions are true then
// execute this skipping all other.
}
elseif ladder statement using Flow Chart
if (condition1) {
// If condition 1 is true then
// execute this and skip other if
}
else if (condition2) {
// If condition 1 is false and
// condition 2 is true then
// execute this and skip other if
}
else if(condition3){
// If condition 1 and 2 is false and
// condition 3 is true then
// execute this and skip other if
}
else
// If no expressions are true then
// execute this skipping all other.
}
elseif ladder statement Example 1
 Question: Take input of a  Steps:
number and test whether 1. First we have to declare a variable
it is a positive number or
2. Take input of that variable
negative number. Solve
the problem using Flow 3. Check if it is grater than zero
Chart first. 4. If yes, then positive number
5. If no, then check if it is less than zero
1. If yes, then negative number
2. If no, then the number is zero
elseif ladder statement Example 1
 Steps:
1. First we have to declare a variable
2. Take input of that variable
3. Check if it is grater than zero
4. If yes, then positive number
5. If no, then check if it is less than zero
1. If yes, then negative number
2. If no, then the number is zero
elseif ladder statement Example 1
 Code Representation:
#include<stdio.h>
int main() {
int x;
printf("Enter an integer \n");
scanf("%d",&x);
if(x>0){
printf("x is a positive number\n");
}
else if(x<0){
printf("x is a negative number");
}
else{
printf(“x is equal to zero”);
}
}
elseif ladder statement Example 2
Question: Take input of your  Steps:
1. First we have to declare a variable marks
marks in CSE 281 and print your
2. Take input of marks
grade. Solve the problem using Flow 3. Check if marks in various ranges
Chart first. grading system is given
below:
Marks Grade
grater than or equal 80 A
60 to 79 B
40 to 59 C
Below 40 F
elseif ladder statement Example 2
elseif ladder statement Example 2
Code Representation:
#include<stdio.h>
int main() {
int marks;
printf("Enter your marks \n");
scanf("%d",&marks);
if(marks>=80){
printf("Your grade is:A\n");
}
else if(marks>=60 && marks<80){
printf("Your grade is:B\n");
}
else if(marks>=40 && marks<60){
printf("Your grade is:C\n");
}
else if(marks<40){
printf("Your grade is:F\n");
}
}
Task(Draw flow chart & then write code)
1. Take input of a number and test whether it is an odd number or even
number
2. Take input of a number and print the absolute value of it
3. Take input of 2 numbers and find the maximum number
4. Take input of two integer numbers a, b and show whether a is divisible
by b or not.
5. Take input of a character and print whether it is uppercase or lowercase
6. Take input of two numbers a, b; and print if a is equal to b or a grater
than b or a less than b
NESTEDIF-ELSESTATEMENT
 When an if else statement is
present inside the body of
another “if” or “else” then
this is called nested if else.
 We can use this technique
when we need to check
further conditions after
checking a condition.
NESTEDIF-ELSESTATEMENTFLOWCHART
NESTEDIF-ELSEEXECUTIONSEQUENCE
 This condition will be evaluated
first.
 If the condition is false, statements
inside the body of else are
executed
NESTEDIF-ELSEEXECUTIONSEQUENCE
 If the condition is true, statements
inside the body of if are executed
 Then this condition2 will be
evaluated.
 If condition2 is true, statements
inside the body of if are executed
 If condition2 is false, statements
inside the body of else are
executed
NESTEDIFELSE
if(p)
if(q)
printf(“p and q are true”);
else
printf(“To which if statement does this else belong to?”);

Ans –
An else always associates with the nearest if in the same block that
does not already have an else associated with it.
NESTEDIF ELSEEXAMPLE1
int main(){
int a;
printf(“enter the number:");
scanf("%d”,&a);
if(a>0){
printf("%d is positive number",a);
}
else{
if(a<0){
printf("%d is negative number",a);}
else{
printf("%d is equal to zero",a);}

}
}
NESTEDIF ELSEEXAMPLE2
int main(){ if(a>b){
int a,b,c; if(a>c){
printf(“enter three numbers:"); printf("%d is greater",a);}
scanf("%d%d%d",&a,&b,&c); else{
printf("%d is greater",c);}
}
else{
if(b>c){
printf("%d is greater", b);}
else{
printf("%d is greater",c);}

}
}
Switch-Case Statement
 The switch statement tests the value of the given variable against
the list of case values
 when a match is found, a block of statements associated with that
case is executed.
What Does a Break Statement Do?
When a break statement is encountered inside a loop/switch-case block, the loop/switch-
case block is immediately terminated and the program control resumes at the next
statement after the loop/switch-case block
Switch-Case Syntax
switch(expresssion) The expression will be evaluated.
{ case value_1: If the expression returns value 1, means if
statement1; expression == value_1 then-
break;  case value_1 will be matched and
case value_2:  this block will be executed
statement2; Once break statement is encountered
statement3; the program will immediately leave the
break; switch block and resume the control to the
…………………………… next statement after switch block
default:
statement_default;
break;
}
Statement_outside switch block;
Switch-Case Syntax
switch(expresssion) The expression will be evaluated.
{ case value_1: If the expression returns value 2, means if
statement1; expression == value_2 then-
break;  case value_2 will be matched and
case value_2:  this block will be executed
statement2;  Once break statement is encountered
statement3; the program will immediately leave the
break;
switch block and resume the control to the
…………………… ……… next statement after switch block
default:
statement_default;
break;
}
Statement_outside switch block;
Switch-Case Syntax
switch(expresssion) What happens if no values are matched??
{ case value_1: -The default statement sequence is
statement1; performed.
break; What happens if “break” statement is not
case value_2: there?
statement2; - If we do not use break statement at the
statement3; end of each case, program will execute all
break; consecutive case statements until it finds
…………………………… next break statement or till the end
default: of switch case block
statement_default;
break;
}
Statement_outside switch block;
Switch-Case Statement Rules
The expression for a switch statement must always return either a bool
value or a constant - one of integer type or character
 Case labels must be constants or constant expressions.
 Case labels must be unique. No two labels can have the same value.
 The break statement transfers the control out of the switch statement.
 The break statement is optional.
 The default label is optional.
 There can be at most one default label.
 The default may be placed anywhere but usually placed at the end.
 It is permitted to nest switch statements.
Switch-Case Flow Chart
Switch-Case Example 1
#include<stdio.h> switch(choice)
int main() {
{ case 1 :
int x, y, result, choice; result = x + y;
printf(“%d”, result);
printf(“Enter Choice\n1.Add 2.Sub\n”) break;
scanf(“ %d”, &choice); case 2 :
result = x - y;
printf(“Enter the numbers\n"); printf(“%d”, result);
scanf(“ %d %d”, &x, &y); break;
}
}
Switch-Case Example 2
#include<stdio.h> switch(choice){
int main() case 1 :
result = x + y;
{ printf(“%d”, result);
int x, y, result, choice; break;
case 2 :
printf(“1.Add 2.Sub 3. Multiply\n”); result = x - y;
scanf(“ %d”, &choice); printf(“%d”, result);
break;
case 3 :
printf(“Enter the numbers\n"); result = x * y;
scanf(“ %d %d”, &x, &y); printf(“%d”, result);
break;

}
}
Switch-Case Example 3
#include<stdio.h> switch(choice){
int main() case ‘a’ :
result = x + y;
{ printf(“%d”, result);
int x, y, result; break;
char choice; case ‘s’ :
result = x - y;
printf(“a.Add s.Sub m. Multiply\n”); printf(“%d”, result);
break;
scanf(“ %ch”, &choice); case ‘m’ :
result = x * y;
printf(“Enter the numbers\n"); printf(“%d”, result);
scanf(“ %d %d”, &x, &y); break;

}
}
Solving Previous Problems with Switch-Case
Problems that can be solved by if-else statements, can also be
solved by switch-case statements.
Let’s solve the previous slide’s if-else problems with switch-case
now.
Switch-Case Example 4
switch(i>10){
case 1 :
printf(“You followed the instruction correctly ”);
break;

}
Switch-Case Example 5
#include<stdio.h> switch(age>18){
int main() { case 1 :
int age; printf(“You are eligible for voting ”);
printf("Enter your age \n"); break;
scanf("%d",&age); case 0 :
if(age>=18){ printf(“You are not eligible for voting ”);
printf("You are eligible for voting\n"); break;
}
else{
printf("You are not eligible for voting"); }
}
}
Ternary Operator
? : Operator or Ternary Operator

 The ? : operator is just like an if ... else statement


Every problem we solve using if-else statement, can be solved by using
ternary operators
? : is a ternary operator, so it takes three values, this is the only ternary
operator C has.

 if condition is true ? then X return value : otherwise Y value;


Ternary Operator Syntax

 condition? If true then return X : if false return Y;

Part 1 Part 2 Part 3

 Example:

(var%2)==0? printf("even number") : printf ("odd number") ;


Jump Statement
 goto is a jumping statement in c
language, which transfer the
program’s control from one
statement to another statement
 The goto requires a label in order to
identify the place where the branch
is to be made.
 A label is any valid variable name
and must be followed by a colon.
Jump Statement Example 1
#include <stdio.h>
 /* To read and print the int main() {
number, if number is int number;
positive only*/
printf("Enter an integer number: ");
scanf("%d",&number);

if(number<=0) goto exit;


printf("Number is : %d", number);

exit: printf(“program ends!!!");


Jump Statement Example 2(Multiple Jump)

See how we can use “goto” to change the execution


sequence of the program. This can be useful in many cases.
Task(Solve these problems using switch-case)
1. Take input of a number and test whether it is an odd number or even number
2. Take input of a number and print the absolute value of it
3. Take input of 2 numbers and find the maximum number
4. Take input of 3 numbers and find the maximum and minimum number
5. Take input of two integer numbers a, b and show whether a is divisible by b or
not.
6. Take input of a character and print whether it is uppercase or lowercase
7. Take input of two numbers a, b; and print if a is equal to b or a grater than b or a
less than b
Task(Solve these problems using ternary operator)
8. Take input of a number and test whether it is an odd number or even number
9.Take input of a number and print the absolute value of it
10.Take input of 2 numbers and find the maximum number
11. Take input of two integer numbers a, b and show whether a is divisible by b or not.
12. Take input of a character and print whether it is uppercase or lowercase
13. Take input of two numbers a, b; and print if a is equal to b or a grater than b or a
less than b

You might also like