SlideShare a Scribd company logo
Programming basics
   Before I explain any   int main()
                           {
    code to you, I            int i;
    would like you to         int sum = 0;

    execute your first         for(i = 0; i<10; i++){
                                   sum += i;
    program by                 }
    copying and
                               printf("Sum = %dn", sum);
    pasting my code.           //system("pause");
                               return 0;
                           }
 Locate the compile button and run
  buttons in your program.
 You may alternatively find the compile
  and run button that executes both at
  once.
 You should notice a black box
  appearing and disappearing very
  quickly. That is because I have allowed
  this code to execute without any pause
  statements.
   Programs typically automatically close
    once the final line of code is reached.

   The line system(“pause”); needs to be
    added to “pause” the program in place

   The system pause should be placed right
    before the return 0; line of any program.
    Return 0 is typically used at the end of a
    program to tell the computer the program
    has ended.
   Code is normally presented omitting the
    system("pause"); statement which
    confuses new programmers. Always
    make a mental note to add it in if you
    are testing new code.
 When you place // before a line of
  code, you comment the code out and
  cause the compiler to skip over the
  code.
 Now you can remove the // before the
  system pause line in the example
  program and run the program again to
  see output. If all is correct, you should
  see the output being Sum = 45.
Programming basics
   I want to draw your attention to the first line:
    int main(){
   This is the beginning of your main function. The
    function ends with the use of }.
   Any time you open a { bracket, you must close it with
    a } bracket.
   As you can see with the main function, you can see
    the entire main function enclosed within the { }
    brackets.
   Additional code is allowed within the brackets. This
    includes code that uses another set of { } as well.
   The last { bracket to be opened is the first one to be
    closed when a } bracket is used.
 The main function is the starting point of
  a program and where a compiler first
  starts to execute your code.
 It is required in order to compile your
  code, therefore it would be a good
  practice to use the following template.
