SlideShare a Scribd company logo
1 of 20
CSCI 101
Selection and Looping
Program control, if-else, logical
expressions, for loop
Overview
• Review
• Program control
• if-else statement
– Relational and logical operators
– Logical expressions
• for loop statement
How to write a program?
1. Read the problem statement, and identify
– The input and its range
– The output
– The relationship between the input and the output
(how to compute the output) [Comprehend]
2. Write your thoughts as a sequence of steps.
[Algorithm]
3. Convert these steps to Code. [Program]
4. Test your code and compare your program result
against a human result. [Testing]
Calculate area of a rectangle
(Algorithm  Program)
1. Get height h
 h=input(‘enter height: ’);
2. Get width w
 w=input(‘enter width: ’);
3. Calculate area = h * w
 area = h * w;
4. Display area
 disp (area);
Program Control
• How to control the flow of the program?
– To executes some statements and not the other
based on the input data.
– To make the program takes decision
• How to make some statements executed
several times.
Print the maximum of two numbers
(Algorithm and Program)
1. Get number1 as X  X=input(‘enter num1:’);
2. Get number2 as Y  Y=input(‘enter num2:’);
3. If X > Y  if X>Y
display X disp(X);
Otherwise else
display Y disp(Y);
end
Print the maximum of two numbers
(Program and Testing)
% printmax.m
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X);
else
disp(Y);
end
>>printmax
enter num1: 5
enter num2: 10
10
>>printmax
enter num1: 8
enter num2: 3
8
5
10
X
Y
8
3
Print two number in ascending order
(Algorithm  Program)
1. Get number1 as X  X=input(‘enter num1:’);
2. Get number2 as Y  Y=input(‘enter num2:’);
3. If X > Y  if X>Y
display Y, X disp(Y); disp(X);
Otherwise else
display X, Y disp(X); disp(Y);
end
Print two number in ascending order
(Program and Testing)
% order2.m
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(Y);
disp(X);
else
disp(X);
disp(Y);
end
>>order2
enter num1: 5
enter num2: 10
5
10
>>order2
enter num1: 8
enter num2: 3
3
8
5
10
X
Y
8
3
If-else statement
if condition
% do this part if condition is true
else
%do this part if the condition is false
end
Condition (use relational operators):
>, <, >=, <=, ==, ~=
Relational Operators
• Relational operators test the Relationship between
two operands (which can be expressions)
• They are a “Truth Statement”, and resolve to either
True (1) or False (0)
Operator Operation
== Equal to
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Logical Operators
• Relational operators are binary operators so they can have only two
operands
• To have a more complicated condition, we need to use logical
operators
• Logic Operators can combine “truth statements” together in helpful
ways. (and, or, not)
Inputs and or not
A B A && B A || B ~A
false false false false true
false true false true true
true false false true false
true true true true false
Logical Expressions Examples
Write the if condition for the following cases:
1. Check if x > y > z
 if x>y && y>z
2. Check if x is greater than y or less than or equal to z
 if x>y || x <=z
3. Check if x does not equal y but equal z
 if x~=y && x==z
4. Check if x is even
 if rem(x,2)==0
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X);
else
disp(Y);
end
Repeat a program N times
for i=1:1:5
end
Iteration counter
First counter value
Step value
Last counter value
Try it in MATLAB
for i=1:1:5
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X);
else
disp(Y);
end
end
X
Y
i
How many times these loops will run:
for i=1:2:10 for z=10:2:20
for a=10:-2:1 for x=1:10
?
Calculate sum from 1 to N (Program)
N=input(‘enter a number:’);
sum=0;
for i=1:N
sum=sum+i;
end
disp(sum);
How to sum only even numbers?
N i sum
5 0
1 1
2 3
3 6
4 10
5 15
Calculate factorial of a number N
(Program)
N=input(‘enter a number:’);
f=1;
for i=1:N
f=f*i;
end
disp(f);
N i f
5 1
1 1
2 2
3 6
4 24
5 120
What this program does?
N=input(‘enter a number:’);
sum=0;
for i=1:N
x=input(‘enter a value:’);
sum=sum+x;
end
disp(sum);
N i x sum
5 0
1 3 3
2 5 8
3 0 8
4 -2 6
5 1 7
How to compute the average of N numbers?
Sum only positive numbers?
N=input(‘enter a number:’);
sum=0;
for i=1:N
x=input(‘enter a value:’);
if x>0
sum=sum+x;
end
end
disp(sum);
N i x sum
5 0
1 3 3
2 5 8
3 -3 8
4 -2 8
5 1 9
Sum and Average positive numbers?
N=input(‘enter a number:’);
sum=0;
c=0;
for i=1:N
x=input(‘enter a value:’);
if x>0
sum=sum+x;
c=c+1;
end
End
Avg=sum/c;
disp(sum);
Disp(Avg);
N i x sum c
5 0 0
1 3 3 1
2 5 8 2
3 -3 8 2
4 -2 8 2
5 1 9 3
C is used to count positive numbers

