Friday, June 23, 2023

Basic Steps

 


Step 1:Printing something on screen

Code:

#include<stdio.h>

#include<conio.h>

void main()

{

int i=10;

clrscr();

print("%d",i);

getch();

}

Explanation:This just prints i's value on screen that is 10


Step 2: Using for loop

Code:

#include<stdio.h>

#include<conio.h>

void main()

{

int i=,n=5;

clrscr();

for(i=1;i<=10;i++)

{

print("*");

}

getch();

}

Explanation: This loop will iterate from initial value 1 till it reaches 10 so the output will have 10 *


Step 3:Using \n

Code:

#include<stdio.h>

#include<conio.h>

void main()

{

int i=,n=5;

clrscr();

for(i=1;i<=10;i++)

{

print("*");

printf("\n");

}

getch();

}

Explanation:The \n character is used to move the cursor to the next line


Step 4: Using nested for loop

     a for loop placed inside another for loop is called nested for loop. The outer loop will be called outer for loop and inner loop will be called inner for loop .

#include<stdio.h>

#include<conio.h>

void main()

int i,j,n;

n=5;

for(i=1;i<=10;i++)

{         for(j=1;j<=5;i++)

               { 

                   printf("*");

                 }

printf("\n");

}

Explanation: ▪️The first loop is outer for loop ▪️The second loop is inner for loop

▪️Basic thing that you need to understand is that the outer for loop specifies the number of lines you want. In the above example it specifies that it will iterate from 1 to 10 i.e 10 lines

▪️The second loop specifies what needs to be printed each time for the iteration of the outer loop from 1 to 10.The inner loop iterates through 1 to 5 and each time '*' is printed. After all the 5 '*' printf("\n") is used to move to the next line

▪️ The output will be 10 lines of pattern"*****"


Step 5: Using condition to print something

#include<stdio.h>

#include<conio.h>

void main()

int i,j,n;

n=5;

for(i=1;i<=10;i++)

{        for(j=1;j<=5;i++)

              {

                    if(i == 1)

                        {

                             printf("*");

                         }

                          else

                          {

                            printf("o");

                           }

                 }

printf("\n");

}


Explanation: In this code we have used if else statements which will decide what will be printed.

 For the first line i.e i == 1 it will print "*" 5 times but for the next 9 lines it will print "o"  5 times

because i has been incremented    


Step 6 Logical And

#include<stdio.h>
#include<conio.h>
void main(){
int i=20,j=30;
clrscr();
if(i==20 && j == 30)
{
printf("Both conditions return true");
}
else{
printf("Atleast one of them is false")
}
getch();

}

  • Logical and "&&" sees that both condition should return true then only it returns true
  • In this case both are true so it will print Both conditions return true

Step 7

#include<stdio.h>
#include<conio.h>
void main(){
int i=20,j=30;
clrscr();
if(i==20 || j == 50)
{
printf("Atleast one of the conditions returns true");
}
else{
printf("Both  returns false");
}
getch();

}
 
  • Logical or "||" sees that atleast one condition should return true then it returns true
  • In this case first condition is true while second is false
  • Since atleast one of the condition is true it returns true
  • It prints "Atleast one of the conditions return true"


These were the basic  steps that you need to know before starting to learn coding patterns                 






No comments:

Post a Comment