int main(){

     //your new written code goes here

     system("pause");
     return 0;
}
   Our program is going to need a way to hold
    our values
   These values are stored in different types of
    variables.
   Integers, floats, and doubles are the main
    “number” holders of a program.
   A variable in C programming must always be
    declared at the top of the program right under
    int main() { if you want to use the variable in
    your program.
   Unlike some programming languages, you may
    not locally declare a variable anywhere.
 An integer is a number only
  representable in a whole number form.
 Integers: 1,2,3,4,5..
 Not Integers: 1.5, 1.7, 2.5, 3.9, 1003.4...
 Integer math is the calculation between
  two integers. The math is calculated as
  you would normally between any two
  numbers except the decimal value is
  dropped. Ex. 1+3=4, 1+.5=1 , 5/4 = 1
   Double and Float variables allow the use of
    decimal numbers. A float has a smaller
    default precision than a double.
   If you declare a float and then try to store
    the number within the float into a double,
    you may be transferring only part of the
    number.
   A float allows at most about 7 decimal
    places precision.
    A double allows at least 7 places.
   To accurately work beyond 7 decimals you
    may require addition resources.
 I typically use only Doubles when working
  with decimals and will work with only
  Doubles within these tutorials.
 The math calculated with a double or
  float does not use integer math. It would
  be safe to assume the calculated
  answer would be similar to the answer
  given in a math class.
 An integer is declared by typing
  int variablename;
 A float is declared by typing
  float variablename;
 A double is declared by typing
  double variablename;
 The variablename is changeable to
  anything starting with a letter and
  anything that has not been assigned to
  a variable yet.
 Multiple variables of the same type are
  declarable by adding a comma after
  the first variable and then typing the
  second.
 int one, two, three;
 The point of a variable is to eventually
  hold data.
 The variable could be initialized
  immediately with a value.
 int one = 1;
 double duck = 12.5;
   To print out the value a variable is holding
    you use the printf statement
   printf(“The variable one = %d”, one);
   As shown above, the text you want printed
    out is held within the quotation marks. A
    %variabletype is added to signal to the
    computer you want to print out a variable.
    int -> %d float -> %fl double -> %lf
   After the comma, you list the variables in
    order that match up with the %variables
 A double could be printed out but it may
  contain too many decimal places.
 Limit a double by using %.#lf for the
  number of decimal places you want to
  be printed out.
 The key “n” to a computer is
  interpreted as a line break.
 When used in a printf statement, it will
  add a line break into the output.
int main(){

        int one = 5.5;
        float two = 7.5;
        double three = 10.5;

        printf("One equals: %d n", one);
        printf("Two equals: %f n", two);
        printf("Three equals: %lf n ", three);

        three = 15;

        printf("Three equals: %lf ", three);

        system("pause");
        return 0;
}
   Programming Template

   Integers, Floats, and Doubles

   Printf statements

More Related Content

DOCX
C rules
PDF
Control Structures in Python
PDF
Learning R while exploring statistics
PPTX
Conditional and control statement
PPT
Ch3 selection
PPT
Python Control structures
PPTX
FLOW OF CONTROL-INTRO PYTHON
PDF
Python Flow Control
C rules
Control Structures in Python
Learning R while exploring statistics
Conditional and control statement
Ch3 selection
Python Control structures
FLOW OF CONTROL-INTRO PYTHON
Python Flow Control

What's hot (18)

PPTX
Types of Statements in Python Programming Language
PPTX
FLOWOFCONTROL-IF..ELSE PYTHON
PDF
Python - Control Structures
PDF
Chapter 3
PPTX
C Programming: Control Structure
PPTX
Basic computer-programming-2
PDF
1584503386 1st chap
PPT
Lecture 9- Control Structures 1
PPTX
MATLAB programming tips 2 - Input and Output Commands
PPTX
Control structure of c language
PDF
Matlab syntax
PPT
Lecture 10 - Control Structures 2
PDF
Basic c# cheat sheet
DOCX
C language assignment help
PDF
[ITP - Lecture 11] Loops in C/C++
PPTX
Operators
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
PPT
Visula C# Programming Lecture 3
Types of Statements in Python Programming Language
FLOWOFCONTROL-IF..ELSE PYTHON
Python - Control Structures
Chapter 3
C Programming: Control Structure
Basic computer-programming-2
1584503386 1st chap
Lecture 9- Control Structures 1
MATLAB programming tips 2 - Input and Output Commands
Control structure of c language
Matlab syntax
Lecture 10 - Control Structures 2
Basic c# cheat sheet
C language assignment help
[ITP - Lecture 11] Loops in C/C++
Operators
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Visula C# Programming Lecture 3
Ad

Viewers also liked (6)

DOCX
Mca java application projects completed list(is)
PPT
SHOW104: Practical Java
PDF
Java practical(baca sem v)
PPTX
File Uploading in PHP
DOCX
Java PRACTICAL file
Mca java application projects completed list(is)
SHOW104: Practical Java
Java practical(baca sem v)
File Uploading in PHP
Java PRACTICAL file
Ad

Similar to Programming basics (20)

PPT
Programming For As Comp
PPT
Programming For As Comp
PDF
Tutorial basic of c ++lesson 1 eng ver
PPT
Lập trình C
DOCX
C programming perso notes
PPTX
lecture 2.pptx
PDF
Maxbox starter
DOCX
Core programming in c
PPTX
the refernce of programming C notes ppt.pptx
PPTX
C decision making and looping.
DOC
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
PDF
C++ Course - Lesson 1
PDF
C programming
PPT
Introduction to Basic C programming 01
PPT
Lecture 1
PPT
Lecture 1
PPTX
Input-output
PPTX
Introduction to Programming c language.pptx
PPTX
Fundamentals of prog. by rubferd medina
PDF
Programming fundamental 02
Programming For As Comp
Programming For As Comp
Tutorial basic of c ++lesson 1 eng ver
Lập trình C
C programming perso notes
lecture 2.pptx
Maxbox starter
Core programming in c
the refernce of programming C notes ppt.pptx
C decision making and looping.
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
C++ Course - Lesson 1
C programming
Introduction to Basic C programming 01
Lecture 1
Lecture 1
Input-output
Introduction to Programming c language.pptx
Fundamentals of prog. by rubferd medina
Programming fundamental 02

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Transforming Manufacturing operations through Intelligent Integrations
PPT
Teaching material agriculture food technology
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced IT Governance
PDF
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
Transforming Manufacturing operations through Intelligent Integrations
Teaching material agriculture food technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Understanding_Digital_Forensics_Presentation.pptx
GamePlan Trading System Review: Professional Trader's Honest Take
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
20250228 LYD VKU AI Blended-Learning.pptx
madgavkar20181017ppt McKinsey Presentation.pdf
Modernizing your data center with Dell and AMD
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Chapter 2 Digital Image Fundamentals.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Advanced IT Governance
cuic standard and advanced reporting.pdf

Programming basics

  • 2. Before I explain any int main() { code to you, I int i; would like you to int sum = 0; execute your first for(i = 0; i<10; i++){ sum += i; program by } copying and printf("Sum = %dn", sum); pasting my code. //system("pause"); return 0; }
  • 3.  Locate the compile button and run buttons in your program.  You may alternatively find the compile and run button that executes both at once.  You should notice a black box appearing and disappearing very quickly. That is because I have allowed this code to execute without any pause statements.
  • 4. Programs typically automatically close once the final line of code is reached.  The line system(“pause”); needs to be added to “pause” the program in place  The system pause should be placed right before the return 0; line of any program. Return 0 is typically used at the end of a program to tell the computer the program has ended.
  • 5. Code is normally presented omitting the system("pause"); statement which confuses new programmers. Always make a mental note to add it in if you are testing new code.
  • 6.  When you place // before a line of code, you comment the code out and cause the compiler to skip over the code.  Now you can remove the // before the system pause line in the example program and run the program again to see output. If all is correct, you should see the output being Sum = 45.
  • 8. I want to draw your attention to the first line: int main(){  This is the beginning of your main function. The function ends with the use of }.  Any time you open a { bracket, you must close it with a } bracket.  As you can see with the main function, you can see the entire main function enclosed within the { } brackets.  Additional code is allowed within the brackets. This includes code that uses another set of { } as well.  The last { bracket to be opened is the first one to be closed when a } bracket is used.
  • 9.  The main function is the starting point of a program and where a compiler first starts to execute your code.  It is required in order to compile your code, therefore it would be a good practice to use the following template.
  • 10. int main(){ //your new written code goes here system("pause"); return 0; }
  • 11. Our program is going to need a way to hold our values  These values are stored in different types of variables.  Integers, floats, and doubles are the main “number” holders of a program.  A variable in C programming must always be declared at the top of the program right under int main() { if you want to use the variable in your program.  Unlike some programming languages, you may not locally declare a variable anywhere.
  • 12.  An integer is a number only representable in a whole number form.  Integers: 1,2,3,4,5..  Not Integers: 1.5, 1.7, 2.5, 3.9, 1003.4...  Integer math is the calculation between two integers. The math is calculated as you would normally between any two numbers except the decimal value is dropped. Ex. 1+3=4, 1+.5=1 , 5/4 = 1
  • 13. Double and Float variables allow the use of decimal numbers. A float has a smaller default precision than a double.  If you declare a float and then try to store the number within the float into a double, you may be transferring only part of the number.  A float allows at most about 7 decimal places precision.  A double allows at least 7 places.  To accurately work beyond 7 decimals you may require addition resources.
  • 14.  I typically use only Doubles when working with decimals and will work with only Doubles within these tutorials.  The math calculated with a double or float does not use integer math. It would be safe to assume the calculated answer would be similar to the answer given in a math class.
  • 15.  An integer is declared by typing int variablename;  A float is declared by typing float variablename;  A double is declared by typing double variablename;  The variablename is changeable to anything starting with a letter and anything that has not been assigned to a variable yet.
  • 16.  Multiple variables of the same type are declarable by adding a comma after the first variable and then typing the second.  int one, two, three;
  • 17.  The point of a variable is to eventually hold data.  The variable could be initialized immediately with a value.  int one = 1;  double duck = 12.5;
  • 18. To print out the value a variable is holding you use the printf statement  printf(“The variable one = %d”, one);  As shown above, the text you want printed out is held within the quotation marks. A %variabletype is added to signal to the computer you want to print out a variable.  int -> %d float -> %fl double -> %lf  After the comma, you list the variables in order that match up with the %variables
  • 19.  A double could be printed out but it may contain too many decimal places.  Limit a double by using %.#lf for the number of decimal places you want to be printed out.  The key “n” to a computer is interpreted as a line break.  When used in a printf statement, it will add a line break into the output.
  • 20. int main(){ int one = 5.5; float two = 7.5; double three = 10.5; printf("One equals: %d n", one); printf("Two equals: %f n", two); printf("Three equals: %lf n ", three); three = 15; printf("Three equals: %lf ", three); system("pause"); return 0; }
  • 21. Programming Template  Integers, Floats, and Doubles  Printf statements