Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 1
Contents • If tests • Function • For loops • While loops • Reading and writing files • Exercise
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 2
If test • Very often in life, and in computer programs, the next action depends on the outcome of a question starting with “if”. • This gives the possibility to branch into different types of action depending on some criterion. • The test reads if condition, elseif condition, or else, where condition is a boolean expression that evaluates to true (1) or false (0). • If condition is true, the following statements up to the next elseif, else, or end are executed, and the remaining elseif or else branches are skipped. • If condition is false, the program flow jumps to the next elseif or else branch.
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 3
If test T = -50 + rand*200; ## generate a random float 0<r<1 fprintf("The water temperature is %d degree celcius \n" ,T); if (T < 0) fprintf("The water becomes ice") elseif (T <100) fprintf("The water becomes liquid") else fprintf("The water becomes vapor") endif
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 4
If test T = 21 % assign value to a variable T == 20 % temperature equal to 20 T ~= 20 % temperature not equal to 20 T < 20 % temperature less than 20 T > 20 % temperature greater than 20 T <= 20 % temperature less than or equal to 20 T >= 20 % temperature greater than or equal to 20
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 5
Functions • A function in a program is much like a mathematical function e.g. function to convert currency, temperature, etc • Purpose: • to group statements into separate units of code lines that naturally belong together (a strategy which may dramatically ease the problem solving process), and/or • to parameterize a set of statements such that they can be written only once and easily be re-executed with variations
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 6
Functions
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 7
Functions • Return → used to enforce a return from a function before it reaches the end
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 8
Functions
A function may take no arguments,
or many, in which case they are just listed within the parentheses (following the function name) and separated by a comma
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 9
Functions
Functions are straightforwardly
passed as arguments to other functions
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 10
Assignment 2 • Create an interactive calculator (any calculator) that use • Data input • Function • If • Be unique and creative
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 11
For loops • Loop variable → (start:step:stop) • See for i = 3:7 i^2 end
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 12
For loops
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 13
For loops inside for loops
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 14
For loops to calculate sum
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 15
While loops
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 16
Reading / writing files data = load ("tmp.dat"); x = data(:,1); y = data(:,2); data(:,2) = log(y); % insert transformed y back in array filename = "tmp_out.dat"; outfile = fopen(filename, "w"); % open file for writing fprintf(outfile, "%% x and y coordinates\n"); fprintf(outfile, "%10.5f %10.5f\n", data); fclose(outfile);
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 17
Assignment 3 • Develop your previous program with addition of data reading and writing features • Be unique and creative
Ahmad Syihan Auzani - Eng Programming - DTM FTUI 2021 18