More Related Content

What's hot

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02Pooja Gupta
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators pptViraj Shah
 
COM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingCOM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingHemantha Kulathilake
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsShameer Ahmed Koya
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of PrecedenceMuhammad Hammad Waseem
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATORrricky98
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_iElsayed Hemayed
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++Muhammad Hammad Waseem
 
C operators
C operatorsC operators
C operatorsGPERI
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programmingManoj Tyagi
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++Muhammad Hammad Waseem
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C languageAbdul Rehman
 

What's hot (20)

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators
OperatorsOperators
Operators
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 
COM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingCOM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & Branching
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
C operators
C operatorsC operators
C operators
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
C Operators
C OperatorsC Operators
C Operators
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C language
 

Similar to Csci101 lect02 selection_andlooping

Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionElsayed Hemayed
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialTo Sum It Up
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programsElsayed Hemayed
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxjacksnathalie
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10alish sha
 
Here is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docxHere is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docxtrappiteboni
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1 Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1 To Sum It Up
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilizationJAYDEV PATEL
 

Similar to Csci101 lect02 selection_andlooping (20)

Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study material
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
Csci101 lect08a matlab_programs
Csci101 lect08a matlab_programsCsci101 lect08a matlab_programs
Csci101 lect08a matlab_programs
 
Software Metrics
Software MetricsSoftware Metrics
Software Metrics
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docx
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
C important questions
C important questionsC important questions
C important questions
 
Here is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docxHere is the grading matrix where the TA will leave feedback. If you .docx
Here is the grading matrix where the TA will leave feedback. If you .docx
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1 Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1
 
Python
PythonPython
Python
 
computer programming and utilization
computer programming and utilizationcomputer programming and utilization
computer programming and utilization
 
GE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_NotesGE3151_PSPP_UNIT_3_Notes
GE3151_PSPP_UNIT_3_Notes
 

More from Elsayed Hemayed

14 cie552 camera_calibration
14 cie552 camera_calibration14 cie552 camera_calibration
14 cie552 camera_calibrationElsayed Hemayed
 
12 cie552 object_recognition
12 cie552 object_recognition12 cie552 object_recognition
12 cie552 object_recognitionElsayed Hemayed
 
11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift11 cie552 image_featuresii_sift
11 cie552 image_featuresii_siftElsayed Hemayed
 
10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner10 cie552 image_featuresii_corner
10 cie552 image_featuresii_cornerElsayed Hemayed
 
09 cie552 image_featuresi
09 cie552 image_featuresi09 cie552 image_featuresi
09 cie552 image_featuresiElsayed Hemayed
 
08 cie552 image_segmentation
08 cie552 image_segmentation08 cie552 image_segmentation
08 cie552 image_segmentationElsayed Hemayed
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicingElsayed Hemayed
 
06 cie552 image_manipulation
06 cie552 image_manipulation06 cie552 image_manipulation
06 cie552 image_manipulationElsayed Hemayed
 
05 cie552 image_enhancement
05 cie552 image_enhancement05 cie552 image_enhancement
05 cie552 image_enhancementElsayed Hemayed
 
04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequencyElsayed Hemayed
 
03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatialElsayed Hemayed
 
02 cie552 image_andcamera
02 cie552 image_andcamera02 cie552 image_andcamera
02 cie552 image_andcameraElsayed Hemayed
 
