-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigi1.txt
156 lines (127 loc) · 4.25 KB
/
digi1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
VARIABLES
They are containers used to store
data
my_school
my_name = "Hawee"
my_age
puts ("My name is " + my_name)
I go to _______
I am ________
RULES WHILE NAMING VARIABLES
-> All variable names must begin with a letter of the alphabet or an undescore(_)
-> After the initial letter, variable names can also contain letters and numbers
eg myschool
my8
-> Uppercase letters are distince from lowercase letters
eg MYAGE is different from myage
school is different from SCHOOL
-> You cannot use a KEYWORD(reseved words) as variable name
-> Variables can ONLY contain letters, numbers or undescores
(*, %, $, @, -, +, (), )
DATA TYPES IN RUBY
-> Numbers
Integers - Are whole numbers Eg 8, 9, 10
Floating points or Floats - Are not whole numbers, They are either decimals or
fractions eg 1.2, 3/4
* -> multiply
- -> Subtract
/ -> Divide
+ -> Add
-> Strings
Strings are a groups of letters that represent a sentence or a word
A string is anything inside double quotation marks
-> Boolean
They represent only one bit of information either true or false
-> Arrays
They can contain numbers, strings and boolean values
ARRAYS IN RUBY
Variables stores only one value. Arrays can store more than one value
******IF STATEMENTS******
I wake up
If I'm hungry
I eat breakfast
I leave my house
If its cloudy
I bring an umbrella
I'm at the restaurant
If i want meat
I order a steak
For an if statement to execute the condition must be true
= (assignment)
==, >, <, <=, >=, != (comparisons)
and -> is used when both the statements must be true
or -> is used when one of the statements must be true, atleast one
statement should be true
Functions
They are "self contined" modules of code that accomplish a specific task. Functions
usually 'take in' data, process it, and 'return' a result. Once a function is
written, it can be used over and over and over again.
Functions can be called from the inside of other functions
Loops
Loops in Ruby are used to execute the same block of code a specified number of times.
While Loops
while condition [do]
code
end
A while loop's condition is separated from the code by the reserved word do.
Quiz
1. Write a function calculation() such that it can accept two
variables and calculate their product.
2. Create a function showStudent() in such a way that it should
accept age, and Subtract it from 10 and display the output.
3. Print the numbers between 40 and 60 using while loop
4. Given a list, iterate it, and display numbers divisible by 3.
HINT -> The modulus should be 0 if the number is divisible by three.
5. Write code to display numbers from 20 to 10 using loop in decending order
6. Use a while loop to print the following pattern (20lines)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
1. Write code to display numbers from 10 to 1 using for loop in decending order
2. Write a program to display all odd numbers from 0 to 80
3. Given a two list of numbers create a new list such that new list should contain only odd
numbers from the first list and even numbers from the second list
list1 = [10, 20, 23, 11, 17]
list2 = [13, 43, 24, 36, 12] #Use the append
4. Display a message “Done” after successful execution of for loop
5. Write a program to display all prime numbers within 0 to 100
6. Given an array of numbers. Turn every item of that array into its square
Hint -> A square of a number is that number multiplied by itself
7. Create a ruby function that generates a ruby array of all the even numbers between 4 to 30
def calculation(num1, num2)
puts(num1*num2)
puts(num1/num2)
end
calculation(30,3)
def showStudent(StudentName, StudentAge)
puts("The students name is " + StudentName + "and their age is " + StudentAge.to_s)
end
showStudent("Hawee", 45)
i = 50
while i < 200
puts(i)
i += 1
end
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in numbers do
if i % 4 == 0
puts(i)
end
end
i = 10
while i > 0
puts(i)
i -= 1
end
n = 1
while n <= 10
puts "* " * n
n += 1
end