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

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Flow of Control Part 1: Selection
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
CPS120: Introduction to Computer Science Decision Making in Programs.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Lab 4: Loops and Iteration Graham Northup
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Core Java Statements in Java.
Chapter 10 Programming Fundamentals with JavaScript
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Introduction to Python
Control Flow Constructs: Conditional Logic
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Chapter 4: Making Decisions.
ECS10 10/10
Chapter 4: Making Decisions.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Engineering Innovation Center
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structures – Selection
Control Statement Examples
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
TOPIC 4: REPETITION CONTROL STRUCTURE
Chapter 10 Programming Fundamentals with JavaScript
Building Java Programs
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Coding Concepts (Sub- Programs)
Encryption and Decryption
Topic 1: Problem Solving
Coding Concepts (Basics)
Introduction to TouchDevelop
Variables Title slide variables.
Coding Concepts (Data- Types)
Chapter 4 Selection.
Problem Solving Designing Algorithms.
Computing Fundamentals
CPS120: Introduction to Computer Science
Python programming exercise
Control Structures Part 3
Control Structures Part 1
Fundamentals of Functional Programming
Fundamental Programming
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Software Development Techniques
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Switch Case Structures
Programming Fundamental
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 “|”

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

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

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

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

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

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

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

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

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)

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

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

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

END OF UNIT!