Csci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiCsci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiElsayed Hemayed
 
Csci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeCsci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeElsayed Hemayed
 
Csci101 lect08b matlab_programs
Csci101 lect08b matlab_programsCsci101 lect08b matlab_programs
Csci101 lect08b matlab_programsElsayed Hemayed
 
Csci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiElsayed Hemayed
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingElsayed Hemayed
 
Csci101 lect05 formatted_output
Csci101 lect05 formatted_outputCsci101 lect05 formatted_output
Csci101 lect05 formatted_outputElsayed Hemayed
 
Csci101 lect01 first_program
Csci101 lect01 first_programCsci101 lect01 first_program
Csci101 lect01 first_programElsayed Hemayed
 

More from Elsayed Hemayed (20)

14 cie552 camera_calibration
14 cie552 camera_calibration14 cie552 camera_calibration
14 cie552 camera_calibration
 
12 cie552 object_recognition
12 cie552 object_recognition12 cie552 object_recognition
12 cie552 object_recognition
 
11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift11 cie552 image_featuresii_sift
11 cie552 image_featuresii_sift
 
10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner10 cie552 image_featuresii_corner
10 cie552 image_featuresii_corner
 
09 cie552 image_featuresi
09 cie552 image_featuresi09 cie552 image_featuresi
09 cie552 image_featuresi
 
08 cie552 image_segmentation
08 cie552 image_segmentation08 cie552 image_segmentation
08 cie552 image_segmentation
 
07 cie552 image_mosaicing
07 cie552 image_mosaicing07 cie552 image_mosaicing
07 cie552 image_mosaicing
 
06 cie552 image_manipulation
06 cie552 image_manipulation06 cie552 image_manipulation
06 cie552 image_manipulation
 
05 cie552 image_enhancement
05 cie552 image_enhancement05 cie552 image_enhancement
05 cie552 image_enhancement
 
04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency04 cie552 image_filtering_frequency
04 cie552 image_filtering_frequency
 
03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial03 cie552 image_filtering_spatial
03 cie552 image_filtering_spatial
 
02 cie552 image_andcamera
02 cie552 image_andcamera02 cie552 image_andcamera
02 cie552 image_andcamera
 
01 cie552 introduction
01 cie552 introduction01 cie552 introduction
01 cie552 introduction
 
Csci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iiiCsci101 lect10 algorithms_iii
Csci101 lect10 algorithms_iii
 
Csci101 lect09 vectorized_code
Csci101 lect09 vectorized_codeCsci101 lect09 vectorized_code
Csci101 lect09 vectorized_code
 
Csci101 lect08b matlab_programs
Csci101 lect08b matlab_programsCsci101 lect08b matlab_programs
Csci101 lect08b matlab_programs
 
Csci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_ii
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_looping
 
Csci101 lect05 formatted_output
Csci101 lect05 formatted_outputCsci101 lect05 formatted_output
Csci101 lect05 formatted_output
 
Csci101 lect01 first_program
Csci101 lect01 first_programCsci101 lect01 first_program
Csci101 lect01 first_program
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

