UNit 2 Conditional Operator and Control Statements
UNit 2 Conditional Operator and Control Statements
Session 2022-23
Conditional Operator
C offers a special type of operator for decision making, also known as ternary operator which
is the conditional operator ( ?: in combination) to construct conditional expressions.
Operator Description
? : Conditional Expression
If an expression contains a condition
-relational operators
-logical operator
Conditional operators return
one value if condition is true and
returns another value is condition is false.
include <stdio.h>
int main()
{
int a, b, max ;
printf(“\n Enter any Two numbers”);
scanf(“%d%d”, &a, &b);
max = ( a>b ?a :b ) ;
printf("Max value is %d\n", max);
}
include <stdio.h>
int main()
{
int a, b, c, max ;
printf("\n Enter any Three numbers");
scanf("%d%d%d", &a, &b,&c);
max = ( (a>b&&a>c)?a:(b>c?b:c)) ;// Conditional Operator with logical &
// max = ( a>b?(a>c?a:c):(b>c?b:c));// Nested of Conditional Operator
printf("Max value is %d\n", max);
}
include <stdio.h>
int main()
{
int a, b, c, max ;
printf("\n Enter any Three numbers");
scanf("%d%d%d", &a, &b,&c);
// max = ( (a>b&&a>c)?a:(b>c?b:c)) ;// Conditional Operator with logical &
max = ( a>b?(a>c?a:c):(b>c?b:c));// Nested of Conditional Operator
printf("Max value is %d\n", max);
}
Control Statements in C
To control the Execution flow of statements in a program/programming languages uses
control statements.
1. Conditional Statements
a. Decision Making statements
Decision making where a condition id given to decide which statement or block of
statements will be executed for required actions
if Statement,
if...else statement
Nested if (If with-in if, If.. else…with-in If else)
b. Branching
Selection statement are used to make one-time decisions in C Programming, that is, to
execute some code/s and ignore some code/s depending upon the test expression.
switch statement
c. Looping (or) Iterative statements
Looping is deciding how many times to take a certain action.
for loop
do while loop
while loop
2. Unconditional jumping
goto statement (No or Low use of goto statement in structured/Modular
Programming Approach)
IF STATEMENT: In if statement, a given condition is evaluated and it executes only true
block of statement(s)
Syntax
if(condition)
True statement; //if condition is true than a statement to be executed
If(condition)
{ // if condition is true than block of statements to be executed
Statement 1;
Statement 1;
Statement 1;
…
}
Example
Age=?
if(age>=18)
printf(“ you are eligible to vote”);
Note if there is only one statement no need to use being and end ‘{‘ ‘}’ or if more
than one statements or a block of statements then {}will be used
if else statement in C programming language it is used to execute a set of statements if
condition is true and execute another set of statements when condition is false.
Only either if block or else block of code gets executed (not both) depending on the
outcome of condition.
Syntax:
if
(condition_expression)
{
statements; //code to be execute if the condition_expression is true
}
else
{
statements; //code to be execute if the condition_expression is false
}
} Inner
if else
else
{
}
}
else
{
statements; //code to be execute if the condition_expression is false
if(condition)
{ statements; //code to be execute if the condition_expression is true
} Inner else
if else
else
{
Example
Age=?
if(age>=18)
{
printf(“ you are eligible to vote”); // if condition true this statement will be executed
}
else // if condition false, this statement will be executed
{
printf(“ Sorry, you are not eligible to vote”);
}
#include <stdio.h>
int main()
{
int a, b, max;
printf("\n Enter any two numbers");
scanf("%d%d", &a, &b);
if(a>b)
max=a;
else
max=b;
printf("\n Max. out of two numbers %d",max);
}
find max out of 3 numbers
Algorithm1
Step 1: declare three variables
step 2: read the value of three variables
step 3: compare var1 and var2 and find max out of these two
step 4: compare big out of two with var3 and find max out of three
step 5: display the max value
Start
A, B, C
Read value of
A, B, and C
False
Ture
Big=B Ma=A
If A>B
False
Max=C
If max>C
Ture
Display max
Stop
Now lets try to make alteration in Algorithm
Step 1: read any three values in three variables
step 2: compare var1 and var2 find max
step 3: compare max with var3 and move bigger value to max
step 4: display the max value
Not:e Both if and else block of statements can never execute at a time.
return 0;
}
#include <stdio.h>
int main(){
char c;
printf("Enter a Character: ");
scanf("%c", &c);
Example 1
#include <stdio.h>
int main()
{
int n1, n2, result;
char ch[]="*";
printf(" \n Enter any two numbers");
scanf("%d%d",&n1,&n2);
printf(" \n Enter any operator to perform airthematic Operation +, -, *, /, ");
scanf("%c",&ch);
switch(ch)
{
case '+' : { result= n1+n2;
printf("\n Sum of Two numbers = %d", result);
break;
}
case '-' : {
result= n1-n2;
printf("\n Substraction of Two numbers = %d", result);
break;
}
case '*' : {
result= n1*n2;
printf("\n multiplication of Two numbers = %d", result);
break;
}
case '/': {
result= n1*n2;
printf("\n division = %d", result);
break;
}
default: printf("\n Case not matched ");
}
}
Example 2
#include <stdio.h>
int main()
{
int x = 2;
switch (x)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
return 0;
}
Example 3: Print Appreciation on grade entered by user
int main ()
{
char grade;
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
LOOPING (Repeated Execution- execute again and again until the given condition
becomes false )
A loop is a conditional Control statement
A Loop executes the sequence of statements as many number of times until the stated
condition becomes false.
loop consists of two parts,
a body of a loop : What to repeat { }
a control statement. Condition to begin Repetition or terminate the repetition
control statement is a combination of some conditions that direct the body of the loop
to execute until the specified condition becomes false.
The purpose of the loop is to repeat the same code a number of times.
1. Entry controlled loop (Like while loop or for loop): In an entry controlled loop, a
condition is checked before executing the body of a loop. It is also called as a pre-checking
loop.
2. Exit controlled loop (like do while loop): In an exit controlled loop, a condition is
checked after executing the body of a loop. It is also called as a post-checking loop.
Let number = 17
Table 5
5*3=15 7*
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9= 45
5*10=50
Seq 1-10
}
}
1
12
123
1234
12345
123456
**************************************************************************
*****/
#include <stdio.h>
int main()
{
int r, c;
for(r=1;r<=6;r++)
{
printf("\n");
for (c=1;c<=r;c++)
{
printf("%d ",c);
}
}
}
col1 col2
Row1- >1 2 3 4 5 6
Row2- >12345
1234
123
12
1
#include <stdio.h>
int main()
{
int r, c;
int n;
printf("\n Enter no of rows");
scanf("%d",&n);