Presentation is loading. Please wait.

Presentation is loading. Please wait.

We’re moving on to more recap from other programming languages

Similar presentations


Presentation on theme: "We’re moving on to more recap from other programming languages"— Presentation transcript:

1

2 We’re moving on to more recap from other programming languages
Variables in C++ We’re moving on to more recap from other programming languages Starting with variables Most are the same Two have big differences

3 Variables in C++ (1) We have the usual suspects int: a whole number
double: a decimal number float: a smaller decimal number char: a character bool: true/false, 0/1

4 Variables in C++ (1) Ex Open Visual Studio 2015/2017
Create a New solution for C++ Make it an Empty Project Add the main() function Make it return 0 Create each of the following variables (and give them values) int, double, float, char, bool

5 Variables in C++ (2) The only one missing is string
Unlike some other programming languages, string is NOT a primitive type here It has no ‘keyword’ like int or double In fact, for a while, there was only one way to make them Arrays of characters

6 Variables in C++ (2) Still starts as a char
[] not added until after variable’s name Can still use “” to denote a string though

7 Variables in C++ (2) However, creating arrays for strings is cumbersome So, a quicker way of making them was created Added to C++ as an update Using the string library

8 Variables in C++ (2) We can include the string library at the top
This lets us use the string data-type Like other programming languages Still use “” to denote strings Note that string is a different colour to other keywords – there’s a reason for that, which we’ll find out this term

9 Include the string library at the top of the C++ file
Variables in C++ (2) Ex Include the string library at the top of the C++ file Create a string variable for your name And give it a value

10 The arrows (<< and >>) show data flow
Variables in C++ (3) We can output these variables to the console However, printf doesn’t play well with string So we can use cout Uses slightly different syntax More like concatenation than formatting from before The arrows (<< and >>) show data flow

11 The arrows (<< and >>) show data flow
Variables in C++ (3) One thing to note about cout It doesn’t support formatting like printf If needed, we can output string in printf Using name.c_str() The arrows (<< and >>) show data flow

12 Variables in C++ (3) Ex Output each of your variables to the console
Use cout to do this Part of the iostream library Use << to concatenate text/values Don’t forget the \n at the end

13 Variables in C++ (4) The final thing is getting a string from the user
Can’t use scanf_s Same reason why printf doesn’t work So, we use cin instead Same as cout But data flows in opposite direction

14 Variables in C++ (4) Be careful about the direction of the arrows!
This will be the preferred way of outputting/inputting for now.

15 Use cin to get a value for each of your variables
Variables in C++ (4) Ex Use cin to get a value for each of your variables Make sure the arrows are pointing the right way Output these to the console after to check they are all correct

16 Now we have two main aspects to recap
Branching (1) Now we have two main aspects to recap Branching Iteration Both are core constructs in many other programming languages

17 Branching (1) Branching involves running different instructions
Depending on a condition Like other languages, we have techniques for these If, else-if, else Switch

18 Branching (1) Note that the usual rules of if/else-if/else apply
Need to start with if (OPTIONAL) Can have as many else if as needed (OPTIONAL) The else goes at the end

19 Branching (1) Ex Comment out the inside of the main() so far
Can use block comments at the start and end Use /* and */ Ask for the user’s age Check their age for the following (and output the respective message) < 20: Still growing < 40: You’re an adult now < 60: Getting on to middle-aged Otherwise: Enjoy retirement when/while you can

20 Branching (2) The other technique was the switch
It takes a single numerical/enum value And compares it against many cases Note that they only check for equality No less than/greater than here

21 Branching (2) Note that each case requires a break at the end Can also add an else by using the default label

22 Branching (2) Ex Comment out your main() so far
Ask the user what symbol is used for logical OR Use a switch to check for |: That is correct! +: In some cases, yes! Otherwise: Not quite! The answer is “|”

23 Iteration (1) Next up is iteration The two techniques we have for this
while-loops for-loops They both behave like they have done in other languages

24 Iteration (1) Let’s start with the while
It repeats its scope until its condition returns false The scope contains all the code within the curly braces While-loops are best used for “indefinite” instructions That could go on forever

25 Iteration (1) This program turns a number into that letter of the alphabet The while stops the number from being invalid

26 Iteration (1) Ex Comment out your main() so far
Ask the user to accept/decline an agreement Get their input in char form Show them options for (y/n) Use a while to make sure they enter one of those options If they didn’t, ask them again After the while, either thank them for agreeing or say “Maybe next time” if they declined

27 The other technique is the for
Iteration (2) The other technique is the for This gives us a bit more control than a while Still repeats based off a condition But we can also create a counter and change it at the end of every loop

28 Iteration (2) This program outputs a right-angled triangle
Gets a size from the user Outputs a triangle of that size

29 Iteration (2) Ex Comment out your main() so far
Ask the user for a size of a square Use for-loops to output a hollow square Outside formed of “0”s Inside formed of spaces

30 Random Numbers The final useful thing to cover before we look at some programs Making random numbers Other languages have us make a random number generator We don’t need to do that here Simply run a single function However, still have some setup

31 This requires three things
Random Numbers This requires three things Including the time.h library Using srand() to use a specific seed Using rand() to make a random number (and limiting it via a modulo)

32 Random Numbers The modulo is the best way of limiting rand()
Only adds a maximum Need to ‘shift up’ if we want to make a minimum The srand() function sets the seed And time() gets a time Using NULL makes time() uses the system time In total makes sure seed is always changing

33 Random Numbers Ex Comment out your main() so far
Include the time.h library, and use srand()/time() to make a seed Generate a random integer Output a message if the number is even/odd

34 Higher or Lower Ex Create a new C++ solution
This solution should be the Higher or Lower program First the program generates a random number (0 – 100) Then the program repeatedly Asks the user for a guess If the guess is too low/high, one message is output If the guess it spot on, the game ends

35 END OF UNIT!


Download ppt "We’re moving on to more recap from other programming languages"

Similar presentations


Ads by Google