Condition in c

advertisement
1
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Chapter No: 03
Programming
Fundamentals
INSTRUCTOR:
ILTAF MEHDI
(MCS, MCSE, CCNA, Web Developer)
2
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Course Contents
3
Chapter No
Chapter Name
Page No
1.
The Turbo C Programming Environment
01
2.
C Building Blocks
27
3.
Decisions
99
4.
Loops
67
5.
Functions
135
6.
Arrays and Strings
179
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Chapter No 03 Detail:
 The if statement
 The if-else Statement
 The else-if statement
 The switch statement
 The conditional expression operator
4
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Conditional Structure
 The computer can perform arithmetic calculations correctly and
rapidly. Besides these capabilities the computer is also capable of
quick decision-making and repetitions.
 A statement or set of statements that executed when a particular
condition is true and ignored when the condition is false is called
conditional statement.
 The computer tests a condition and selects one of the two alternatives
actions depending on whether the condition is true or false.
 C/C++ has five decision making statements:
 if statement
 if-else statement
 The else-if statement
 The switch statement and
 The conditional expression operator.
5
BY ILTAF MEHDI(MCS,MCSE, CCNA)
If Statement
 The fundamental decision making statement in C/C++ is the if statement.
 This statement is also called as conditional jump statement or conditional transfer of control
statement, because it is mostly used to transfer the control to another statement if and only if a
condition is true. If the condition is not fulfilled i.e. if the condition is false, transfer of control does
not occur and the program execution moves on to the next statement following the if structure.

Syntax: if(condition)
Statement;
or
if(condition)
{ statement 1;
statement 2;
}
<?php
$satisfied = "very";
if ( $satisfied == "very" )
{
print "We are pleased that you are happy with our service";
// register customer satisfaction in some way
}
6
?>
BY ILTAF MEHDI(MCS,MCSE, CCNA)
It is if-statement
example used in PHP
The if Statement
 The
basic if statement
allows your program to
execute a single statement,
or a block of statements
enclosed between braces, if
a given condition is true.
 Example
int a = 0, b=1;
if (a < b)
printf(“Hello”);
No
Condition
is true?
Yes
Statement
or Block of
Statements
Next
Statement
7
BY ILTAF MEHDI(MCS,MCSE, CCNA)
C-program of a simple if statement
8
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int no;
printf(“enter any number?”);
scanf(“%d”, &no);
if(no>10)
{
printf(“ the value you entered is above 10”);
printf(“its ok “);
}
If (no<10)
printf(“below 10 number is entered”);
getch();
}
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Making a decision
C-program for the use of a conditional if statement.
#include <stdio.h>
#include <conio.h>
void main(void)
{ clrscr();
int val;
printf(“Enter a value between 50 and 100”);
scanf(“%d”, &val);
if(val < 50)
printf(“Wrong value entered, below 50”);
if (val > 100)
printf( “Wrong Value entered above 100”);
if (val>=50 && val <=100)
printf( “You have entered the correct value:%d“,val);
getch();
}
9
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Q: Get a value from the keyboard then find
that the value is positive or negative
10
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int number;
printf(“Enter a number on your choice ”);
scanf(“%d”, &number);
if(number < 0)
printf(“the number you entered is negative”);
if (number > 0)
printf(“the number you entered is positive”);
if (number == 0)
printf(“you enter the number equal to 0”);
printf(“You have entered :%d“, number);
getch();
BY ILTAF MEHDI(MCS,MCSE, CCNA)
}
The if-else Statement
 The basic if-else statement allows your
program to execute a single statement,
or a block of statements enclosed
between braces, if a given condition is
true, and another statement or block of
statements if the condition is false.
 Example
