Download presentation
Presentation is loading. Please wait.
1
Quiz 1 96/07/23
2
Quiz 1 عدد زیر را به مبنای 2 بنویسید. 100.25
-11 (به روش متمم 2، طول بیتی = 4) الگوریتم برنامه ای بنویسید که عدد n را دریافت کند و دنباله فیبوناچی را عدد کمتر از n چاپ کند. برای n = 10 خروجی زیر مورد نظر است: 1 , 1 , 2 , 3 , 5 , 8
4
Control Structures Lecture 6
5
Outline Control Structures selection structure if if … else
Nested if … else switch
6
Control Structures All programs could be written in terms of only three control structures, sequence structure Normal operation, definition, etc. selection structure if … else, switch, etc. repetition structure for, while, do … while.
7
Control Structures Decisions are based on conditions
Do statements based on conditions True The statements will be done False The statement wont be done
8
Conditions Conditions by comparisons; e.g.,
If a is greater then b If c equals to d Comparing numbers: Relational Operators
9
Relations Relations produce a bool value
10
You should read the book
Boolean operations Multiple conditions in decision making Logical relation between conditions if you are student and you have the programming course You should read the book C Boolean operators and && or || not ! p q p && q p || q !p False True
11
Boolean operations Examples bool a = true, b=false, c;
c = !a; //c=false c = a && b; //c=false c = a || b; //c=true c = !a || b; //c=false
12
Precede
13
Example
14
Casting In logical operations In mathematical & comparison operations
0 False, non-zero True In mathematical & comparison operations False 0 , True 1
15
Examples x [10 , 20] Wrong Version Correct Version Let x = 30
10 <= 30 <=20 (10 <= 30) <= 20 true <= 20 1 <= 20 true!!! Correct Version (10 <= x) && (x <= 20) (10 <= 30) && (30 <= 20) true && false false
16
Examples a,b > 0 Wrong version
Let a = -10, b = 20 -10 && 20 > 0 -10 && (20 > 0) -10 && true true && true true !!! Correct version (a > 0) && (b > 0) Let a = -10, b = 20 (-10 > 0) && (20 > 0) false && true false
17
Lazy evaluation(Short-circuit)
When final result is found, does not evaluate remaining int i bool a = true, b false, c true; d a || || c; = 1; bool bool d = b && (a || c); d = (i > 0) && (sqrt(i) > 5.6);
18
Selection Structures C provides three types of selection structures in the form of statements: The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else selection statement performs an action if a condition is true and performs a different action if the condition is false. The switch selection statement performs one of many different actions depending on the value of an expression.
19
Selection Structures The if statement is called a single-selection statement because it selects or ignores a single action. The if…else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions.
20
if statement Decision making in C Expression
if( <expression> ) <statements1> else <statements2> Expression A boolean statement: a <= b + A mathematical statement: a zero false Non-zero true b or a variable: a
21
Type of statements Expression statement Compound statement
Single statements x = y + 10; Compound statement Starts with { and ends with } All statements can be between { and }
22
Flowchart if(<expression>) <statement1> else
23
Program to check if number is even or odd
24
Statements in if-else Empty statement Block statements
26
More than two choices If statement: 2 choices
If conditions are true if statements If conditions are false else statements How to make decisions when there are multiple choices?
27
Reminder if( <Condition> ) <command 1> Other command… true
false
28
Reminder if( <Condition> ) <command 1> else <command 2> Other command… false true
29
Reminder if( <Condition> ) { <command 1> <command 2> } else <command 3> Other command… false true
30
More than two choices To avoid repeating conditions in if statements
To avoid running unnecessary statements Nested if: check multiple conditions <Statements 1> becomes an if-else statement <Statements 2> becomes an if-else statement Repeat it as many as needed
31
Nested if
32
Reminder if( <Condition 1> ) { <command 1> <command 2> } else if( <Condition 2>) <command 3> else <command 4> Other command… false false true true
33
Map numeric grade to alphabetic
34
Example 1: Map numeric grade to alphabetic
35
Map numeric grade to alphabetic
36
Map numeric grade to alphabetic
37
Nested if: Example 2 Determine a char is alphabetic, Uppercase or not, numeric, less or greater than 5 or none of them
38
Equivalent if-else
39
Nested if: Incomplete branch
1) else part is optional 2) else always associates with the nearest if Example: To avoid error you should close off you code or Use Empty statements
40
Nested if: close off & empty statement
41
switch-case: Multiple choices
Multiple conditions If-else if-else if-…. Select from alternative values of a variable switch-case Values should be constant not expression: i, i+j, Values & Variables should be int or char switch(variable) { case value1: <statements 1> case value2: <statements 2> }
42
How does switch-case work?
Each switch-case can be rewritten by If-else if-else version of switch-case in the previous slide if(variable == value1)} <statements 1> <statements 2> } else if(variable == value2){
43
Example Write a program that get a char, if a or b or c print uppercase of char otherwise print error.
44
Example using switch
45
switch-case All values used in case should be different switch(i){ //Error case 1: … case 2:
46
switch-case All values must be value, not expression of variables switch(i){ //Error case j: … case 2: case k+10:
47
switch-case: multiple matches
48
switch-case vs. if-else
if-else is more powerful than switch-case switch-case is only for checking the values of a variable and the values must be constant Some if-else cannot be rewritten by switch-case double var1, var2; if(var1 <= 1.1) <statements 1> if(var1 == var2) <statements 2>
49
Nested switch-case
50
Conditional Expression
Assign value according to conditions A ternary operator Example:
51
Example Map Alphabetic Grade to Numeric
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.