Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

True or false A variable of type char can hold the value 301. ( F )
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Chapter 2: Using Data.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
JavaScript, Fourth Edition
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Expressions.
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Programming Fundamentals
Lecture 7: Repeating a Known Number of Times
Nahla Abuel-ola / WIT.
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
A First Book of ANSI C Fourth Edition
Computing Fundamentals
Expressions and Assignment
C++ Programming Basics
Using C++ Arithmetic Operators and Control Structures
DATA TYPES There are four basic data types associated with variables:
is multiplied by a variable of type to yield a result of type
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Programming Fundamentals

The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where n is the argument to setw(n). The value is right justified within the field.

OUTPUT

Example code

OUTPUT

Field width and setw Name:MadihaLiaqat Reg#:09-SE-99 Name:MadihaLiaqt Reg#:09-SE-99 Without setw manipulator

OUTPUT

Variable Type Summary

Unsigned Data Types

Type Conversion

Automatic Conversions

When two operands of different types are encountered in the same expression, the lower-type variable is converted to the type of the higher-type variable.

Automatic Conversions In our example, the int value of count is converted to type float and stored in a temporary variable before being multiplied by the float variable avgWeight. The result (still of type float) is then converted to double so that it can be assigned to the double variable totalWeight.

Process of coversion 4 bytes These conversions take place invisibly

Casts In C++ Cast applies to data conversions specified by the programmer, as opposed to the automatic data conversions. Casts are also called type casts. What are casts for?

Casts Sometimes a programmer needs to convert a value from one type to another in a situation where the compiler will not do it automatically or without complaining. We have four specific casting operators: dynamic_cast (expression) reinterpret_cast (expression) static_cast (expression) const_cast (expression)

Example

The Remainder Operator This operator (also called the modulus operator) finds the remainder when one number is divided by another.

Arithmetic Assignment Operators

Increment Operators The ++ operator increments (adds 1 to) its argument. Normal way: Using arithmetic assignment operator Using Increment operator

Prefix and Postfix the increment operator can be used in two ways: Prefix meaning that the operator precedes the variable Postfix meaning that the operator follows the variable Example See next slide

The Decrement (--) Operator The decrement operator, --, behaves very much like the increment operator, except that it subtracts 1 from its operand. It too can be used in both prefix and postfix forms. Example count-- --count

Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as char, int, and float or they can be user- defined classes. The comparison involves such relationships as equal to, less than, and greater than. The result of the comparison is true or false; for example, either two values are equal (true), or they’re not (false).

Complete list of C++ relational operators

Example

OUTPUT

Example Expressions

Loops Loops cause a section of your program to be repeated a certain number of times. The repetition continues while a condition is true a certain number of times. When the condition becomes false, the loop ends and control passes to the statements following the loop.

Kinds of Loop There are three kinds of loops in C++: for loop, while loop, do loop.

The for Loop The for loop executes a section of code a fixed number of times. It’s usually (although not always) used when you know, before entering the loop, how many times you want to execute the code.

Example

OUTPUT

Example

OUTPUT

How does this work?

Explanations The for statement controls the loop. It consists of the keyword for, followed by parentheses that contain three expressions separated by semicolons:

These three expressions are the initialization expression, the test expression, and the increment expression. These three expressions usually (but not always) involve the same variable, which we call the loop variable.

Explanation The body of the loop is the code to be executed each time through the loop. In our example the loop body consists of a single statement:

Contdd.... This statement prints out the square of j, followed by two spaces. The square is found by multiplying j by itself. As the loop executes, j goes through the sequence 0, 1, 2, 3, and so on up to 14; so the squares of these numbers are displayed—0, 1, 4, 9, up to 196.

The Initialization Expression The initialization expression is executed only once, when the loop first starts. It gives the loop variable an initial value. In our example it sets j to 0.

The Test Expression The test expression usually involves a relational operator. It is evaluated each time through the loop, just before the body of the loop is executed. It determines whether the loop will be executed again. If the test expression is true, the loop is executed one more time. If it’s false, then loop ends, and control passes to the statements following the loop.

Contdd.... In our example the statement is executed following the completion of the loop.

Increment /Decrement Expression The increment expression changes the value of the loop variable, often by incrementing it. It is always executed at the end of the loop, after the loop body has been executed. How Many Times? In the figure shown in previous slides, The first time j=0, This is ensured in the initialization expression. The last time through the loop, j is 14. This is determined by the test expression j<15.

Contdd.... When j becomes 15, the loop terminates; the loop body is not executed when j has this value. Next slide shows a flow chart for loop operation. The arrangement shown is commonly used to do something a fixed number of times: start at 0, use a test expression with the less-than operator and a value equal to the desired number of iterations increment the loop variable after each iteration.

Another for loop example for(count=0; count<100; count++) What are your comments about this example?

Multiple Statements in the Loop Body We want to execute more than one statement in the loop body. Multiple statements are delimited by braces. Note : there is no semicolon following the final brace of the loop body, although there are semicolons following the individual statements in the loop body.

Questions????