Csci101 lect02 selection_andlooping

  • 1. CSCI 101 Selection and Looping Program control, if-else, logical expressions, for loop
  • 2. Overview • Review • Program control • if-else statement – Relational and logical operators – Logical expressions • for loop statement
  • 3. How to write a program? 1. Read the problem statement, and identify – The input and its range – The output – The relationship between the input and the output (how to compute the output) [Comprehend] 2. Write your thoughts as a sequence of steps. [Algorithm] 3. Convert these steps to Code. [Program] 4. Test your code and compare your program result against a human result. [Testing]
  • 4. Calculate area of a rectangle (Algorithm  Program) 1. Get height h  h=input(‘enter height: ’); 2. Get width w  w=input(‘enter width: ’); 3. Calculate area = h * w  area = h * w; 4. Display area  disp (area);
  • 5. Program Control • How to control the flow of the program? – To executes some statements and not the other based on the input data. – To make the program takes decision • How to make some statements executed several times.
  • 6. Print the maximum of two numbers (Algorithm and Program) 1. Get number1 as X  X=input(‘enter num1:’); 2. Get number2 as Y  Y=input(‘enter num2:’); 3. If X > Y  if X>Y display X disp(X); Otherwise else display Y disp(Y); end
  • 7. Print the maximum of two numbers (Program and Testing) % printmax.m X=input(‘enter num1:’); Y=input(‘enter num2:’); if X>Y disp(X); else disp(Y); end >>printmax enter num1: 5 enter num2: 10 10 >>printmax enter num1: 8 enter num2: 3 8 5 10 X Y 8 3
  • 8. Print two number in ascending order (Algorithm  Program) 1. Get number1 as X  X=input(‘enter num1:’); 2. Get number2 as Y  Y=input(‘enter num2:’); 3. If X > Y  if X>Y display Y, X disp(Y); disp(X); Otherwise else display X, Y disp(X); disp(Y); end
  • 9. Print two number in ascending order (Program and Testing) % order2.m X=input(‘enter num1:’); Y=input(‘enter num2:’); if X>Y disp(Y); disp(X); else disp(X); disp(Y); end >>order2 enter num1: 5 enter num2: 10 5 10 >>order2 enter num1: 8 enter num2: 3 3 8 5 10 X Y 8 3
  • 10. If-else statement if condition % do this part if condition is true else %do this part if the condition is false end Condition (use relational operators): >, <, >=, <=, ==, ~=
  • 11. Relational Operators • Relational operators test the Relationship between two operands (which can be expressions) • They are a “Truth Statement”, and resolve to either True (1) or False (0) Operator Operation == Equal to ~= Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to
  • 12. Logical Operators • Relational operators are binary operators so they can have only two operands • To have a more complicated condition, we need to use logical operators • Logic Operators can combine “truth statements” together in helpful ways. (and, or, not) Inputs and or not A B A && B A || B ~A false false false false true false true false true true true false false true false true true true true false
  • 13. Logical Expressions Examples Write the if condition for the following cases: 1. Check if x > y > z  if x>y && y>z 2. Check if x is greater than y or less than or equal to z  if x>y || x <=z 3. Check if x does not equal y but equal z  if x~=y && x==z 4. Check if x is even  if rem(x,2)==0
  • 14. X=input(‘enter num1:’); Y=input(‘enter num2:’); if X>Y disp(X); else disp(Y); end Repeat a program N times for i=1:1:5 end Iteration counter First counter value Step value Last counter value
  • 15. Try it in MATLAB for i=1:1:5 X=input(‘enter num1:’); Y=input(‘enter num2:’); if X>Y disp(X); else disp(Y); end end X Y i How many times these loops will run: for i=1:2:10 for z=10:2:20 for a=10:-2:1 for x=1:10 ?
  • 16. Calculate sum from 1 to N (Program) N=input(‘enter a number:’); sum=0; for i=1:N sum=sum+i; end disp(sum); How to sum only even numbers? N i sum 5 0 1 1 2 3 3 6 4 10 5 15
  • 17. Calculate factorial of a number N (Program) N=input(‘enter a number:’); f=1; for i=1:N f=f*i; end disp(f); N i f 5 1 1 1 2 2 3 6 4 24 5 120
  • 18. What this program does? N=input(‘enter a number:’); sum=0; for i=1:N x=input(‘enter a value:’); sum=sum+x; end disp(sum); N i x sum 5 0 1 3 3 2 5 8 3 0 8 4 -2 6 5 1 7 How to compute the average of N numbers?
  • 19. Sum only positive numbers? N=input(‘enter a number:’); sum=0; for i=1:N x=input(‘enter a value:’); if x>0 sum=sum+x; end end disp(sum); N i x sum 5 0 1 3 3 2 5 8 3 -3 8 4 -2 8 5 1 9
  • 20. Sum and Average positive numbers? N=input(‘enter a number:’); sum=0; c=0; for i=1:N x=input(‘enter a value:’); if x>0 sum=sum+x; c=c+1; end End Avg=sum/c; disp(sum); Disp(Avg); N i x sum c 5 0 0 1 3 3 1 2 5 8 2 3 -3 8 2 4 -2 8 2 5 1 9 3 C is used to count positive numbers