Beruflich Dokumente
Kultur Dokumente
DECISIONS! DECISIONS!
Instructions were executed sequentially so far Structures other than sequential structure Decision Control Structure
You may want to execute an instruction in one situation and a different instruction for another situation E.g. A/B where B should not be 0 In short, only divide the two variables if B is not 0
CONTROL STRUCTURES
Control structure enable us to specify the order in which the various instructions in a program are to be executed. In short, they determine the flow of control The three types of control structures are given below
Sequence Control Instruction Instructions are executed in order Decision/Selection Control Instruction Instructions execute if they meet the conditions Repetition/Loop Control Instruction The execution of instructions are repeated
If Statement If-else statement Nested if-else statements Else if statements Case control
THE IF STATEMENT
if keyword used for decision control instruction if(this condition is true) execute this statement;
If the condition within the parenthesis is true then the statement is executed Otherwise, the compiler will skip past it
CONDITIONAL EXPRESSIONS
The conditional expression are used with if They are constructed using relational operators
x>y
x<=y x>=y
x is greater than y
x is less than and equal to y x is greater than and equal to y
EXERCISE
SOLUTION
true false false true false false true Error, the "not less than" is not a valid operator.
CAUTION!!!!!!!!!!!
Int x=3, y=3, z=4; (1)X=Y assigns Ys value to X (2)X==Y compares the two values (3)X==Z compares the two values The == comparison returns either true or false
if conditions will be satisfied every time. All assignment operations like X=Y are considered as true. All values other than zero are considered to be true
FLOW CHARTS
A graphic representation of an algorithm, often used in the design phase of programming to work out the logical flow of a program. Flow Charts are used to help programmers in the early stages of programming. A flow chart is a chart that flows from one stage to the next and it will show what stage or event is first, second, third etc...
FLOWCHART EXAMPLE
EXERCISE
Draw a flow chart and write a program for the problem given below While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboards, write a program to calculate the total expenses
FLOW CHART
PROGRAM
#include<stdio.h> void main(void) { int Discount=0, Quantity; float Rate, Total; scanf(%d %f, &Quantity, &Rate); if(quantity>1000) Discount=10;
Total= Quantity*Rate (Discount/100 * Rate * Quantity) printf(Total Expenses =%f,Total); }
The else statement allows us to evaluate instructions or set of instructions if the expression is false If(expression) instruction; else instruction;
EXAMPLE
Write a program that tells takes an integer as input and tells us whether the number entered is odd or even. #include<stdio.h> void main(void) { int a; scanf(%d, &a); if((a%2)==0) printf(Number entered is even); else printf(Number entered is odd);
FLOW CHART
of statements after the if up to else is called an if block. Similarly, statements after else are in the else block part always comes after an if expression an if block has only one statement then braces can be ignored. Same goes for the else block
Else
If
// if block starts
NESTED IF-ELSES
You can define an entire if-else construct within either the body of the if statement or the body of an else statement. E.g. If(expression1) statement; else { if(expression2) statement; else statement; }
PROBLEM
The Marks obtained by a student in 5 different subjects are input through the keyboard. The student gets division as per the following rules
Percentage above or equal to 60 is 1st Division Percentage between 50 and 59 is 2nd Division Percentage between 40 and 49 is 3rd Division Percentage less than 40 is Fail
SOLUTION
#include<stdio.h> Void main(void) { int m1,m2,m3,m4,m5,percent; printf(Enter marks in five subjects); scanf(%d %d %d %d %d,&m1,&m2,&m3,&m4,&m5); percent=(m1+m2+m3+m4+m5)/500 * 100;
SOLUTION
If(percent>=60) printf(First Division); Else { If(percent>=50) printf(Second Division); Else { If(percent>=40) printf(Third Division); Else printf(Fail); }
Else blocks are moving towards the right Can become difficult to match the corresponding if and else blocks Can become difficult to match braces of if and else blocks
AND OR NOT
&& || !
The logical operators contain two symbols note 1 & and | have different meanings as compared to && and ||
Logical operators can be used to combine two or more conditions. E.g. If(Marks>90 && Marks<100)
AND OPERATOR
AND operator is used when we want both the conditions to be true Here the condition will become true if and only if the number is between 50 and 60
A>50 FALSE
FALSE TRUE TRUE
Operator &&
&& && &&
A<60 FALSE
TRUE FALSE TRUE
Result FALSE
FALSE FALSE TRUE
OR OPERATOR
OR operator is used when we want either of the conditions to be TRUE. Here the condition will be true if either A or B is greater than 50.
A>50 FALSE
FALSE TRUE TRUE
Operator ||
|| || ||
B>50 FALSE
TRUE FALSE TRUE
Result FALSE
TRUE TRUE TRUE
NOT OPERATOR
The not operator i.e. ! reverses the result of the expression. E.g. 2==2 is true However, !(2==2) if false
Similarly, But
1==2 !(1==2)
is false is true
If(!flag)
is equivalent to
if(flag==0)
No else Blocks No need to match the corresponding if and else blocks Matching braces of if blocks becomes easy
If(percent>=60) printf(First Division); else if(percent>=50) printf(Second Division); else if(percent>=40) printf(Third Division); else if(percent<40) printf(Fail);