int a = 0, b=1;
if (a < b)
printf(“ a is less than b”);
else
printf(“a is greater than or equal to b”);
if (expression)
{ // code to execute if the expression evaluates to true
}
else
{
// code to execute in all other cases
}
11
No
Yes
Statement
or Block of
Statements
for false
if-else example
in PHP
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Condition
is true?
Statement
or Block of
Statements
for true
Next
Statement
Syntax of if-else Statement
It has two syntaxes:
i. if(condition)
statement;
else
statement;
12
BY ILTAF MEHDI(MCS,MCSE, CCNA)
ii. If(condition)
{
statement 1;
statement 2;
}
else
{
statement1;
statement2;
}
Create a program in C that get two values from the
keyboard and then show the greatest value.
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int val1, val2;
printf(“Enter the first value: ”);
scanf(“%d”, &val1);
printf( “Enter the second value: ”);
scanf(“%d”, &val2);
if(val1>val2)
printf( “value 1 is greater than the value 2”);
else
printf(“value 2 is greater than value 1”);
printf( “the above is the result “);
getch();
}
13
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Else-if Condition
Purpose:  This condition is also called
multiple-if condition. in this if
condition the first condition is
checked if that’s true then execute
the entire statement in a specific
block if false then next condition
will be checked and so on. In case
when all the conditions are fails then
only else statement will be
executed.
14
BY ILTAF MEHDI(MCS,MCSE, CCNA)
True
Condition
Block
False
True
Condition
False
Statement
after if-else
structure
Block
Syntax for else-if
if (condition)
{
c statemetn1;
}
else if (condition)
{
c statement ;
else if (condition)
{
c statement ;
}
else
{
c statement ;
}
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Example
if ( expression )
{
// code to execute if the expression evaluates to true
}
else if ( another expression )
{
// code to execute if the previous expression failed
// and this one evaluates to true
}
else
{
// code to execute in all other cases
}
15
Write
a program that input a student marks then fine out the proper grade according
to the Score as >=90—A, 80-89B, 70-79C, 60-69D, Below-60Fail
#include <stdio.h>
#include <conio.h>
void main(void)
{ clrscr();
int marks;
printf(“Enter your marks”);
scanf(“%d”, & marks);
if(marks >= 90)
printf(“ your grade is A “);
else if (marks >=80)
printf(“your grade is B“ );
else if (marks >=70)
printf(“ your grade is C“);
else if (marks >=60)
printf(“ your grade is D“);
else
printf(“ you are Fail” );
getch();
}
16
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Create a C-program that compare three values then find out these values are
equal or not equal .
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
int a,b,c;
printf(“Enter first value ”);
scanf(“%d”, &a);
printf(“ enter second value”);
scanf(“%d”, &b);
printf(“ enter third value”);
scanf(“%d”, &c) ;
if(a ==b)
{
if (a==c)
printf(“ all the numbers are equal ”);
}
else
printf(“ the values are different ”);
getch();
}
17
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Write a c-program that get a character from the keyboard and
then display whether it is vowel or not by using logical operator OR.
#include <stdio.h>
#include <conio.h>
void main(void)
{
clrscr();
char ch;
printf(“Enter a character ”);
scanf(“%c”, &ch);
if(ch==‘a’ || ch==‘A’ || ch ==‘e’ || ch==‘E’ || ch==‘i’ || ch==‘I’ || ch==‘o’ || ch== ‘O’
|| ch == ‘u’ || ch == ‘U’)
printf(“ the character you entered is VOWEL”);
else
printf(“the character you entered is not a VOWEL”);
getch();
}
18
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Switch Statement
 The switch statement is another
conditional structure.
 It is good alternative of nested elseif statement.
 It can be used easily when there are
many choices available and only one
should be executed. The nested if
becomes very difficult in such
situation.
switch (expression)
{
case result1:
// execute this if expression results in
result1
break;
case result2:
// execute this if expression results in
result2
break;
default:
// execute this if no break statement
// has been encountered
}
The break Statement:
The break statement is mostly used with switch statement and loops.
The break Statement is used to exit from a switch statement or a loop.
19
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Working of the switch statement
It works in the following way:
 switch evaluates expression and checks if it is equivalent to
constant1, if it is, it executes group of statements 1 until it finds
the break statement. When it finds this break statement the
program jumps to the end of the switch selective structure.
 If expression was not equal to constant1 it will be checked against
constant2. If it is equal to this, it will execute group of statements
2 until a break keyword is found, and then will jump to the end of
the switch selective structure.
 Finally, if the value of expression did not match any of the
previously specified constants (you can include as many case labels
as values you want to check), the program will execute the
statements included after the default: label, if it exists (since it is
optional).
20
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Syntax of the Switch Statement
Enables you to select from multiple choices (called
cases).
 Syntax
switch(expression)
{
case val1 :
stat1;
stat2;
……
break;
case val2 :
stat1;
stat2;
……
break;
……………….
default:
stat1;
stat2;
……
}

21
BY ILTAF MEHDI(MCS,MCSE, CCNA)
c1
Statement
B
c2
statement
B
c3
Statement
B
Next Stat
Write a program in C to show the use of switch statement.
#include<stdio.h>
#include<conio.h>
void main (void)
{
clrscr();
int code;
printf("enter any choice from 1 to 3:");
scanf("%d", &code);
switch(code)
{
case 1:
printf("\n **I am in Kabul**");
break;
case 2:
printf("\n **I am in Herat**");
break;
case 3:
printf("\n **I am in the Mazar Sharif**");
break;
default:
printf("\n **No case is correct**");
}
printf("\n\n **end of the prog**");
getch();
}
22
BY ILTAF MEHDI(MCS,MCSE,CCNA)
The conditional expression operator
OR
The ? operator
 The ? (ternary condition) operator is a more efficient form for
expressing simple if statements. It has the following form:
expression1 ? Expression2 : expression3
 It simply states:
if expression1 then expression2 else expression3
 For example to assign the maximum of a and b to z:
z = (a>b) ? a : b;
which is the same as:
if (a>b)
z = a;
else
z=b;
23
BY ILTAF MEHDI(MCS,MCSE, CCNA)
Download